I have a method with the signature public void setFoo(int newFoo) in a model I’m writing. I’m using the following code to call it from within my controller:
protected void setModelProperty(String propertyName, Object newValue) {
for (AbstractModel model: registeredModels) {
try {
Method method = model.getClass().
getMethod("set"+propertyName, new Class[] {
newValue.getClass()
}
);
method.invoke(model, newValue);
} catch (Exception ex) {
// Handle exception.
System.err.println(ex.toString());
}
}
}
Calling this method like controller.setModelProperty("Foo",5); results in an exception being thrown: java.lang.NoSuchMethodException: foo.bar.models.FooModel.setFoo(java.lang.Integer) — it looks like the int is being boxed as an Integer, which doesn’t match the signature of setFoo.
Is there any way to convince this reflection code to pass 5 (or whatever int I pass in) as an Integer, without the boxing? Or do I have to create public void setFoo(Integer newFoo) in my model and unbox explicitly, then call the original setFoo?
You could specialise your
setModelPropertyfor any primitives you expect to be used with:Alternatively, you could use instanceof on newValue to check for boxed primitives:
Or finally, if you have the source for setFoo, you could change it to take a boxed Integer instead of an int – the overhead is usually negligible.