Discussion:
[8.0] TextField<Integer> with LambdaModel forced to use String
smallufo
2018-06-16 07:29:37 UTC
Permalink
After upgrading to 8.0 , I tried the exciting LambdaModel , try to replace
the non-TypeSafety PropertyModel
But I found it cannot handle type intelligently.
For a TextField<Integer> , for example :

form.add(new TextField<Integer>("year", LambdaModel.of(obj::getYear,
obj::setYear )));

At runtime , it reports Cannot cast from String to Integer.

I have to rewrite getter / setter to String type (and do conversion in
setter ) , but the backing field is Integer type. (And change
TextField<Integer> to TextField<String> , or <> )

This is pity ...
Did I miss anything ?

Thanks.
Andrew Geery
2018-06-16 09:31:54 UTC
Permalink
You have to use the TextField constructor that specifies the type of the
model:
https://ci.apache.org/projects/wicket/apidocs/8.x/org/apache/wicket/markup/html/form/TextField.html#TextField-java.lang.String-org.apache.wicket.model.IModel-java.lang.Class-

It should be:

add(new TextField<>("year", LambdaModel.of(obj::getYear, obj::setYear),
Integer.class));

That one always gets me too...

Thanks
Andrew
Post by smallufo
After upgrading to 8.0 , I tried the exciting LambdaModel , try to replace
the non-TypeSafety PropertyModel
But I found it cannot handle type intelligently.
form.add(new TextField<Integer>("year", LambdaModel.of(obj::getYear,
obj::setYear )));
At runtime , it reports Cannot cast from String to Integer.
I have to rewrite getter / setter to String type (and do conversion in
setter ) , but the backing field is Integer type. (And change
TextField<Integer> to TextField<String> , or <> )
This is pity ...
Did I miss anything ?
Thanks.
smallufo
2018-06-16 10:31:40 UTC
Permalink
Thanks .
It works (but seems a little duplication)
Post by Andrew Geery
You have to use the TextField constructor that specifies the type of the
https://ci.apache.org/projects/wicket/apidocs/8.x/org/apache/wicket/markup/html/form/TextField.html#TextField-java.lang.String-org.apache.wicket.model.IModel-java.lang.Class-
add(new TextField<>("year", LambdaModel.of(obj::getYear, obj::setYear),
Integer.class));
That one always gets me too...
Thanks
Andrew
Post by smallufo
After upgrading to 8.0 , I tried the exciting LambdaModel , try to
replace
Post by smallufo
the non-TypeSafety PropertyModel
But I found it cannot handle type intelligently.
form.add(new TextField<Integer>("year", LambdaModel.of(obj::getYear,
obj::setYear )));
At runtime , it reports Cannot cast from String to Integer.
I have to rewrite getter / setter to String type (and do conversion in
setter ) , but the backing field is Integer type. (And change
TextField<Integer> to TextField<String> , or <> )
This is pity ...
Did I miss anything ?
Thanks.
Sven Meier
2018-06-16 14:15:41 UTC
Permalink
Hi,

regretfully Java lambdas don't provide type information via reflection.

Have fun
Sven
Post by smallufo
Thanks .
It works (but seems a little duplication)
Post by Andrew Geery
You have to use the TextField constructor that specifies the type of
the
https://ci.apache.org/projects/wicket/apidocs/8.x/org/apache/wicket/markup/html/form/TextField.html#TextField-java.lang.String-org.apache.wicket.model.IModel-java.lang.Class-
Post by Andrew Geery
add(new TextField<>("year", LambdaModel.of(obj::getYear,
obj::setYear),
Post by Andrew Geery
Integer.class));
That one always gets me too...
Thanks
Andrew
Post by smallufo
After upgrading to 8.0 , I tried the exciting LambdaModel , try to
replace
Post by smallufo
the non-TypeSafety PropertyModel
But I found it cannot handle type intelligently.
form.add(new TextField<Integer>("year",
LambdaModel.of(obj::getYear,
Post by Andrew Geery
Post by smallufo
obj::setYear )));
At runtime , it reports Cannot cast from String to Integer.
I have to rewrite getter / setter to String type (and do conversion
in
Post by Andrew Geery
Post by smallufo
setter ) , but the backing field is Integer type. (And change
TextField<Integer> to TextField<String> , or <> )
This is pity ...
Did I miss anything ?
Thanks.
Loading...