xplorer87
25-07-2007, 14:49
ciao ragazzi, ho un problemino con questo codice facile-facile:
Manager boss = new Manager("Paul Brown", 1000, 500);
Class c = boss.getClass();
try
{
Field field = c.getField("bonus");
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus);
}
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();
}
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();
}
com'e' facile capire, ho una classe Manager con un campo public (altrimenti avrei NoSuchFieldException) Integer bonus. vorrei modificarlo tramite reflection, ma ottengo un errore in questa riga:
Integer newBonus = field.getInt(boss);
Exception in thread "main" java.lang.IllegalArgumentException: Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus" with illegal data type conversion to int
at sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? per noi comuni mortali cosa sarebbe? :)
grazie a tutti :)
Manager boss = new Manager("Paul Brown", 1000, 500);
Class c = boss.getClass();
try
{
Field field = c.getField("bonus");
Integer newBonus = field.getInt(boss);
newBonus = newBonus + 200;
field.setInt(boss, newBonus);
}
catch(NoSuchFieldException nsfe)
{
System.out.println(nsfe.getMessage());
nsfe.printStackTrace();
}
catch(IllegalAccessException iae)
{
System.out.println(iae.getMessage());
iae.printStackTrace();
}
com'e' facile capire, ho una classe Manager con un campo public (altrimenti avrei NoSuchFieldException) Integer bonus. vorrei modificarlo tramite reflection, ma ottengo un errore in questa riga:
Integer newBonus = field.getInt(boss);
Exception in thread "main" java.lang.IllegalArgumentException: Attempt to get java.lang.Integer field "dynamicbinding.Manager.bonus" with illegal data type conversion to int
at sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.getInt(Unknown Source)
at java.lang.reflect.Field.getInt(Unknown Source)
at reflection.Main.main(Main.java:30)
illegal data type conversion to int? per noi comuni mortali cosa sarebbe? :)
grazie a tutti :)