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

IllegalStateException when inner class has private constructor on Java>8 #958

Closed
remibarat opened this issue Nov 20, 2018 · 4 comments
Closed
Assignees

Comments

@remibarat
Copy link

@remibarat remibarat commented Nov 20, 2018

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
@suojian
Copy link

@suojian suojian commented Dec 6, 2018

I have same issue.

@sam-ma
Copy link
Contributor

@sam-ma sam-ma commented Mar 8, 2019

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.

denis-yuen added a commit to dockstore/dockstore that referenced this issue Apr 2, 2019
denis-yuen added a commit to dockstore/dockstore that referenced this issue Apr 5, 2019
* Fix liquibase and maybe jacoco
* Avoid powermock/powermock#958
* Xenial should be closer to our collab environment
denis-yuen added a commit to dockstore/dockstore that referenced this issue Apr 10, 2019
* Working on collab VM

* Fix liquibase and maybe jacoco
* Avoid powermock/powermock#958
* Xenial should be closer to our collab environment

* Iterate to 1.7.0

* Xenial requires sudo certs?
* Update failsafe and surefire
* Try Maven 3.6.0?
* Ignore TokenResourceIT due to mocking DockstoreWebserviceConfiguration issue

* Update readme and enforcer
PowerMock 2.x automation moved this from To Do to Done Apr 21, 2019
@remibarat
Copy link
Author

@remibarat remibarat commented Apr 24, 2019

Thank you! 👍

tobous added a commit to saros-project/saros that referenced this issue Mar 6, 2020
This was done to avoid an issue with the CTOR accessibility occurring
when using Java 11.

This issue should theoretically have been resolved with
powermock/powermock#958.
powermock/powermock#958

My guess would be that the issue still occurs as the private CTOR is
part of the test class (and not the tested classes), meaning the
PowerMock runner can't modify them as they are already completely parsed
as that point.
tobous added a commit to saros-project/saros that referenced this issue Mar 6, 2020
This was done to avoid an issue with the CTOR accessibility occurring
when using Java 11.

This issue should theoretically have been resolved with
powermock/powermock#958.
powermock/powermock#958

My guess would be that the issue still occurs as the private CTOR is
part of the test class (and not the tested classes), meaning the
PowerMock runner can't modify them as they are already completely parsed
as that point.
@mansourofski
Copy link

@mansourofski mansourofski commented Apr 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
PowerMock 2.x
  
Done
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants