IndexOutOfBoundsException when passing method reference as argument
CollinAlpert opened this issue · 4 comments
Consider this method:
private SqlFunction<Person, Integer> getFunction(Person p) {
if (p.getHeight() > 150) {
return Person::getHeight;
}
return Person::getAge;
}
A SqlFunction
is just a java.util.function.Function
that implements Serializable
.
Now, when I try to parse the function returned by the method like this: LambdaExpression.parse((SqlFunction<IPerson, ?>) this::getFunction);
, I get the exception java.lang.IndexOutOfBoundsException: Index -1 out-of-bounds for length 0
. However, if I do not pass a method reference, but a normal lambda like this: LambdaExpression.parse((SqlFunction<IPerson, ?>) p -> getFunction(p));
, there is no exception and everything works.
In version 2.4.3, the exception is a bit different. The stack trace now is:
java.lang.IllegalArgumentException: 0
at com.trigersoft.jaque.expression.InvocationExpression.<init>(InvocationExpression.java:51)
at com.trigersoft.jaque.expression.Expression.invoke(Expression.java:934)
at com.trigersoft.jaque.expression.ExpressionClassCracker.lambda(ExpressionClassCracker.java:273)
at com.trigersoft.jaque.expression.ExpressionClassCracker.lambda(ExpressionClassCracker.java:167)
at com.trigersoft.jaque.expression.LambdaExpression.parse(LambdaExpression.java:79)
The problem here is that the method returns lambda and this case is not correctly handled.
Very nice, thanks a lot!