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

illegal reflective access operation: AccessibilityChanger #1325

Closed
lviggiano opened this issue Feb 26, 2018 · 36 comments
Closed

illegal reflective access operation: AccessibilityChanger #1325

lviggiano opened this issue Feb 26, 2018 · 36 comments

Comments

@lviggiano
Copy link

Hi.

I'm using IntelliJ Community 2017.3 and when I run junit tests using "Menu>Run>Run All In Owner" in my project (https://github.com/lviggiano/owner/) I get the following warning at the console:

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51785:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA CE.app/Contents/plugins/junit/lib/junit5-rt.jar:/Users/luigi/var/src/owner/owner/target/test-classes:/Users/luigi/var/src/owner/owner/target/classes:/Users/luigi/.m2/repository/org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar:/Users/luigi/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/luigi/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/luigi/.m2/repository/org/mockito/mockito-core/2.15.0/mockito-core-2.15.0.jar:/Users/luigi/.m2/repository/net/bytebuddy/byte-buddy/1.7.9/byte-buddy-1.7.9.jar:/Users/luigi/.m2/repository/net/bytebuddy/byte-buddy-agent/1.7.9/byte-buddy-agent-1.7.9.jar:/Users/luigi/.m2/repository/org/objenesis/objenesis/2.6/objenesis-2.6.jar:/Users/luigi/.m2/repository/commons-codec/commons-codec/1.11/commons-codec-1.11.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 @w@/private/var/folders/yx/d_5sbt_j7ns2x62q3x_7lcgr0000gn/T/idea_working_dirs_junit.tmp @/private/var/folders/yx/d_5sbt_j7ns2x62q3x_7lcgr0000gn/T/idea_junit.tmp -socket51784
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/Users/luigi/.m2/repository/org/mockito/mockito-core/2.15.0/mockito-core-2.15.0.jar) to field java.util.Properties.defaults
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

Process finished with exit code 0

I cannot identify the problematic test, since I have hundreds and this happens when I run them all at once. But eventually if I find the problematic one, I'll update this issue.

I'm using mockito-core 2.15.0; java 9.0.1, Maven 3.3.9, Mac OS X 10.13.3

/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=/Users/luigi/var/src/owner "-Dmaven.home=/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3" "-Dclassworlds.conf=/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/bin/m2.conf" "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=51962:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/boot/plexus-classworlds-2.5.2.jar" org.codehaus.classworlds.Launcher -Didea.version=2017.3.4 -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T17:41:47+01:00)
Maven home: /Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3
Java version: 9.0.1, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
Default locale: en_IT, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.3", arch: "x86_64", family: "mac"

Process finished with exit code 0

Thanks & Best regards,
L.

@ssoloff
Copy link

ssoloff commented Mar 27, 2018

Not sure if this helps, but I'm seeing the same issue in a different project. We've tracked it down to spying on types from the java.base module. The following MCVE demonstrates the issue:

package org.triplea;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.mockito.Mockito.when;

import java.io.File;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public final class MockitoJava9Test {
  @Test
  public void spyReportsIllegalReflectiveAccess() {
    final File directory = Mockito.spy(new File("path"));
    // NB: using a mock instead of a spy does not report illegal reflective access
    // final File directory = Mockito.mock(File.class);

    final File file1 = new File("file1");
    final File file2 = new File("file2");
    when(directory.listFiles()).thenReturn(new File[] {file1, file2});

    assertThat(directory.listFiles(), arrayContaining(file1, file2));
  }
}

Running the test as-is reports the following warnings to the console:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/home/ssoloff/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/2.17.0/dbc47ed385a0b14665adf5dfe74007d9b5f5d034/mockito-core-2.17.0.jar) to field java.io.File.path
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

Commenting out the initialization using the spy and uncommenting the initialization using the mock produces no warnings.

@luan-cestari
Copy link

luan-cestari commented Nov 15, 2018

Hi,

Found another similar issue running OpenJDK 11:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/..../org.mockito/mockito-core/2.23.0/497ddb32fd5d01f9dbe99a2ec790aeb931dff1b1/mockito-core-2.23.0.jar) to field java.util.HashSet.map
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

@ealmasque
Copy link

Same here:

[info] Mockito can only mock non-private & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info]
[info] Java : 10
[info] JVM vendor name : Oracle Corporation
[info] JVM vendor version : 10.0.2-adoptopenjdk+13
[info] JVM name : OpenJDK 64-Bit Server VM
[info] JVM version : 10.0.2-adoptopenjdk+13
[info] JVM info : mixed mode
[info] OS name : Mac OS X
[info] OS version : 10.13.6

@jnape
Copy link

jnape commented Feb 17, 2019

I'm also seeing this warning using jdk11 and mockito-core 2.24.0, the latest stable version at the time of this writing:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/Users/jnape/.m2/repository/org/mockito/mockito-core/2.24.0/mockito-core-2.24.0.jar) to field java.util.ArrayList.elementData
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

Is there any hope of this being addressed in the near future? I don't have a good sense from the referenced issue about how much of a commitment it is to fix this on Mockito's side.

@triceo
Copy link

triceo commented Aug 15, 2019

Another illegal reflective access, this time to java.util.HashMap.table.
This causes all kinds of issues, especially together with Maven Surefire, as the JDK's error message will corrupt Surefire's stdin.

Any plans for a fix?

(I should add I am using Mockito 3.0.4 on OpenJDK 11.0.4.)

@MarceloMariduena
Copy link

Changed the project's assigned JDK from 11 to 8 and it worked. Also made sure the jdk folder was not locked.

@lbar
Copy link

lbar commented Dec 5, 2019

Hi,

Any updates about this issue?

Thanks

@Tonyhasaheart
Copy link

I agree with MarceloMariduena. Changing the JDK works.

@ityreh
Copy link

ityreh commented Jan 26, 2020

I have the same issue and changing the JDK is not an option for me. Any updates?

@arocnies
Copy link

arocnies commented Jan 30, 2020

Looks like version 1.9.3's io.mockk.proxy.jvm.ClassLoadingStrategyChooser class is doing illegal reflection. Which is odd since mockk/mockk#278 was supposed to fix issue's with Java 12

WARNING: Illegal reflective access using Lookup on io.mockk.proxy.jvm.ClassLoadingStrategyChooser

@omnipitous
Copy link

Ooo.. we have a culprit.. hopefully this leads to a resolution?
PS to the above mentions about downgrading the JDK to 8.. yeah, no. That's useful debugging info not a solution. Need to be able to dev on modern versions of Java.. Staying on 8 forever is not an option. Only useful out of that is narrowing down the issue was aggravated by some JDK change between 8 and 9.

@tomhydra
Copy link

In my case, changing the dependency from mockito-all:2.0.2-beta to mockito-core:3.2.4 solved the problem

@samanthakem
Copy link

I am with @omnipitous

This issue happens to me when I use @SPY annotation.

@nbyrd
Copy link

nbyrd commented Mar 19, 2020

I am also experiencing this in JDK 13 with mockito-all version 1.10.19.

Discovered it while bootstrapping a new pet-project. Example test:

@Test
    void runWithoutNames() {
        // Setup
        final PrintStream mockPrinter = mock(PrintStream.class);
        final String[] names = new String[0];
        Main main = new Main(mockPrinter, names);

        // Execute
        main.run();

        // Assert
        verify(mockPrinter, times(1)).println("Hello World!");
    }

Output:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.creation.cglib.ClassImposterizer (file:/Users/nbyrd/.m2/repository/org/mockito/mockito-all/1.10.19/mockito-all-1.10.19.jar) to constructor java.io.PrintStream(boolean,java.io.OutputStream)
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.creation.cglib.ClassImposterizer
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

Process finished with exit code 0

@TimvdLippe
Copy link
Contributor

mockito-all is old. Please upgrade to a newer version of Mockito.

@EngJay
Copy link

EngJay commented Apr 8, 2020

I'm also experiencing this when using a spy with JDK 14 and mockito-core:3.2.4.

@joostheijkoop
Copy link

The issues seems to be still present in 3.3.3:

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:~/.m2/repository/org/mockito/mockito-core/3.3.3/mockito-core-3.3.3.jar) to field java.time.Clock$FixedClock.instant
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

@aurelien-minvielle
Copy link

aurelien-minvielle commented Jul 1, 2020

Issue still present in 3.3.3 with any JDK 9+.

It does happen everytime we use a spy(...).
Here's the stacktrace from --illegal-access=debug:

WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/Users/am/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.3.3/4878395d4e63173f3825e17e5e0690e8054445f1/mockito-core-3.3.3.jar) to field java.util.HashMap.tabl
        at org.mockito.internal.util.reflection.AccessibilityChanger.enableAccess(AccessibilityChanger.java:30)
        at org.mockito.internal.util.reflection.LenientCopyTool.copyValues(LenientCopyTool.java:40)
        at org.mockito.internal.util.reflection.LenientCopyTool.copy(LenientCopyTool.java:25)
        at org.mockito.internal.util.reflection.LenientCopyTool.copyToMock(LenientCopyTool.java:16)
        at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:39)
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:63)
        at org.mockito.Mockito.spy(Mockito.java:1992)

@aurelien-minvielle
Copy link

aurelien-minvielle commented Jul 2, 2020

This comes from a call to java.lang.reflect.AccessibleObject#isAccessible, which is deprecated in Java 9+.

The javadoc suggests to use #canAccess instead:

/**
     * Get the value of the {@code accessible} flag for this reflected object.
     *
     * @return the value of the object's {@code accessible} flag
     *
     * @deprecated
     * This method is deprecated because its name hints that it checks
     * if the reflected object is accessible when it actually indicates
     * if the checks for Java language access control are suppressed.
     * This method may return {@code false} on a reflected object that is
     * accessible to the caller. To test if this reflected object is accessible,
     * it should use {@link #canAccess(Object)}.
     *
     * @revised 9
     * @spec JPMS
     */
    @Deprecated(since="9")
    public boolean isAccessible() {
        return override;
    }

Unfortunately this method is only available from Java 9, which would make Mockito incompatible with Java 8.
Not sure if there's an easy workaround to silence the warning and keep Mockito 3.x Java 8 compatible.

@Dmeghana1
Copy link

Using OpenJDK11 and facing the same issue with mockito-core 3.2.4

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:build-cache/caches/modules-2/files-2.1/org.mockito/mockito-core/3.2.4/cb53dbffde7a79e81b0f79e4a3e5e74ed67afcda/mockito-core-3.2.4.jar) to field java.io.File.path
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
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

@carlspring
Copy link

I'm seeing the same issue under:

$ mvn --version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /java/apache/maven-3.6.3
Java version: 11.0.7, vendor: Oracle Corporation, runtime: /java/jdk-11.0.7
Default locale: en_GB, platform encoding: UTF-8
OS name: "linux", version: "5.4.0-42-generic", arch: "amd64", family: "unix"

Error message:

[ERROR] WARNING: An illegal reflective access operation has occurred
[ERROR] WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/home/carlspring/.m2/repository/org/mockito/mockito-core/3.4.6/mockito-core-3.4.6.jar) to field java.nio.channels.spi.AbstractInterruptibleChannel.closeLock
[ERROR] WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.AccessibilityChanger
[ERROR] WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
[ERROR] WARNING: All illegal access operations will be denied in a future release

Any updates on this?

@TimvdLippe
Copy link
Contributor

I think we fixed this issue in Mockito 3.5.0. Can you upgrade 3.5.5 and confirm it is indeed fixed?

@TimvdLippe
Copy link
Contributor

Oh and you will probably need the inline mockmaker (e.g. mockito-inline), as there is no non-inline version that is compatible with Java modules. (@raphw please correct me if that statement is wrong)

@aurelien-minvielle
Copy link

aurelien-minvielle commented Aug 24, 2020

I still get a warning with mockito 3.5.5, but the warning has changed.

Running the following test on JDK 14:

    @Test
    public void spyReportsIllegalReflectiveAccess() {
        Mockito.spy(new File("path"));
    }

With mockito-core:3.5.5 I get the following output:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.ReflectionMemberAccessor (file:/*********/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.5.5/e8121e886510d920f75af560a3765c6f673e789/mockito-core-3.5.5.jar) to field java.io.File.path
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.ReflectionMemberAccessor

And with mockito-inline:3.5.5 :

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access using Lookup on org.mockito.internal.util.reflection.InstrumentationMemberAccessor$Dispatcher$ByteBuddy$tKOT3Vep to class java.io.File
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.InstrumentationMemberAccessor$Dispatcher$ByteBuddy$tKOT3Vep

@raphw
Copy link
Member

raphw commented Aug 24, 2020

That's curious. I probably have overlooked something. I'll look into it.

@raphw
Copy link
Member

raphw commented Aug 24, 2020

Okay, the check for a module's openness does of course not work as expected when unnamed modules are involved. I fixed the check and this should hopefully be fixed in 3.5.6.

@raphw raphw closed this as completed Aug 24, 2020
@carlspring
Copy link

I think we fixed this issue in Mockito 3.5.0. Can you upgrade 3.5.5 and confirm it is indeed fixed?

Hi, @TimvdLippe ,

I tried it with:

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.5.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-inline</artifactId>
            <version>3.5.5</version>
        </dependency>

... But that didn't help. Any further advice? (I'm using JDK11).

carlspring added a commit to carlspring/s3fs-nio that referenced this issue Aug 24, 2020
* Added `mockito-inline` which will have to be upgraded (in #48) in the future, once mockito/mockito#1325 has been fixed.
@carlspring
Copy link

Thanks, 3.5.6 fixes it for us!

@MatMercer
Copy link

Just to give feedback for everyone:

using the 2 dependencies with Java 11 and mockito 3.5.15

mockito-core
mockito-inline

Successfully removes the warning. If I don't add the mockito-inline dependency, the warning continues.

@raphw
Copy link
Member

raphw commented Nov 10, 2020

Great, thanks for the feedback. I tested it pretty thoroughly this time and even when prohibiting access, Mockito seems to work without any issues on the inline mock maker. There's one edge cases when mocking a generated class without any constructor but that I don't believe many will run into.

@jianglai
Copy link

I am still seeing this error with 3.7.7:

WARNING: An illegal reflective access operation has occurred                                                                 
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.ReflectionMemberAccessor (file:/usr/local/google/h
ome/jianglai/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.7.7/5d43943be9b57c8ff47d2b9640ea0a7fc7211cf2/mocki
to-core-3.7.7.jar) to field java.io.ByteArrayInputStream.buf                                                                 
WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.ReflectionMemberAccessor  
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 

@raphw
Copy link
Member

raphw commented Feb 10, 2021

Are you using mockito-inline?

@jianglai
Copy link

Are you using mockito-inline?

We are using:

'org.mockito:mockito-core:3.7.7',                                         
'org.mockito:mockito-junit-jupiter:3.7.7', 

@raphw
Copy link
Member

raphw commented Feb 10, 2021

There's nothing we can do with the standard mock maker. You'd need to use the inline-mock-maker for that.

@jianglai
Copy link

Thanks! After switching to the inline mock maker I'm getting this warning instead:

OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

Is it WAI?

@kdebski85
Copy link

kdebski85 commented May 13, 2022

I still have the issue:

[ERROR] WARNING: An illegal reflective access operation has occurred
[ERROR] WARNING: Illegal reflective access by org.mockito.internal.util.reflection.ReflectionMemberAccessor (file:/var/lib/jenkins/.m2/repository/org/mockito/mockito-core/4.0.0/mockito-core-4.0.0.jar) to field javax.xml.stream.util.StreamReaderDelegate.reader
[ERROR] WARNING: Please consider reporting this to the maintainers of org.mockito.internal.util.reflection.ReflectionMemberAccessor
[ERROR] WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
[ERROR] WARNING: All illegal access operations will be denied in a future release

I get version 4.0.0 from Spring Boot dependencies - https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies/2.6.7
Why Spring Boot uses older version of Mockito?
I checked that they upgraded to 4.5.1 in 2.7.0, but it has not been released yet - spring-projects/spring-boot#30936

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests