8238195: Lookup::defineClass should link the class to match the specification

Reviewed-by: alanb, chegar
This commit is contained in:
Mandy Chung 2020-04-21 07:09:18 -07:00
parent 1feb24e561
commit 1c6ca09b02
2 changed files with 32 additions and 2 deletions

View File

@ -1631,7 +1631,7 @@ public class MethodHandles {
}
/**
* Creates a class or interface from {@code bytes}
* Creates and links a class or interface from {@code bytes}
* with the same class loader and in the same runtime package and
* {@linkplain java.security.ProtectionDomain protection domain} as this lookup's
* {@linkplain #lookupClass() lookup class} as if calling

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* 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
@ -237,6 +237,11 @@ public class DefineClassTest {
lookup().defineClass(null);
}
@Test(expectedExceptions = { NoClassDefFoundError.class })
public void testLinking() throws Exception {
lookup().defineClass(generateNonLinkableClass(THIS_PACKAGE + ".NonLinkableClass"));
}
/**
* Generates a class file with the given class name
*/
@ -336,6 +341,31 @@ public class DefineClassTest {
return cw.toByteArray();
}
/**
* 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);
// <init>
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "MissingSuperClass", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
cw.visitEnd();
return cw.toByteArray();
}
private int nextNumber() {
return ++nextNumber;
}