Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.language.postfixOps

val currentScalaVersion = "2.13.16"
val currentScalaVersion = "2.13.17"

inThisBuild(
Seq(
Expand Down
22 changes: 11 additions & 11 deletions common/src/main/scala/org/mockito/ReflectionUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import org.scalactic.TripleEquals.*

import java.lang.reflect.Method
import scala.reflect.ClassTag
import scala.reflect.internal.Symbols
import scala.util.Try as uTry

object ReflectionUtils {
import scala.reflect.runtime.universe as ru
import ru.*

implicit def symbolToMethodSymbol(sym: Symbol): Symbols#MethodSymbol = sym.asInstanceOf[Symbols#MethodSymbol]

private val mirror = runtimeMirror(getClass.getClassLoader)
private val customMirror = mirror.asInstanceOf[{
def methodToJava(sym: Symbols#MethodSymbol): Method
}]
private val mirror = runtimeMirror(getClass.getClassLoader)
private val methodToJavaMethod: Method =
mirror.getClass.getMethods.find(m => m.getName == "methodToJava" && m.getParameterCount == 1 && m.getParameterTypes.head.getName.endsWith("Symbol")).get
private def asJavaMethod(sym: Symbol): Method = methodToJavaMethod.invoke(mirror, sym).asInstanceOf[Method]

private[mockito] def returnType(invocation: InvocationOnMock): Class[?] = {
val javaReturnType = invocation.method.getReturnType
Expand All @@ -31,12 +28,15 @@ object ReflectionUtils {
}

private[mockito] def returnsValueClass(invocation: InvocationOnMock): Boolean =
findTypeSymbol(invocation).exists(_.returnType.typeSymbol.isDerivedValueClass)
findTypeSymbol(invocation).exists { s =>
val typeSymbol = s.asMethod.returnType.typeSymbol
typeSymbol.isClass && typeSymbol.asClass.toType <:< typeOf[AnyVal]
}

private def resolveWithScalaGenerics(invocation: InvocationOnMock): Option[Class[?]] =
uTry {
findTypeSymbol(invocation)
.filter(_.returnType.typeSymbol.isClass)
.filter(_.asMethod.returnType.typeSymbol.isClass)
.map(_.asMethod.returnType.typeSymbol.asClass)
.map(mirror.runtimeClass)
}.toOption.flatten
Expand All @@ -48,7 +48,7 @@ object ReflectionUtils {
.info
.decls
.collectFirst {
case symbol if isNonConstructorMethod(symbol) && customMirror.methodToJava(symbol) === invocation.method => symbol
case symbol if isNonConstructorMethod(symbol) && asJavaMethod(symbol) === invocation.method => symbol
}
}.toOption.flatten

Expand Down Expand Up @@ -82,7 +82,7 @@ object ReflectionUtils {
}.toSet
}
.collect {
case (symbol, indices) if indices.nonEmpty => customMirror.methodToJava(symbol) -> indices
case (symbol, indices) if indices.nonEmpty => asJavaMethod(symbol) -> indices
}
.toSeq
}.toOption
Expand Down