]> source.dussan.org Git - javassist.git/commitdiff
Fix #265 javassist.CannotCompileException: [source error] the called constructor... 267/head
authorSam Ma <samuel.ma2012@gmail.com>
Tue, 23 Jul 2019 11:35:24 +0000 (21:35 +1000)
committerSam Ma <samuel.ma2012@gmail.com>
Tue, 23 Jul 2019 11:35:24 +0000 (21:35 +1000)
src/main/javassist/compiler/MemberCodeGen.java
src/test/javassist/JvstTest5.java
src/test/test5/NestHost4.java [new file with mode: 0644]

index 25be0ee1f42d1e1ce5e3353d5e1e84b8361e921d..f1ec84a5757c1597ab1944635da55a744dc0a8ac 100644 (file)
@@ -619,6 +619,18 @@ public class MemberCodeGen extends CodeGen {
                           aload0pos, found);
     }
 
+    private boolean isFromSameDeclaringClass(CtClass outer, CtClass inner) {
+        try {
+            while (outer != null) {
+                if (isEnclosing(outer, inner))
+                    return true;
+                outer = outer.getDeclaringClass();
+            }
+        }
+        catch (NotFoundException e) {}
+        return false;
+    }
+
     private void atMethodCallCore2(CtClass targetClass, String mname,
                                    boolean isStatic, boolean isSpecial,
                                    int aload0pos,
@@ -636,19 +648,8 @@ public class MemberCodeGen extends CodeGen {
                 throw new CompileError("no such constructor: " + targetClass.getName());
 
             if (declClass != thisClass && AccessFlag.isPrivate(acc)) {
-                boolean isNested = false;
-                if (declClass.getClassFile().getMajorVersion() >= ClassFile.JAVA_11) {
-                    try {
-                        CtClass[] nestedClasses = declClass.getNestedClasses();
-                        for (int i = 0; i < nestedClasses.length; i++) {
-                            if (thisClass == nestedClasses[i]) {
-                                isNested = true;
-                                break;
-                            }
-                        }
-                    } catch (NotFoundException ignored) { }
-                }
-                if (!isNested) {
+                if (declClass.getClassFile().getMajorVersion() < ClassFile.JAVA_11
+                        || !isFromSameDeclaringClass(declClass, thisClass)) {
                     desc = getAccessibleConstructor(desc, declClass, minfo);
                     bytecode.addOpcode(Opcode.ACONST_NULL); // the last parameter
                 }
index 96f4635619d334a3030182413f1106c9fd17ca76..4d4fc719df2e074eca863963c01d37741cd4f09f 100644 (file)
@@ -490,6 +490,33 @@ public class JvstTest5 extends JvstTestRoot {
         }
     }
 
+    public void testNestPrivateConstructor2() throws Exception {
+        CtClass cc = sloader.get("test5.NestHost4$InnerClass1");
+        cc.instrument(new ExprEditor() {
+            public void edit(NewExpr e) throws CannotCompileException {
+                String code = "$_ = $proceed($$);";
+                e.replace(code);
+            }
+        });
+        cc.writeFile();
+
+        cc = sloader.get("test5.NestHost4$InnerClass1$InnerClass5");
+        cc.instrument(new ExprEditor() {
+            public void edit(NewExpr e) throws CannotCompileException {
+                String code = "$_ = $proceed($$);";
+                e.replace(code);
+            }
+        });
+        cc.writeFile();
+        try {
+            Class<?> nestHost4Class = cloader.loadClass("test5.NestHost4");
+            nestHost4Class.getDeclaredConstructor().newInstance();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            fail("it should be able to access the private constructor of the nest host");
+        }
+    }
+
     public void testSwitchCaseWithStringConstant2() throws Exception {
         CtClass cc = sloader.makeClass("test5.SwitchCase2");
         cc.addMethod(CtNewMethod.make(
diff --git a/src/test/test5/NestHost4.java b/src/test/test5/NestHost4.java
new file mode 100644 (file)
index 0000000..a8d60f0
--- /dev/null
@@ -0,0 +1,26 @@
+package test5;
+
+public class NestHost4 {
+  public void test() {
+    new InnerClass1().new InnerClass5();
+  }
+
+  private class InnerClass1 {
+    private InnerClass1() {
+      new InnerClass2();
+    }
+
+    private class InnerClass5 {
+      private InnerClass5() {
+        new InnerClass2().new InnerClass3();
+      }
+    }
+  }
+
+  private class InnerClass2 {
+
+    private class InnerClass3 {
+
+    }
+  }
+}