Skip to content

Commit

Permalink
Merge pull request jboss-javassist#267 from sam-ma/master
Browse files Browse the repository at this point in the history
Fix jboss-javassist#265 javassist.CannotCompileException: [source error] the called …
  • Loading branch information
chibash authored Sep 1, 2019
2 parents cf1d22f + d6d6b2e commit bad4a16
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/main/javassist/compiler/MemberCodeGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ public void atMethodCallCore(CtClass targetClass, String mname,
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,
Expand All @@ -636,19 +648,8 @@ private void atMethodCallCore2(CtClass targetClass, String mname,
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
}
Expand Down
27 changes: 27 additions & 0 deletions src/test/javassist/JvstTest5.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,33 @@ public void edit(NewExpr e) throws CannotCompileException {
}
}

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(
Expand Down
26 changes: 26 additions & 0 deletions src/test/test5/NestHost4.java
Original file line number Diff line number Diff line change
@@ -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 {

}
}
}

0 comments on commit bad4a16

Please sign in to comment.