Powermock: IllegalStateException when inner class has private constructor on Java>8

Created on 20 Nov 2018  路  4Comments  路  Source: powermock/powermock

Hi,
Using

  • powermock 2.0.0-RC.4
  • java 11.0.1-open
  • javassist 3.23.1-GA

when I run the following code, it works with Java 8 but not with Java 11

@RunWith(PowerMockRunner.class)
@PrepareForTest(OuterClass.class)
public class TestPowerMock {
    @Test
    public void doNothing() {}

}

public class OuterClass {
    private static InnerSingleton theInstance = new InnerSingleton();
    public static class InnerSingleton {
        private InnerSingleton() {}
    }
}

StackTrace is:

java.lang.IllegalStateException: Failed to transform class with name com.activeviam.lic.impl.OuterClass. Reason: [source error] the called constructor is private in com.activeviam.lic.impl.OuterClass$InnerSingleton

    at org.powermock.core.classloader.javassist.JavassistMockClassLoader.defineAndTransformClass(JavassistMockClassLoader.java:119)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:174)
    at org.powermock.core.classloader.MockClassLoader.loadClassByThisClassLoader(MockClassLoader.java:102)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:147)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:98)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at java.base/sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at java.base/sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:440)
    at java.base/sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:421)
    at java.base/sun.reflect.annotation.AnnotationParser.lambda$parseClassArray$0(AnnotationParser.java:719)
    at java.base/sun.reflect.annotation.AnnotationParser.parseArrayElements(AnnotationParser.java:747)
    at java.base/sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:718)
    at java.base/sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:532)
    at java.base/sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:356)
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:287)
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:121)
    at java.base/sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:73)
    at java.base/java.lang.Class.createAnnotationData(Class.java:3758)
    at java.base/java.lang.Class.annotationData(Class.java:3747)
    at java.base/java.lang.Class.getAnnotation(Class.java:3652)
    at org.junit.internal.MethodSorter.getDeclaredMethods(MethodSorter.java:52)
    at org.junit.internal.runners.TestClass.getAnnotatedMethods(TestClass.java:45)
    at org.junit.internal.runners.MethodValidator.validateTestMethods(MethodValidator.java:71)
    at org.junit.internal.runners.MethodValidator.validateStaticMethods(MethodValidator.java:44)
    at org.junit.internal.runners.MethodValidator.validateMethodsForDefaultRunner(MethodValidator.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.validate(PowerMockJUnit44RunnerDelegateImpl.java:124)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.<init>(PowerMockJUnit44RunnerDelegateImpl.java:86)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl.<init>(PowerMockJUnit47RunnerDelegateImpl.java:42)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit49RunnerDelegateImpl.<init>(PowerMockJUnit49RunnerDelegateImpl.java:25)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:165)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:47)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:107)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:69)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:36)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    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: javassist.CannotCompileException: [source error] the called constructor is private in com.activeviam.lic.impl.OuterClass$InnerSingleton
    at javassist.expr.NewExpr.replace(NewExpr.java:235)
    at org.powermock.core.transformers.javassist.support.PowerMockExpressionEditor.edit(PowerMockExpressionEditor.java:73)
    at javassist.expr.ExprEditor.loopBody(ExprEditor.java:217)
    at javassist.expr.ExprEditor.doit(ExprEditor.java:96)
    at javassist.CtClassType.instrument(CtClassType.java:1541)
    at org.powermock.core.transformers.javassist.InstrumentMockTransformer.transform(InstrumentMockTransformer.java:41)
    at org.powermock.core.transformers.javassist.AbstractJavaAssistMockTransformer.transform(AbstractJavaAssistMockTransformer.java:40)
    at org.powermock.core.transformers.support.DefaultMockTransformerChain.transform(DefaultMockTransformerChain.java:43)
    at org.powermock.core.classloader.MockClassLoader.transformClass(MockClassLoader.java:184)
    at org.powermock.core.classloader.javassist.JavassistMockClassLoader.defineAndTransformClass(JavassistMockClassLoader.java:102)
    ... 56 more
Caused by: compile error: the called constructor is private in com.activeviam.lic.impl.OuterClass$InnerSingleton
    at javassist.compiler.MemberCodeGen.getAccessibleConstructor(MemberCodeGen.java:740)
    at javassist.compiler.MemberCodeGen.atMethodCallCore2(MemberCodeGen.java:639)
    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:908)
    at javassist.compiler.CodeGen.atVariableAssign(CodeGen.java:841)
    at javassist.compiler.CodeGen.atAssignExpr(CodeGen.java:795)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:362)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:53)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:381)
    at javassist.compiler.ast.Stmnt.accept(Stmnt.java:53)
    at javassist.compiler.CodeGen.atIfStmnt(CodeGen.java:441)
    at javassist.compiler.CodeGen.atStmnt(CodeGen.java:385)
    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)
    ... 65 more
bug jdk9

Most helpful comment

I think the issue is due to JEP181 JDK11 stops creating special access method for nested classes. In addition, classes go through the transformer chain one by one, which means there are chances (I'm not sure how the order is determined, alphabet order?) the nest host class (OuterClass in the test case) passes through the chain before the nest member class (InnerSingleton in the test case). In that case, the constructor is still private which caused the compilation error.

All 4 comments

I have same issue.

I think the issue is due to JEP181 JDK11 stops creating special access method for nested classes. In addition, classes go through the transformer chain one by one, which means there are chances (I'm not sure how the order is determined, alphabet order?) the nest host class (OuterClass in the test case) passes through the chain before the nest member class (InnerSingleton in the test case). In that case, the constructor is still private which caused the compilation error.

Thank you! :+1:

I think the issue is due to JEP181 JDK11 stops creating special access method for nested classes. In addition, classes go through the transformer chain one by one, which means there are chances (I'm not sure how the order is determined, alphabet order?) the nest host class (OuterClass in the test case) passes through the chain before the nest member class (InnerSingleton in the test case). In that case, the constructor is still private which caused the compilation error.

I have the same issue plz How could I resolve

[14:41] SRIDI Mansour

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.566 s <<< FAILURE! - in com.byzaneo.task.test.util.TaskEventHelperTest
[ERROR] com.byzaneo.task.test.util.TaskEventHelperTest.initializationError Time elapsed: 0.009 s <<< ERROR!
java.lang.IllegalStateException: Failed to transform class with name com.byzaneo.task.util.TaskHelper. Reason: [source error] the called constructor is private in com.byzaneo.task.util.TaskHelper$TaskAttributes
Caused by: javassist.CannotCompileException: [source error] the called constructor is private in com.byzaneo.task.util.TaskHelper$TaskAttributes
Caused by: javassist.compiler.CompileError: the called constructor is private in com.byzaneo.task.util.TaskHelper$TaskAttributes

Was this page helpful?
0 / 5 - 0 ratings