简体   繁体   中英

Change the value of the fields of an object, that are int, with java Reflection

I have a table with 3 fields, name = varchar, number1 = int, number2 = int

Below I can modify a single field at a time. How could I change all fields of type int for a single time?

public static void change(Object t) throws IllegalArgumentException,
IllegalAccessException {
    Class c = t.getClass(); 
    Field f = t.getDeclaredFields()[0];
    f.set(o, "name2");
    int a = f.getModifiers();
    System.out.println(Modifier.isVolatile(a));
    Field f2 = t.getDeclaredFields()[1];
    f2.setInt(t, 10);
    int a2 = f2.getModifiers();
    System.out.println(Modifier.isVolatile(a2));
}

The step that I like to follow is to check whether the field is int, and then assign the value 10 fixed them all. Whit java reflect.

to get the second and third field automatically,

I try this

public static void sta(Object t) throws IllegalArgumentException, IllegalAccessException {
Class<?> c = t.getClass();
for (Field f : t.getDeclaredFields()) {
    if (f.getType() == int.class) {
        f.setAccessible(true);
        f.set(t, 10);
    }
}


test.java

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
    Object t = new Object("Name1", 777, 777);


    teste2.sta(t);
System.out.println(t.number1);

}}

Object.java

public class Object {
String name;
int number1;
    int number2;
public Object(String name, int number1, int number2){
    this.name=name;
    this.numeber1=number1;
            this.number2=number2 ;
}
public String getName(){
    return name;
}
public int getNumber1(){
    return number1;
}
public int getNumber2(){
    return number2;
    }
}

the error is

Exception in thread "main" java.lang.IllegalArgumentException: Can not set java.lang.String field Object.name to java.lang.Integer
    at  un.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)   at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at teste2.sta(teste2.java:76)
    at teste2.main(teste2.java:95)
Java Result: 1

how do I get the output of System.out.println (t.number1); be modified value by the method teste2.sta (t);

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
        Object t = new Object("Name1", 777, 777);


        teste2.sta(t);
    System.out.println(t.number1);

    }}

The following code iterates over all int fields declared directly in the concrete class of a given object obj, and sets them all to 10:

Class<?> c = obj.getClass();
for (Field f : c.getDeclaredFields()) {
    if (f.getType() == int.class) {
        f.setAccessible(true);
        f.set(obj, 10);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM