Skip to content

Commit

Permalink
A single call to JavaTypeUtils.getTypeArguments is required to get ty…
Browse files Browse the repository at this point in the history
…pe variables from the class inheritance hierarchy
  • Loading branch information
gbloisi committed Sep 4, 2023
1 parent 5db30e1 commit bb1b17d
Showing 1 changed file with 3 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import java.lang.reflect.{ParameterizedType, Type, TypeVariable}
import java.util.{List => JList, Map => JMap}
import javax.annotation.Nonnull

import scala.annotation.tailrec
import scala.collection.JavaConverters._
import scala.reflect.ClassTag

Expand Down Expand Up @@ -131,7 +130,9 @@ object JavaTypeInference {
// TODO: we should only collect properties that have getter and setter. However, some tests
// pass in scala case class as java bean class which doesn't have getter and setter.
val properties = getJavaBeanReadableProperties(c)
val classTV = getClassHierarchyTypeArguments(c, typeVariables)
// add type variables from inheritance hierarchy of the class
val classTV = JavaTypeUtils.getTypeArguments(c, classOf[Object]).asScala.toMap ++
typeVariables
// Note that the fields are ordered by name.
val fields = properties.map { property =>
val readMethod = property.getReadMethod
Expand All @@ -158,17 +159,4 @@ object JavaTypeInference {
.filterNot(_.getName == "declaringClass")
.filter(_.getReadMethod != null)
}

@tailrec
def getClassHierarchyTypeArguments(cls: Class[_],
typeVariables: Map[TypeVariable[_], Type])
: Map[TypeVariable[_], Type] = {
if (cls == null) {
return typeVariables;
}

getClassHierarchyTypeArguments(cls.getSuperclass, typeVariables ++
JavaTypeUtils.getTypeArguments(cls, classOf[Object]).asScala.toMap
)
}
}

0 comments on commit bb1b17d

Please sign in to comment.