powermock: PowerMockito 2 + Java 11 causes "An illegal reflective access operation has occurred"

Getting these for Java 11.0.2 + PowerMockito 2.0.0:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl (file:...) to method java.lang.Object.clone()
WARNING: Please consider reporting this to the maintainers of org.powermock.reflect.internal.WhiteboxImpl
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

I tried to add this annotation, but it didn’t help: @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 111
  • Comments: 24

Commits related to this issue

Most upvoted comments

Please stop posting “+1”-style comments if you don’t have any additional information on the issue, and use the issue’s reaction feature instead (clicking the thumbs up).

+1 same issue

Any suggestion hint on this?

The same occurs with PowerMock-EasyMock 2.0.2:

    testCompile 'org.powermock:powermock-module-junit4:2.0.2'
    testCompile 'org.powermock:powermock-api-easymock:2.0.2'

and JDK 11:

java version "11.0.3" 2019-04-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.3+12-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.3+12-LTS, mixed mode)

+1 same issue

Having the same issue here. Not able to move to jdk 11.

I just upgraded our project from Java 8 to Java 11 and I solved this issue as a workaround with the following, in Maven (check especially the argLine tags):

   <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                        --add-opens java.xml/jdk.xml.internal=ALL-UNNAMED
                    </argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
                <configuration>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
     </pluginManagement>

As you see the argLine has a new line for each new command. I only needed the java.xml/jdk.xml.internal to be bypassed, because I had errors only on this Java 11 package; eventually you can add other packages aswell.

I didn’t have to use any @PowermockIgnore annotation.

These warnings are due to the fact that as of Java 9 a module system was introduced. Powermock relies heavily on reflection and other techniques to provide its functionality. When these techniques cross module boundaries these warnings are thrown. I found that by properly configuring the test runner to explicitly “open” the modules, I was able to eliminate these warnings with no change to powermock itself. The configuration depends on the set of these crossed module boundaries. In my case in a gradle environment the following eliminated these warnings:

test.jvmArgs = [“–illegal-access=debug”, “–add-opens”,“java.logging/java.util.logging=ALL-UNNAMED”, “–add-opens”,“java.base/java.time.zone=ALL-UNNAMED”, “–add-opens”,“java.base/java.lang.reflect=ALL-UNNAMED”, “–add-opens”,“java.base/java.security.cert=ALL-UNNAMED”, “–add-opens”,“java.base/java.text=ALL-UNNAMED”, “–add-opens”,“java.base/java.net=ALL-UNNAMED”, “–add-opens”,“java.base/java.nio.charset=ALL-UNNAMED”, “–add-opens”,“java.base/java.nio.file=ALL-UNNAMED”, “–add-opens”,“java.base/sun.nio.fs=ALL-UNNAMED”, “–add-opens”,“java.base/sun.security.x509=ALL-UNNAMED”, “–add-opens”,“java.base/java.util.regex=ALL-UNNAMED”, “–add-opens”,“java.base/java.util.stream=ALL-UNNAMED”, “–add-opens”,“java.base/java.util.concurrent=ALL-UNNAMED”, “–add-opens”,“java.base/java.util.concurrent.atomic=ALL-UNNAMED”, “–add-opens”,“java.base/java.util.concurrent.locks=ALL-UNNAMED”, “–add-opens”,“java.base/java.time=ALL-UNNAMED”, “–add-opens”,“java.base/java.util=ALL-UNNAMED”, “–add-opens”,“java.base/java.io=ALL-UNNAMED”, “–add-opens”,“java.base/java.lang=ALL-UNNAMED”]

+1

+1 same here

+1 same here

+1 same issue

Also seeing this issue

image image https://stackoverflow.com/questions/56039341/get-declared-fields-of-java-lang-reflect-fields-in-jdk12/

For the devs, this is how you reflect into and set final variables with Java 11+. Hopefully they can add this to PowerMock, so it works there as well! I never tested it on Java 11, but it should work.