Skip to content

Commit

Permalink
review fixes 13
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Bigorajski committed Sep 29, 2024
1 parent 3fae4d5 commit 6984283
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
* [#6925](https://github.com/TouK/nussknacker/pull/6925) Fix situation when preset labels were presented as `null` when node didn't pass the validation.
* [#6935](https://github.com/TouK/nussknacker/pull/6935) Spel: Scenario labels added to meta variable - `#meta.scenarioLabels`
* [#6952](https://github.com/TouK/nussknacker/pull/6952) Improvement: TypeInformation support for scala.Option
* [#6840](https://github.com/TouK/nussknacker/pull/6840) Introduce castTo and canCastTo extension methods in SpeL.
* [#6840](https://github.com/TouK/nussknacker/pull/6840) Introduce canCastTo, castTo and castToOrNull extension methods in SpeL.

## 1.17

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.lang.Nullable;
import pl.touk.nussknacker.engine.extension.Cast;
import pl.touk.nussknacker.engine.extension.ExtensionMethods;
import scala.PartialFunction;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -42,17 +43,16 @@ public Object invoke(Method method,
Object target,
Object[] arguments,
ClassLoader classLoader) throws IllegalAccessException, InvocationTargetException {
if (target != null && target.getClass().isArray() && method.getDeclaringClass().isAssignableFrom(List.class)) {
Class<?> methodDeclaringClass = method.getDeclaringClass();
if (target != null && target.getClass().isArray() && methodDeclaringClass.isAssignableFrom(List.class)) {
return method.invoke(RuntimeConversionHandler.convert(target), arguments);
}
PartialFunction<Class<?>, Object> extMethod =
ExtensionMethods.invoke(method, target, arguments, classLoader);
if (extMethod.isDefinedAt(methodDeclaringClass)) {
return extMethod.apply(methodDeclaringClass);
} else {
return ExtensionMethods.invoke(method, target, arguments, classLoader)
.applyOrElse(method.getDeclaringClass(), (ignored) -> {
try {
return method.invoke(target, arguments);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
});
return method.invoke(target, arguments);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CastImpl(target: Any, classLoader: ClassLoader) extends Cast {
}
}

override def castToOrNull[T >: Null](className: String): T = Try { castTo(className) }.getOrElse(null)
override def castToOrNull[T >: Null](className: String): T = Try { castTo[T](className) }.getOrElse(null)
}

private[extension] object CastImpl extends ExtensionMethodsImplFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,22 @@ class SpelExpressionSpec extends AnyFunSuite with Matchers with ValidatedValuesD
}
}

test("should return null if castToOrNull fails") {
parse[Any](
expr = "#unknownString.value.castToOrNull('java.lang.Integer')",
context = ctx,
methodExecutionForUnknownAllowed = true
).validExpression.evaluateSync[Any](ctx) == null shouldBe true
}

test("should castToOrNull succeed") {
parse[Any](
expr = "#unknownString.value.castToOrNull('java.lang.String')",
context = ctx,
methodExecutionForUnknownAllowed = true
).validExpression.evaluateSync[Any](ctx) shouldBe "unknown"
}

}

case class SampleObject(list: java.util.List[SampleValue])
Expand Down

0 comments on commit 6984283

Please sign in to comment.