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

Spying on ArrayList generates Illegal reflective access warning with Java 11 #1782

Closed
szpak opened this issue Sep 24, 2019 · 3 comments
Closed
Labels

Comments

@szpak
Copy link
Member

szpak commented Sep 24, 2019

Spying on ArrayList generates Illegal reflective access warning with Java 11 (12):

WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/home/foobar/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.0.8/.../mockito-core-3.0.8.jar)
to field java.util.ArrayList.elementData
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/home/foobar/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.0.8/.../mockito-core-3.0.8.jar)
 to field java.util.ArrayList.size
WARNING: Illegal reflective access by org.mockito.internal.util.reflection.AccessibilityChanger (file:/home/foobar/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/3.0.8/.../mockito-core-3.0.8.jar)
 to field java.util.AbstractList.modCount

Sample code to reproduce the situation:

    @Test
    public void shouldNotCallOriginalMethod() {
        //given
        List<Integer> spiedList = spy(new ArrayList<>());
        willReturn(3).given(spiedList).get(0);
        //when
        Integer readValue = spiedList.get(0);
        //then
        assertThat(readValue).isEqualTo(3);
    }

Occurs with (at least):

  • Mockito 3.0.0 and 3.0.8
  • ByteBuddy 1.9.10 and 1.10.1
  • Java 11 and 12 (modern versions)

It's just a warning right now, but it could be disallowed one day, so maybe there is some other way to do that with ArrayList (and probably also some other classes from JDK).

@szpak szpak added the java-11 label Sep 24, 2019
@TimvdLippe
Copy link
Contributor

I reckon this is unrelated to ArrayList in particular, but more that https://github.com/openjdk/jdk/blob/a36b2af52b54f3c9b276555671971ae6ceca772b/src/java.base/share/classes/java/util/ArrayList.java#L138 and https://github.com/openjdk/jdk/blob/a36b2af52b54f3c9b276555671971ae6ceca772b/src/java.base/share/classes/java/util/AbstractList.java#L628 are supposed to be private. It also likely related to modules.

Could you construct a minimal example that uses modules and has a private field that would run into the same scenario? That would ease debugging.

@raphw
Copy link
Member

raphw commented Sep 27, 2019

This is very expected since we rely on accessing all fields with reflection.

There is not really a good way around it for us, we need to access these fields. We could attempt to generated classes and inject them but this can only be done legally if the module and packages are open which then would also allow reflection.

@TimvdLippe
Copy link
Contributor

Closing per the above comment. We recommend using mockito-inline on more recent JDKs to avoid the illegal reflection access.

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

Successfully merging a pull request may close this issue.

3 participants