reflection problem

hi all, i got a problem with this bunch of code:

[CODE]

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();

}

[/CODE]

i got a class Manager with a public (otherwise i'd get a NoSuchFieldException) Integer bonus field. i'd like to modify it via reflection, but i get an error at this point:

Integer newBonus = field.getInt(boss);

[CODE]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)[/CODE]

illegal data type conversion to int? what does it mean? how can i fix this? thank you all :)

[1492 byte] By [xplorer87a] at [2007-11-27 11:42:00]
# 1

<Integer newBonus = field.getInt(boss);

pretty sure you got a string in that.... what exactly are you trying to do?>

mark07a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 2

Are you trying to hack Manager to give him a bonus ;-)

BigDaddyLoveHandlesa at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 3

hi, ty for replying.

i'm trying to get the value of the current field bonus (Integer newBonus = field.getInt(boss);), modify it (newBonus = newBonus + 200; could be anything else), and then setting it back (f.setInt(boss, newBonus);).

why do you say that i got a string in... where? :x

xplorer87a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 4

<Manager boss = new Manager("Paul Brown", 1000, 500);

><field.getInt(boss)

><why do you say that i got a string in... where? :x

becuase you set boss to also have his name my suggestion is put another method in the class that's .getbonus to return what value you want.>

mark07a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 5

Why are you using reflection to do this?

BigDaddyLoveHandlesa at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 6

well, obviously i can write getter/setter methods to do that in a simply way... to be honest, i would have never written such a complicated code to do a easy task like this one, if i wasn't forced by OOP learning purposes: in clean english, i have to do that like an homework :)

xplorer87a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 7

> i have to do that like an homework :)

There are more legitimate uses for reflection. Too bad your instructor didn't assign that.

BigDaddyLoveHandlesa at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 8

oh :|

too bad too, that i got a pdf of a slide shown at lesson, where he writes a similar code, assuming it's working fine :(

Plane a = new Plane ();

Class c = a.getClass();

Field f = a.getField(speed); // i think here there's an error, should be c.getField("speed"), otherwise it makes no sense :x

Float v = f.getFloat(a);

v=v+10;

f.setFloat(a,v);

xplorer87a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 9

Works for me:

import java.lang.reflect.*;

public class ReflectionExample {

public int foo;

public static void main(String[] args)

throws NoSuchFieldException, IllegalAccessException {

ReflectionExample obj = new ReflectionExample();

obj.foo = 17;

Class cls = obj.getClass();

Field field = cls.getField("foo");

int value = field.getInt(obj);

System.out.println("By reflection, foo=" + value);

field.setInt(obj, value+1);

System.out.println("After increment, obj.foo=" + obj.foo);

}

}

BigDaddyLoveHandlesa at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...
# 10

ty all, i found the problem: it was just that getInt() method returns an int, while my field was an integer. i should have used

Integer newBonus = (Integer)f.get(boss);

instead, get returns an object, that i just cast to Integer

xplorer87a at 2007-7-29 17:41:32 > top of Java-index,Java Essentials,Java Programming...