From abb5994f17a5ef732a50f0a51f310e23b0fcb623 Mon Sep 17 00:00:00 2001 From: Alberto Lago Date: Tue, 2 Feb 2021 09:56:38 +0100 Subject: [PATCH 1/2] getField for java 12+ --- .../main/scala/org/mockito/MockitoAPI.scala | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/common/src/main/scala/org/mockito/MockitoAPI.scala b/common/src/main/scala/org/mockito/MockitoAPI.scala index 85f97963..98fa6d15 100644 --- a/common/src/main/scala/org/mockito/MockitoAPI.scala +++ b/common/src/main/scala/org/mockito/MockitoAPI.scala @@ -28,9 +28,12 @@ import org.mockito.stubbing._ import org.mockito.verification.{ VerificationAfterDelay, VerificationMode, VerificationWithTimeout } import org.scalactic.{ Equality, Prettifier } +import java.lang.reflect.Field import scala.collection.JavaConverters._ import scala.reflect.ClassTag import scala.reflect.runtime.universe.WeakTypeTag +import scala.util.{ Failure, Success, Try } +import scala.util.control.Breaks.break private[mockito] trait ScalacticSerialisableHack { //Hack until Equality can be made serialisable @@ -630,7 +633,25 @@ private[mockito] trait MockitoEnhancer extends MockCreator { def withObjectMocked[O <: AnyRef: ClassTag](block: => Any)(implicit defaultAnswer: DefaultAnswer, $pt: Prettifier): Unit = { val objectClass = clazz[O] objectClass.synchronized { - val moduleField = objectClass.getDeclaredField("MODULE$") + val moduleField: Field = Try(objectClass.getDeclaredField("MODULE$")) match { + case Success(module) => module + case Failure(e) => + Try { + val getDeclaredFields0 = objectClass.getDeclaredMethod("getDeclaredFields0", classOf[Boolean]) + val accessibleBeforeSet: Boolean = getDeclaredFields0.isAccessible + getDeclaredFields0.setAccessible(true) + @SuppressWarnings("unchecked") + val declaredFields = getDeclaredFields0.invoke(classOf[Field], false).asInstanceOf[Array[Field]] + getDeclaredFields0.setAccessible(accessibleBeforeSet) + declaredFields.find("MODULE$" == _.getName).get + } match { + case Success(module) => module + case Failure(ex) => + e.addSuppressed(ex) + throw e + } + } + val realImpl: O = moduleField.get(null).asInstanceOf[O] val threadAwareMock = createMock( From 43ee7b846b818eaaab324f9d79db947b14844893 Mon Sep 17 00:00:00 2001 From: Alberto Lago Date: Tue, 2 Feb 2021 10:29:04 +0100 Subject: [PATCH 2/2] Fix for java 13+, avoid type conversion error --- common/src/main/scala/org/mockito/MockitoAPI.scala | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/common/src/main/scala/org/mockito/MockitoAPI.scala b/common/src/main/scala/org/mockito/MockitoAPI.scala index 98fa6d15..be0d50ae 100644 --- a/common/src/main/scala/org/mockito/MockitoAPI.scala +++ b/common/src/main/scala/org/mockito/MockitoAPI.scala @@ -640,8 +640,7 @@ private[mockito] trait MockitoEnhancer extends MockCreator { val getDeclaredFields0 = objectClass.getDeclaredMethod("getDeclaredFields0", classOf[Boolean]) val accessibleBeforeSet: Boolean = getDeclaredFields0.isAccessible getDeclaredFields0.setAccessible(true) - @SuppressWarnings("unchecked") - val declaredFields = getDeclaredFields0.invoke(classOf[Field], false).asInstanceOf[Array[Field]] + val declaredFields: Array[Field] = getDeclaredFields0.invoke(classOf[Field], () => false).asInstanceOf[Array[Field]] getDeclaredFields0.setAccessible(accessibleBeforeSet) declaredFields.find("MODULE$" == _.getName).get } match {