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

[JDK11] javassist.CannotCompileException: [source error] the called constructor is private #265

Closed
sam-ma opened this issue Jul 23, 2019 · 0 comments · Fixed by #267
Closed

Comments

@sam-ma
Copy link
Contributor

sam-ma commented Jul 23, 2019

With JDK11, consider the following host class and its two inner classes.

package test5;

public class NestHost4 {
  public void test() {
    new InnerClass1();
  }

  private class InnerClass1 {
    private InnerClass1() {
      new InnerClass2();
    }

  private class InnerClass2 {
  }
}

A simple instrumentation WITHOUT any modification should work. However, javassist.CannotCompileException: [source error] the called constructor is private was thrown instead.

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();
javassist.CannotCompileException: [source error] the called constructor is private in test5.NestHost4$InnerClass2

	at javassist.expr.NewExpr.replace(NewExpr.java:235)
	at javassist.JvstTest5$4.edit(JvstTest5.java:498)
	at javassist.expr.ExprEditor.loopBody(ExprEditor.java:217)
	at javassist.expr.ExprEditor.doit(ExprEditor.java:96)
	at javassist.CtClassType.instrument(CtClassType.java:1541)
	at javassist.JvstTest5.testNestPrivateConstructor2(JvstTest5.java:495)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at junit.framework.TestCase.runTest(TestCase.java:176)
	at junit.framework.TestCase.runBare(TestCase.java:141)
	at junit.framework.TestResult$1.protect(TestResult.java:122)
	at junit.framework.TestResult.runProtected(TestResult.java:142)
	at junit.framework.TestResult.run(TestResult.java:125)
	at junit.framework.TestCase.run(TestCase.java:129)
	at junit.framework.TestSuite.runTest(TestSuite.java:252)
	at junit.framework.TestSuite.run(TestSuite.java:247)
	at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: compile error: the called constructor is private in test5.NestHost4$InnerClass2
	at javassist.compiler.MemberCodeGen.getAccessibleConstructor(MemberCodeGen.java:771)
	at javassist.compiler.MemberCodeGen.atMethodCallCore2(MemberCodeGen.java:669)
	at javassist.compiler.MemberCodeGen.atMethodCallCore(MemberCodeGen.java:618)
	at javassist.expr.NewExpr$ProceedForNew.doit(NewExpr.java:259)
	at javassist.compiler.JvstCodeGen.atCallExpr(JvstCodeGen.java:252)
	at javassist.compiler.ast.CallExpr.accept(CallExpr.java:49)
	at javassist.compiler.CodeGen.atAssignCore(CodeGen.java:956)
	at javassist.compiler.CodeGen.atVariableAssign(CodeGen.java:889)
	at javassist.compiler.CodeGen.atAssignExpr(CodeGen.java:843)
	at javassist.compiler.CodeGen.atStmnt(CodeGen.java:362)
	at javassist.compiler.ast.Stmnt.accept(Stmnt.java:53)
	at javassist.compiler.Javac.compileStmnt(Javac.java:578)
	at javassist.expr.NewExpr.replace(NewExpr.java:229)
	... 23 more
sam-ma added a commit to sam-ma/javassist that referenced this issue Jul 23, 2019
chibash added a commit that referenced this issue Sep 1, 2019
Fix #265 javassist.CannotCompileException: [source error] the called …
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 10, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 17, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6a404f1)
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 17, 2019
3.25 fixes:
- jboss-javassist/javassist#72
- jboss-javassist/javassist#241
- jboss-javassist/javassist#242
- jboss-javassist/javassist#246
- jboss-javassist/javassist#252
3.26 fixes:
- jboss-javassist/javassist#265
- jboss-javassist/javassist#270
- jboss-javassist/javassist#271
- jboss-javassist/javassist#275

Of these #270 is most important, as it fixes an issue we've seen
with powermock downstream.

Change-Id: Ib4d75d6411e71438436249a8eb9313ccf4411ca2
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 6a404f1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant