Skip to content

Commit

Permalink
extract isAssignable
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Philipiak committed Oct 31, 2024
1 parent e5968f5 commit 81e1d11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cats.data.Validated._
import cats.data.{NonEmptyList, ValidatedNel}
import cats.implicits.{catsSyntaxValidatedId, _}
import org.apache.commons.lang3.ClassUtils
import pl.touk.nussknacker.engine.api.typed.CanBeSubclassDeterminer.isAssignable
import pl.touk.nussknacker.engine.api.typed.supertype.NumberTypesPromotionStrategy.AllNumbers
import pl.touk.nussknacker.engine.api.typed.typing._

Expand Down Expand Up @@ -136,7 +137,11 @@ trait CanBeSubclassDeterminer {
(),
f"${givenClass.display} and ${superclassCandidate.display} are not the same"
) orElse
isAssignable(givenClass.klass, superclassCandidate.klass)
condNel(
isAssignable(givenClass.klass, superclassCandidate.klass),
(),
s"${givenClass.klass} is not assignable from ${superclassCandidate.klass}"
)

val canBeSubclass = equalClassesOrCanAssign andThen (_ => typeParametersMatches(givenClass, superclassCandidate))
canBeSubclass orElse canBeConvertedTo(givenType, superclassCandidate)
Expand Down Expand Up @@ -207,18 +212,19 @@ trait CanBeSubclassDeterminer {
condNel(TypeConversionHandler.canBeConvertedTo(givenType, superclassCandidate), (), errMsgPrefix)
}

}

object CanBeSubclassDeterminer extends CanBeSubclassDeterminer {

// we use explicit autoboxing = true flag, as ClassUtils in commons-lang3:3.3 (used in Flink) cannot handle JDK 11...
private def isAssignable(from: Class[_], to: Class[_]): ValidatedNel[String, Unit] = {
val isAssignable = (from, to) match {
def isAssignable(from: Class[_], to: Class[_]): Boolean = {
(from, to) match {
case (f, t) if ClassUtils.isAssignable(f, t, true) => true
// Number double check by hand because lang3 can incorrectly throw false when dealing with java types
case (f, t) if AllNumbers.contains(f) && AllNumbers.contains(t) =>
AllNumbers.indexOf(f) >= AllNumbers.indexOf(t)
case _ => false
}
condNel(isAssignable, (), s"$to is not assignable from $from")
}

}

object CanBeSubclassDeterminer extends CanBeSubclassDeterminer
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ object TypeConversionHandler {
ClassUtils.isAssignable(boxedGivenClass, classOf[Number], true)

case candidate if isDecimalNumber(candidate) =>
ConversionFromClassesForDecimals.filter(ClassUtils.isAssignable(boxedGivenClass, _, true)).contains(candidate)
import CanBeSubclassDeterminer.isAssignable
isAssignable(boxedGivenClass, candidate)

case _ => false
}
Expand Down

0 comments on commit 81e1d11

Please sign in to comment.