反射泛型获取字段值

问题描述 投票:0回答:10

我正在尝试通过反射获取字段的值。问题是我不知道字段的类型,必须在获取值时决定它。

此代码会产生以下异常:

无法将 java.lang.String 字段 com....fieldName 设置为 java.lang.String

Field field = object.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
        
Class<?> targetType = field.getType();
Object objectValue = targetType.newInstance();

Object value = field.get(objectValue);

我尝试强制转换,但出现编译错误:

field.get((targetType)objectValue)

targetType objectValue = targetType.newInstance();

我该怎么做?

java reflection
10个回答
190
投票

您应该将 object 传递给 fieldget 方法,所以

  import java.lang.reflect.Field;

  Field field = object.getClass().getDeclaredField(fieldName);    
  field.setAccessible(true);
  Object value = field.get(object);

185
投票

就像之前回答的那样,你应该使用:

Object value = field.get(objectInstance);

有时更喜欢的另一种方法是动态调用 getter。示例代码:

public static Object runGetter(Field field, BaseValidationObject o)
{
    // MZ: Find the correct method
    for (Method method : o.getMethods())
    {
        if ((method.getName().startsWith("get")) && (method.getName().length() == (field.getName().length() + 3)))
        {
            if (method.getName().toLowerCase().endsWith(field.getName().toLowerCase()))
            {
                // MZ: Method found, run it
                try
                {
                    return method.invoke(o);
                }
                catch (IllegalAccessException e)
                {
                    Logger.fatal("Could not determine method: " + method.getName());
                }
                catch (InvocationTargetException e)
                {
                    Logger.fatal("Could not determine method: " + method.getName());
                }

            }
        }
    }


    return null;
}

还要注意,当你的类继承另一个类时,需要递归判断Field。例如,获取给定类的所有字段;

    for (Class<?> c = someClass; c != null; c = c.getSuperclass())
    {
        Field[] fields = c.getDeclaredFields();
        for (Field classField : fields)
        {
            result.add(classField);
        }
    }

25
投票

我使用我的首选项类的 toString() 实现中的反射来查看类成员和值(简单快速的调试)。

我正在使用的简化代码:

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    Class<?> thisClass = null;
    try {
        thisClass = Class.forName(this.getClass().getName());

        Field[] aClassFields = thisClass.getDeclaredFields();
        sb.append(this.getClass().getSimpleName() + " [ ");
        for(Field f : aClassFields){
            String fName = f.getName();
            sb.append("(" + f.getType() + ") " + fName + " = " + f.get(this) + ", ");
        }
        sb.append("]");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return sb.toString();
}

我希望它能帮助别人,因为我也搜索过。


11
投票

虽然我不太清楚你想要实现什么,但我在你的代码中发现了一个明显的错误:

Field.get()
期望包含该字段作为参数的对象,而不是该字段的某些(可能)值。所以你应该有
field.get(object)

由于您似乎正在寻找字段值,因此您可以通过以下方式获取该值:

Object objectValue = field.get(object);

无需实例化字段类型并创建一些空/默认值;或者也许我错过了一些东西。


11
投票
 Integer typeValue = 0;
 try {
     Class<Types> types = Types.class;
     java.lang.reflect.Field field = types.getDeclaredField("Type");
     field.setAccessible(true);
     Object value = field.get(types);
     typeValue = (Integer) value;
 } catch (Exception e) {
     e.printStackTrace();
 }

7
投票
    ` 
//Here is the example I used for get the field name also the field value
//Hope This will help to someone
TestModel model = new TestModel ("MyDate", "MyTime", "OUT");
//Get All the fields of the class
 Field[] fields = model.getClass().getDeclaredFields();
//If the field is private make the field to accessible true
fields[0].setAccessible(true);
//Get the field name
  System.out.println(fields[0].getName());
//Get the field value
System.out.println(fields[0].get(model));
`

2
投票

我在 Kotlin 中发布了我的解决方案,但它也可以使用 java 对象。 我创建了一个函数扩展,以便任何对象都可以使用此函数。

fun Any.iterateOverComponents() {

val fields = this.javaClass.declaredFields

fields.forEachIndexed { i, field ->

    fields[i].isAccessible = true
    // get value of the fields
    val value = fields[i].get(this)

    // print result
    Log.w("Msg", "Value of Field "
            + fields[i].name
            + " is " + value)
}}

看看这个网页:https://www.geeksforgeeks.org/field-get-method-in-java-with-examples/


1
投票

能够使用以下方法访问类中的私有字段

 Beneficiary ben = new Beneficiary();//class with multiple fields
 ben.setName("Ashok");//is set by a setter
                
 //then to get that value following was the code which worked for me
 Field[] fields = ben.getClass().getDeclaredFields();
    
 for(Field field: fields) {
   field.setAccessible(true);//to access private fields
   System.out.println(field.get(ben));//to get value
   //assign value for the same field.set(ben, "Y");//to set value
 }

0
投票

您使用错误的参数调用 get 。

应该是:

Object value = field.get(object);

0
投票

这并不完全是基于反射的值访问的答案,但总有一种方法可以更好、更有效地完成工作——无需反射!在编译时,我们可以使用注释处理库,并使用该库

pojo-analyzers
我们可以访问该字段的getter和setter(无需反射,因此是性能最高的选项)。

@DetailedPojo
public class MyClass {
    public String myField;
}
...
MyClass c = new MyClass("bla");
DetailedMyClass.map.get("myField").getFieldValue(c); // bla
© www.soinside.com 2019 - 2024. All rights reserved.