You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Rob Audenaerde <ro...@gmail.com> on 2017/10/04 14:57:59 UTC
NumberTextField and LambdaModel, typing goes wrong?
We are preparing for migration to Wicket 8 (when it comes out). So we are
already replacing our old PropertyModels with a backported LambdaModel (we
use the implementation from Wicket 8).
I noticed an odd behavior where the RangeValidator gets an empty String
from the input (in validatable.getValue()) if the input is not an integer
(for example, changing '2' to '2e' in the textfield).
This results in a class cast exception.
If I replace the Lambdamodel by a IModel<Integer> = new Model<Integer>(2)
it works fine.
After adding the type Integer.class to the NumberTextField it works fine
again.
Am I doing something wrong?
Thanks!
Here is the relevant code (see commented section):
------
public class HomePage extends WebPage {
private static final long serialVersionUID = 1L;
static class MyInt implements Serializable
{
Integer i=2;
public Integer getI() {
return i;
}
public void setI(Integer i) {
this.i = i;
}
}
public HomePage(final PageParameters parameters) {
super(parameters);
IModel<MyInt> myInt = new Model(new MyInt());
IModel<Integer> intModel = LambdaModel.of(myInt, MyInt::getI, MyInt::setI);
Form f = new Form("f") {
};
//Next commented line does not work if you change the input for '2' to '2e'
and click outside the input box.
//NumberTextField ntf = new NumberTextField<>("tf", intModel);
NumberTextField ntf = new NumberTextField<>("tf", intModel, Integer.class);
ntf.setOutputMarkupId(true);
ntf.setMinimum( 1 ).setStep( 1 );
ntf.add( new OnChangeAjaxBehavior()
{
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate( AjaxRequestTarget target )
{
System.out.println("changed!");
}
} );
f.add(ntf);
add(f);
}
}
Re: NumberTextField and LambdaModel, typing goes wrong?
Posted by Sven Meier <sv...@meiers.net>.
Getting generics information from a lambda is (almost) not possible:
https://stackoverflow.com/questions/21887358/reflection-type-inference-on-java-8-lambdas
Sven
Am 5. Oktober 2017 12:37:50 MESZ schrieb Rob Audenaerde <ro...@gmail.com>:
>Thanks Bas,
>
>For now I'll fix it with explicit class in the constructor
>
>I'm not sure but maybe method-references in the LambdaModel could also
>determine their type can the model could be modified to pass that on?
>
>-Rob
>
>On Wed, Oct 4, 2017 at 8:54 PM, Bas Gooren <ba...@iswd.nl> wrote:
>
>> Rob,
>>
>> This is because a property model provides certain metadata (when
>> available);
>> E.g. When the property model points to a field, it provides the type
>of
>> field.
>> Even when the model value is null this type information is still
>available.
>>
>> Since this metadata is not available when using lambdas wicket uses a
>> default (String).
>>
>> The reason it also works with a regular Model is because you provide
>a
>> value; thus, wicket can detect the type based on the value.
>>
>> When your lambda does not return a value, wicket cannot detect the
>type.
>>
>> // Bas
>>
>> Verstuurd vanaf mijn iPhone
>>
>> > Op 4 okt. 2017 om 16:58 heeft Rob Audenaerde
><ro...@gmail.com>
>> het volgende geschreven:
>> >
>> > We are preparing for migration to Wicket 8 (when it comes out). So
>we are
>> > already replacing our old PropertyModels with a backported
>LambdaModel
>> (we
>> > use the implementation from Wicket 8).
>> >
>> > I noticed an odd behavior where the RangeValidator gets an empty
>String
>> > from the input (in validatable.getValue()) if the input is not an
>integer
>> > (for example, changing '2' to '2e' in the textfield).
>> >
>> > This results in a class cast exception.
>> >
>> > If I replace the Lambdamodel by a IModel<Integer> = new
>Model<Integer>(2)
>> > it works fine.
>> >
>> > After adding the type Integer.class to the NumberTextField it works
>fine
>> > again.
>> >
>> > Am I doing something wrong?
>> >
>> > Thanks!
>> >
>> > Here is the relevant code (see commented section):
>> > ------
>> >
>> > public class HomePage extends WebPage {
>> > private static final long serialVersionUID = 1L;
>> >
>> > static class MyInt implements Serializable
>> > {
>> > Integer i=2;
>> >
>> > public Integer getI() {
>> > return i;
>> > }
>> >
>> > public void setI(Integer i) {
>> > this.i = i;
>> > }
>> > }
>> > public HomePage(final PageParameters parameters) {
>> > super(parameters);
>> >
>> > IModel<MyInt> myInt = new Model(new MyInt());
>> > IModel<Integer> intModel = LambdaModel.of(myInt, MyInt::getI,
>> MyInt::setI);
>> > Form f = new Form("f") {
>> >
>> > };
>> > //Next commented line does not work if you change the input for '2'
>to
>> '2e'
>> > and click outside the input box.
>> > //NumberTextField ntf = new NumberTextField<>("tf", intModel);
>> > NumberTextField ntf = new NumberTextField<>("tf", intModel,
>> Integer.class);
>> > ntf.setOutputMarkupId(true);
>> > ntf.setMinimum( 1 ).setStep( 1 );
>> > ntf.add( new OnChangeAjaxBehavior()
>> > {
>> > private static final long serialVersionUID = 1L;
>> >
>> > @Override
>> > protected void onUpdate( AjaxRequestTarget target )
>> > {
>> > System.out.println("changed!");
>> > }
>> > } );
>> > f.add(ntf);
>> > add(f);
>> >
>> > }
>> > }
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
Re: NumberTextField and LambdaModel, typing goes wrong?
Posted by Rob Audenaerde <ro...@gmail.com>.
Thanks Bas,
For now I'll fix it with explicit class in the constructor
I'm not sure but maybe method-references in the LambdaModel could also
determine their type can the model could be modified to pass that on?
-Rob
On Wed, Oct 4, 2017 at 8:54 PM, Bas Gooren <ba...@iswd.nl> wrote:
> Rob,
>
> This is because a property model provides certain metadata (when
> available);
> E.g. When the property model points to a field, it provides the type of
> field.
> Even when the model value is null this type information is still available.
>
> Since this metadata is not available when using lambdas wicket uses a
> default (String).
>
> The reason it also works with a regular Model is because you provide a
> value; thus, wicket can detect the type based on the value.
>
> When your lambda does not return a value, wicket cannot detect the type.
>
> // Bas
>
> Verstuurd vanaf mijn iPhone
>
> > Op 4 okt. 2017 om 16:58 heeft Rob Audenaerde <ro...@gmail.com>
> het volgende geschreven:
> >
> > We are preparing for migration to Wicket 8 (when it comes out). So we are
> > already replacing our old PropertyModels with a backported LambdaModel
> (we
> > use the implementation from Wicket 8).
> >
> > I noticed an odd behavior where the RangeValidator gets an empty String
> > from the input (in validatable.getValue()) if the input is not an integer
> > (for example, changing '2' to '2e' in the textfield).
> >
> > This results in a class cast exception.
> >
> > If I replace the Lambdamodel by a IModel<Integer> = new Model<Integer>(2)
> > it works fine.
> >
> > After adding the type Integer.class to the NumberTextField it works fine
> > again.
> >
> > Am I doing something wrong?
> >
> > Thanks!
> >
> > Here is the relevant code (see commented section):
> > ------
> >
> > public class HomePage extends WebPage {
> > private static final long serialVersionUID = 1L;
> >
> > static class MyInt implements Serializable
> > {
> > Integer i=2;
> >
> > public Integer getI() {
> > return i;
> > }
> >
> > public void setI(Integer i) {
> > this.i = i;
> > }
> > }
> > public HomePage(final PageParameters parameters) {
> > super(parameters);
> >
> > IModel<MyInt> myInt = new Model(new MyInt());
> > IModel<Integer> intModel = LambdaModel.of(myInt, MyInt::getI,
> MyInt::setI);
> > Form f = new Form("f") {
> >
> > };
> > //Next commented line does not work if you change the input for '2' to
> '2e'
> > and click outside the input box.
> > //NumberTextField ntf = new NumberTextField<>("tf", intModel);
> > NumberTextField ntf = new NumberTextField<>("tf", intModel,
> Integer.class);
> > ntf.setOutputMarkupId(true);
> > ntf.setMinimum( 1 ).setStep( 1 );
> > ntf.add( new OnChangeAjaxBehavior()
> > {
> > private static final long serialVersionUID = 1L;
> >
> > @Override
> > protected void onUpdate( AjaxRequestTarget target )
> > {
> > System.out.println("changed!");
> > }
> > } );
> > f.add(ntf);
> > add(f);
> >
> > }
> > }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
Re: NumberTextField and LambdaModel, typing goes wrong?
Posted by Bas Gooren <ba...@iswd.nl>.
Rob,
This is because a property model provides certain metadata (when available);
E.g. When the property model points to a field, it provides the type of field.
Even when the model value is null this type information is still available.
Since this metadata is not available when using lambdas wicket uses a
default (String).
The reason it also works with a regular Model is because you provide a
value; thus, wicket can detect the type based on the value.
When your lambda does not return a value, wicket cannot detect the type.
// Bas
Verstuurd vanaf mijn iPhone
> Op 4 okt. 2017 om 16:58 heeft Rob Audenaerde <ro...@gmail.com> het volgende geschreven:
>
> We are preparing for migration to Wicket 8 (when it comes out). So we are
> already replacing our old PropertyModels with a backported LambdaModel (we
> use the implementation from Wicket 8).
>
> I noticed an odd behavior where the RangeValidator gets an empty String
> from the input (in validatable.getValue()) if the input is not an integer
> (for example, changing '2' to '2e' in the textfield).
>
> This results in a class cast exception.
>
> If I replace the Lambdamodel by a IModel<Integer> = new Model<Integer>(2)
> it works fine.
>
> After adding the type Integer.class to the NumberTextField it works fine
> again.
>
> Am I doing something wrong?
>
> Thanks!
>
> Here is the relevant code (see commented section):
> ------
>
> public class HomePage extends WebPage {
> private static final long serialVersionUID = 1L;
>
> static class MyInt implements Serializable
> {
> Integer i=2;
>
> public Integer getI() {
> return i;
> }
>
> public void setI(Integer i) {
> this.i = i;
> }
> }
> public HomePage(final PageParameters parameters) {
> super(parameters);
>
> IModel<MyInt> myInt = new Model(new MyInt());
> IModel<Integer> intModel = LambdaModel.of(myInt, MyInt::getI, MyInt::setI);
> Form f = new Form("f") {
>
> };
> //Next commented line does not work if you change the input for '2' to '2e'
> and click outside the input box.
> //NumberTextField ntf = new NumberTextField<>("tf", intModel);
> NumberTextField ntf = new NumberTextField<>("tf", intModel, Integer.class);
> ntf.setOutputMarkupId(true);
> ntf.setMinimum( 1 ).setStep( 1 );
> ntf.add( new OnChangeAjaxBehavior()
> {
> private static final long serialVersionUID = 1L;
>
> @Override
> protected void onUpdate( AjaxRequestTarget target )
> {
> System.out.println("changed!");
> }
> } );
> f.add(ntf);
> add(f);
>
> }
> }
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org