Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #252 make instrumentation works on JDK11 for the inner class which has access to the private constructor of the host class #253

Merged
merged 1 commit into from Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/main/javassist/compiler/MemberCodeGen.java
Expand Up @@ -636,8 +636,22 @@ private void atMethodCallCore2(CtClass targetClass, String mname,
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))
Expand Down
21 changes: 21 additions & 0 deletions src/test/javassist/JvstTest5.java
Expand Up @@ -14,6 +14,7 @@
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 {
Expand Down Expand Up @@ -469,6 +470,26 @@ public void testSwitchCaseWithStringConstant() throws Exception {
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(
Expand Down
19 changes: 19 additions & 0 deletions src/test/test5/NestHost3.java
@@ -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);
}
}
}