From 81e1d1118bbf23b7674fca52aafe5dcbf3f84a88 Mon Sep 17 00:00:00 2001 From: Marcel Philipiak Date: Thu, 31 Oct 2024 16:08:20 +0100 Subject: [PATCH] extract isAssignable --- .../api/typed/CanBeSubclassDeterminer.scala | 18 ++++++++++++------ .../api/typed/TypeConversionHandler.scala | 3 ++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/CanBeSubclassDeterminer.scala b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/CanBeSubclassDeterminer.scala index 4d447a75bce..4eedb8b7e4d 100644 --- a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/CanBeSubclassDeterminer.scala +++ b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/CanBeSubclassDeterminer.scala @@ -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._ @@ -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) @@ -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 diff --git a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/TypeConversionHandler.scala b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/TypeConversionHandler.scala index 9b9aa162b68..89942a5942d 100644 --- a/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/TypeConversionHandler.scala +++ b/components-api/src/main/scala/pl/touk/nussknacker/engine/api/typed/TypeConversionHandler.scala @@ -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 }