jdk-24/test/jdk/java/lang/invoke/defineHiddenClass/SelfReferenceDescriptor.java
Mandy Chung 7cc1371059 8238358: Implementation of JEP 371: Hidden Classes
Co-authored-by: Lois Foltan <lois.foltan@oracle.com>
Co-authored-by: David Holmes <david.holmes@oracle.com>
Co-authored-by: Harold Seigel <harold.seigel@oracle.com>
Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com>
Co-authored-by: Alex Buckley <alex.buckley@oracle.com>
Co-authored-by: Jamsheed Mohammed C M <jamsheed.c.m@oracle.com>
Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com>
Co-authored-by: Amy Lu <amy.lu@oracle.com>
Reviewed-by: alanb, cjplummer, coleenp, dholmes, dlong, forax, jlahoda, psandoz, plevart, sspitsyn, vromero
2020-04-21 06:55:38 -07:00

104 lines
3.9 KiB
Java

/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @modules jdk.compiler
* @library /test/lib
* @build jdk.test.lib.Utils
* jdk.test.lib.compiler.CompilerUtils
* SelfReferenceDescriptor
* @run main/othervm -Xverify:remote SelfReferenceDescriptor
* @summary Test that a hidden class cannot be referenced in descriptor
*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
import static java.lang.invoke.MethodHandles.lookup;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.Utils;
/* package-private */ interface Test {
void test();
}
public class SelfReferenceDescriptor {
private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC);
private static final Path CLASSES_DIR = Paths.get("classes");
static void compileSources(Path sourceFile, String... options) throws IOException {
Stream<String> ops = Stream.of("-cp", Utils.TEST_CLASSES + File.pathSeparator + CLASSES_DIR);
if (options != null && options.length > 0) {
ops = Stream.concat(ops, Arrays.stream(options));
}
if (!CompilerUtils.compile(sourceFile, CLASSES_DIR, ops.toArray(String[]::new))) {
throw new RuntimeException("Compilation of the test failed: " + sourceFile);
}
}
// Test that a hidden class cannot use its own name in a field
// signature.
public static void hiddenClassInFieldDescriptor() throws Exception {
compileSources(SRC_DIR.resolve("SelfRefField.java"));
Path path = CLASSES_DIR.resolve("SelfRefField.class");
byte[] bytes = Files.readAllBytes(path);
try {
lookup().defineHiddenClass(bytes, false, NESTMATE);
throw new RuntimeException("expected NCDFE in defining SelfRefField hidden class");
} catch (NoClassDefFoundError e) {
if (!e.getMessage().contains("SelfRefField")) throw e;
}
}
// Test that a hidden class cannot use its own name in a method
// signature.
public static void hiddenClassInMethodDescriptor() throws Exception {
compileSources(SRC_DIR.resolve("SelfRefMethod.java"));
Path path = CLASSES_DIR.resolve("SelfRefMethod.class");
byte[] bytes = Files.readAllBytes(path);
try {
lookup().defineHiddenClass(bytes, false, NESTMATE);
throw new RuntimeException("expected NCDFE in defining SelfRefMethod hidden class");
} catch (NoClassDefFoundError e) {
if (!e.getMessage().contains("SelfRefMethod")) throw e;
}
}
public static void main(String... args) throws Exception {
hiddenClassInMethodDescriptor();
hiddenClassInFieldDescriptor();
}
}