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

#350 get Modifiers for java 13+ #354

Merged
merged 2 commits into from
Feb 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion common/src/main/scala/org/mockito/MockitoAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -630,7 +633,24 @@ 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)
val declaredFields: Array[Field] = 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(
Expand Down