How java uses reflection to get field names that use generics

public class  ClassA <T>{  
    private String name;
    private Object object;    
    private T obj;       
    public void setObject(Object object) {      this.object = object;  }     
    public Object getObject() {    return object;   }
    public void setObj(T obj) {   this.obj = obj;   }
    public T getObj() { return obj;   } 
    public void setName(String name) { this.name = name; }
    public String getName() { return name;  }
}

as ClassA above, because the generic symbol may not be T, how to get the field name obj through reflection?

Jun.07,2022

field name? Get it as much as you can. What does it have to do with field types

Field[] fields = ClassA.class.getDeclaredFields();
for (Field field : fields) {
    System.out.println(field.getName());
}
The field.getType () return of the

obj field is Object, which can be judged by this.


landlord, how did you solve this problem?

Menu