/* * Copyright (c) 2017, 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 java.base/java.lang:open * java.base/jdk.internal.org.objectweb.asm * @run testng/othervm test.DefineClassTest * @summary Basic test for java.lang.invoke.MethodHandles.Lookup.defineClass
*/
/** * Test that a class has the same class loader, and is in the same package and * protection domain, as a lookup class.
*/ void testSameAbode(Class<?> clazz, Class<?> lc) {
assertTrue(clazz.getClassLoader() == lc.getClassLoader());
assertEquals(clazz.getPackageName(), lc.getPackageName());
assertTrue(clazz.getProtectionDomain() == lc.getProtectionDomain());
}
/** * Tests that a class is discoverable by name using Class.forName and * lookup.findClass
*/ void testDiscoverable(Class<?> clazz, Lookup lookup) throws Exception {
String cn = clazz.getName();
ClassLoader loader = clazz.getClassLoader();
assertTrue(Class.forName(cn, false, loader) == clazz);
assertTrue(lookup.findClass(cn) == clazz);
}
/** * Basic test of defineClass to define a class in the same package as test.
*/
@Test publicvoid testDefineClass() throws Exception { final String CLASS_NAME = THIS_PACKAGE + ".Foo";
Lookup lookup = lookup(); Class<?> clazz = lookup.defineClass(generateClass(CLASS_NAME));
// test name
assertEquals(clazz.getName(), CLASS_NAME);
// test loader/package/protection-domain
testSameAbode(clazz, lookup.lookupClass());
// test discoverable
testDiscoverable(clazz, lookup);
/** * Test public/package/protected/private access from class defined with defineClass.
*/
@Test publicvoid testAccess() throws Exception { final String THIS_CLASS = this.getClass().getName(); final String CLASS_NAME = THIS_PACKAGE + ".Runner";
Lookup lookup = lookup();
/** * Test that defineClass does not run the class initializer
*/
@Test publicvoid testInitializerNotRun() throws Exception { final String THIS_CLASS = this.getClass().getName(); final String CLASS_NAME = THIS_PACKAGE + ".ClassWithClinit";
/** * Test defineClass to define classes in a package containing classes with * different protection domains.
*/
@Test publicvoid testTwoProtectionDomains() throws Exception {
Path here = Paths.get("");
// p.C1 in one exploded directory
Path dir1 = Files.createTempDirectory(here, "classes");
Path p = Files.createDirectory(dir1.resolve("p"));
Files.write(p.resolve("C1.class"), generateClass("p.C1"));
URL url1 = dir1.toUri().toURL();
// p.C2 in another exploded directory
Path dir2 = Files.createTempDirectory(here, "classes");
p = Files.createDirectory(dir2.resolve("p"));
Files.write(p.resolve("C2.class"), generateClass("p.C2"));
URL url2 = dir2.toUri().toURL();
/** * Generates a class file with the given class name
*/ byte[] generateClass(String className) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V9,
ACC_PUBLIC + ACC_SUPER,
className.replace(".", "/"), null, "java/lang/Object", null);
/** * Generate a class file with the given class name. The class implements Runnable * with a run method to invokestatic the given targetClass/targetMethod.
*/ byte[] generateRunner(String className,
String targetClass,
String targetMethod) throws Exception {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V9,
ACC_PUBLIC + ACC_SUPER,
className.replace(".", "/"), null, "java/lang/Object", new String[] { "java/lang/Runnable" });
/** * Generate a class file with the given class name. The class will initializer * to invokestatic the given targetClass/targetMethod.
*/ byte[] generateClassWithInitializer(String className,
String targetClass,
String targetMethod) throws Exception {
/** * Generates a non-linkable class file with the given class name
*/ byte[] generateNonLinkableClass(String className) {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V14,
ACC_PUBLIC + ACC_SUPER,
className.replace(".", "/"), null, "MissingSuperClass", null);
/** * Generates a class file with the given class name
*/ byte[] generateModuleInfo() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
+ ClassWriter.COMPUTE_FRAMES);
cw.visit(V14,
ACC_MODULE, "module-info", null, null, null);
cw.visitEnd(); return cw.toByteArray();
}
privateint nextNumber() { return ++nextNumber;
}
privateint nextNumber;
}
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.