wicketwicket-8

Wicket LambdaModel and NumberTextField cannot resolveType


After switching to LambdaModel as an alternative to PropertyModel in one of my forms in my Wicket 8 application I have failing submit tests stating:

java.lang.ClassCastException: java.lang.String cannot be cast to java.math.BigDecimal

My Form Panel has a NumberTextField which in my working scenario is bound to a PropertyModel.

THIS WORKS

form.add(new NumberTextField<BigDecimal>("myBigDecimalField", new PropertyModel<>(getModel(), "myBigDecimalField")));

THIS DOES NOT WORK

form.add(new NumberTextField<BigDecimal>("myBigDecimalField", LambdaModel.of(getModel(), MyClass::getMyBigDecimalField, MyClass::setMyBigDecimalField)));

The problem is that LambdaModel does not implement IObjectClassAwareModel as PropertyModel does and so when the NumberTextField tries to resolve the type in AbstractTextComponent#152 and subsequentially checks in getModelType if the model is an instance of IObjectClassAwareModel it will not work, as LambdaModel does not implement this interface.

Is this intended that LamdaModel does not implmement IObjectClassAwareModel. BTW I know that I can fix this issue with explicitly declaring the type class of the NumberTextField.


Solution

  • Regretfully it's quite hard to retrieve any type information from lambdas.

    See here for an explanation:

    Java: how to resolve generic type of lambda parameter?

    So for now it's recommended to pass the type to the component.