< prev index next >

test/jdk/java/lang/invoke/DefineClassTest.java

Print this page
rev 58875 : 8238195: Lookup::defineClass should link the class to match the specification
Reviewed-by: alanb, chegar

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -235,10 +235,15 @@
     @Test(expectedExceptions = { NullPointerException.class })
     public void testNull() throws Exception {
         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
      */
     byte[] generateClass(String className) {
         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS

@@ -334,10 +339,35 @@
 
         cw.visitEnd();
         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;
     }
 
     private int nextNumber;
< prev index next >