]> source.dussan.org Git - javassist.git/commitdiff
Fix #252 make instrumentation works on JDK11 for the inner class which has access... 253/head
authorSam Ma <samuel.ma2012@gmail.com>
Wed, 20 Mar 2019 02:08:43 +0000 (13:08 +1100)
committerSam Ma <samuel.ma2012@gmail.com>
Wed, 20 Mar 2019 02:08:43 +0000 (13:08 +1100)
src/main/javassist/compiler/MemberCodeGen.java
src/test/javassist/JvstTest5.java
src/test/test5/NestHost3.java [new file with mode: 0644]

index ad254816ecd8c9b23544b04ba73c0ea620bb25ab..25be0ee1f42d1e1ce5e3353d5e1e84b8361e921d 100644 (file)
@@ -636,8 +636,22 @@ public class MemberCodeGen extends CodeGen {
                 throw new CompileError("no such constructor: " + targetClass.getName());
 
             if (declClass != thisClass && AccessFlag.isPrivate(acc)) {
-                desc = getAccessibleConstructor(desc, declClass, minfo);
-                bytecode.addOpcode(Opcode.ACONST_NULL); // the last parameter
+                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) {
+                    desc = getAccessibleConstructor(desc, declClass, minfo);
+                    bytecode.addOpcode(Opcode.ACONST_NULL); // the last parameter
+                }
             }
         }
         else if (AccessFlag.isPrivate(acc))
index 915e1e1ae56bef736bb0637e3bd5793ddf762394..96f4635619d334a3030182413f1106c9fd17ca76 100644 (file)
@@ -14,6 +14,7 @@ import javassist.bytecode.NestMembersAttribute;
 import javassist.expr.ExprEditor;
 import javassist.expr.Handler;
 import javassist.expr.MethodCall;
+import javassist.expr.NewExpr;
 
 @SuppressWarnings({"rawtypes","unchecked","unused"})
 public class JvstTest5 extends JvstTestRoot {
@@ -469,6 +470,26 @@ public class JvstTest5 extends JvstTestRoot {
         assertEquals(2, invoke(obj, "run"));   
     }
 
+    public void testNestPrivateConstructor() throws Exception {
+        CtClass cc = sloader.get("test5.NestHost3$Builder");
+        cc.instrument(new ExprEditor() {
+            public void edit(NewExpr e) throws CannotCompileException {
+                String code = "$_ = $proceed($$);";
+                e.replace(code);
+            }
+        });
+        cc.writeFile();
+        try {
+            Class<?> nestHost3Class = cloader.loadClass("test5.NestHost3");
+            Object builder = nestHost3Class.getDeclaredMethod("builder").invoke(nestHost3Class);
+            Class<?> nestHost3BuilderClass = cloader.loadClass("test5.NestHost3$Builder");
+            nestHost3BuilderClass.getDeclaredMethod("build").invoke(builder);
+        } 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/NestHost3.java b/src/test/test5/NestHost3.java
new file mode 100644 (file)
index 0000000..912a275
--- /dev/null
@@ -0,0 +1,19 @@
+package test5;
+
+public class NestHost3 {
+  private NestHost3(Builder builder) {
+  }
+
+  public static Builder builder() {
+    return new Builder();
+  }
+
+  public static final class Builder {
+    public Builder() {
+    }
+
+    public NestHost3 build() {
+      return new NestHost3(this);
+    }
+  }
+}