Repository: mockito/mockito
Issue: Mockito 3.11.2 (?) fails with "can't load this type of class file"

**Broken version**: 3.11.2?
**Working versions:** 3.10.0

**Description**

All was working OK with 3.8.0 and we upgraded to 3.11.2 but this presented the problem below. I have sequentially downgraded and it seems 3.10.0 doesn't present me with any problems.

I have not been able to confidently isolate it to this version because I have also experienced #2316 and #2327 in the interim versions.

Our context is Android and a `com.android.test` instrumented test module. We are using `org.mockito:mockito-core`, plus `org.mockito:mockito-android` with the same version, and `org.mockito.kotlin:mockito-kotlin:3.2.0`.

```
2021-06-22 21:35:57.029 9913-9913/ourapp.package.debug E/MonitoringInstr: Exception encountered by: ourapp.package.testing.integrations.TestApplication@b4bbfaf. Dumping thread state to outputs and pining for the fjords.
    org.mockito.exceptions.base.MockitoException: 
    Failed to release mocks
    
    This should not happen unless you are using a third-part mock maker
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428)
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446)
        at android.app.ActivityThread.access$1500(ActivityThread.java:301)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:246)
        at android.app.ActivityThread.main(ActivityThread.java:8512)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
     Caused by: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:84)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
        at $Proxy3.isTypeMockable(Unknown Source)
        at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:33)
        at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
        at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:250)
        at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:232)
        at org.mockito.internal.MockitoCore.mock(MockitoCore.java:83)
        at org.mockito.Mockito.mock(Mockito.java:1954)
        at org.mockito.internal.configuration.MockAnnotationProcessor.processAnnotationForMock(MockAnnotationProcessor.java:66)
        at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:27)
        at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:24)
        at org.mockito.internal.configuration.IndependentAnnotationEngine.createMockFor(IndependentAnnotationEngine.java:45)
        at org.mockito.internal.configuration.IndependentAnnotationEngine.process(IndependentAnnotationEngine.java:73)
        at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:74)
        at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:48)
        at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:82)
        at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:100)
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44) 
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192) 
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428) 
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8512) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration@67d84a2
        at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:57)
        at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:65)
        at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:50)
        at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:26)
        at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:20)
        at org.mockito.internal.configuration.plugins.Plugins.getAnnotationEngine(Plugins.java:67)
        at org.mockito.internal.configuration.GlobalConfiguration.tryGetPluginAnnotationEngine(GlobalConfiguration.java:57)
        at org.mockito.MockitoAnnotations.openMocks(MockitoAnnotations.java:81)
        at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:100) 
        at ourapp.package.testing.integrations.TestApplication.onCreate(OurAppTestRunner.kt:44) 
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192) 
        at androidx.test.runner.MonitoringInstrumentation.callApplicationOnCreate(MonitoringInstrumentation.java:428) 
        at android.app.ActivityThread.handleMakeApplication(ActivityThread.java:7507) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7446) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:301) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2148) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:246) 
        at android.app.ActivityThread.main(ActivityThread.java:8512) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 
     Caused by: java.lang.reflect.InvocationTargetException
2021-06-22 21:35:57.029 9913-9913/ourapp.package.debug E/MonitoringInstr:     at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:52)
        	... 21 more
     Caused by: java.lang.ExceptionInInitializerError
        at net.bytebuddy.utility.dispatcher.JavaDispatcher.of(JavaDispatcher.java:104)
        at net.bytebuddy.description.type.TypeDescription$ForLoadedType.<clinit>(TypeDescription.java:8282)
        at net.bytebuddy.description.type.TypeDescription$ForLoadedType.of(TypeDescription.java:8355)
        at net.bytebuddy.implementation.bind.annotation.Super$Binder.<clinit>(Super.java:222)
        at net.bytebuddy.implementation.bind.annotation.TargetMethodAnnotationDrivenBinder$ParameterBinder.<clinit>(TargetMethodAnnotationDrivenBinder.java:166)
        at net.bytebuddy.implementation.MethodDelegation.withDefaultConfiguration(MethodDelegation.java:601)
        at net.bytebuddy.implementation.MethodDelegation.to(MethodDelegation.java:271)
        at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.<init>(SubclassBytecodeGenerator.java:64)
        at org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.<init>(SubclassBytecodeGenerator.java:74)
        at org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker.<init>(SubclassByteBuddyMockMaker.java:37)
        at org.mockito.android.internal.creation.AndroidByteBuddyMockMaker.<init>(AndroidByteBuddyMockMaker.java:22)
        	... 24 more
     Caused by: java.lang.IllegalStateException: Failed to create invoker for net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader.invoker(JavaDispatcher.java:1270)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker$CreationAction.run(JavaDispatcher.java:431)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$Invoker$CreationAction.run(JavaDispatcher.java:420)
        at java.security.AccessController.doPrivileged(AccessController.java:43)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher.<clinit>(JavaDispatcher.java:65)
        	... 35 more
     Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
        at java.lang.ClassLoader.defineClass(ClassLoader.java:591)
        at net.bytebuddy.utility.dispatcher.JavaDispatcher$DynamicClassLoader.invoker(JavaDispatcher.java:1266)
```

I appreciate the above may not be enormously helpful, and I'm happy to try and provide more information on request.

Mocked subjects by this runner are:
* an `open class`
* a sealed class
* various public interfaces

Issue comments:
Some more outfall in the Android ecosystem @raphw 😭 
@robpridham-bbc I understand that you were not the initial reporters that warranted the 3.11.1 and 3.11.2 releases, but would you maybe be able to contribute regression tests for these setups? We have several subprojects already (https://github.com/mockito/mockito/tree/main/subprojects) and I think it would be worthwhile to have an `androidTest` project. Then we can reproduce these kind of failures and ensure we don't break it again in future releases.

If you are not able to contribute these, that's fine of course. Sadly we are no Android experts, but are trying to do our best to support the ecosystem.
> @robpridham-bbc I understand that you were not the initial reporters that warranted the 3.11.1 and 3.11.2 releases, but would you maybe be able to contribute regression tests for these setups? We have several subprojects already (https://github.com/mockito/mockito/tree/main/subprojects) and I think it would be worthwhile to have an `androidTest` project. Then we can reproduce these kind of failures and ensure we don't break it again in future releases.
> 
> If you are not able to contribute these, that's fine of course. Sadly we are no Android experts, but are trying to do our best to support the ecosystem.

I'm a professional Android specialist, and I'd be happy to contribute as time permits (probably not _imminently_) - my main concern would be how much Android baggage this would bring to the project, i.e. I think the minimum project to replicate this fault might be fairly complex. I'll have a think about this. If you fancy a chat about it, I can find time for a Zoom call or whatever else in UK business hours.
I think if we start with a regression test for #2316 we can more easily investigate what it would require. As I understand #2316 even a very basic setup would have caught that issue. For that, I am assuming we need the Android SDK to download, but other than that we should be good?

Given the wide usage of Mockito in the Android ecosystem, I think adding this as a dependency is a justifiable move. Especially since we already include numerous other ecosystem dependencies, such as Kotlin, Groovy and JUnit Jupiter.
Arghs, indeed. Fixed on master of Byte Buddy. Needed a fallback for the reflection dispatcher if dynamic class generation is not supported. Byte Buddy started to create an intermediate dispatcher in a separate class loader to avoid reflective calls to stem from its own context, otherwise method handle lookups might leak, for instance. For Android, this does of course break things.

Will be fixed in the next version.
> I think if we start with a regression test for #2316 we can more easily investigate what it would require. As I understand #2316 even a very basic setup would have caught that issue. For that, I am assuming we need the Android SDK to download, but other than that we should be good?
> 
> Given the wide usage of Mockito in the Android ecosystem, I think adding this as a dependency is a justifiable move. Especially since we already include numerous other ecosystem dependencies, such as Kotlin, Groovy and JUnit Jupiter.

Perhaps open a new issue for establishing some tests and we can go from there. Regression tests themselves are definitely feasible but it depends a little what you want - the easiest option for the Android-uninitiated is to get hold of Android Studio (a version of IntelliJ) which can self-manage the SDK and a lot of the underlying components, but obviously that's only really suited to manually executed tests. If you're looking to run a test suite on a CI server, for example, it's a bit different. Either way, as well as the tests, it probably wants some maintenance documentation so contributors can keep it current. Let's figure this out.

Pull request review comments:

Patch that resolved the issue:
diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle
index eb894e3864..d536ad0bf7 100644
--- a/gradle/dependencies.gradle
+++ b/gradle/dependencies.gradle
@@ -4,7 +4,7 @@ ext {
 
 def versions = [:]
 
-versions.bytebuddy = '1.11.5'
+versions.bytebuddy = '1.11.6'
 versions.junitJupiter = '5.7.2'
 versions.errorprone = '2.7.1'
 


Test patch: