使用反射检索对象的数据类型

Sou*_*abh 5 java reflection

我有一个初始化的对象:

Object obj  = new Object(){
  final String type = "java.lang.Integer";
  final Object value = 6;
};
Run Code Online (Sandbox Code Playgroud)

我想重新创建这个对象:

 Integer i = 6;
Run Code Online (Sandbox Code Playgroud)

有什么方法可以获得对象的type字段obj并使用反射创建一个新实例并将其中的值提供给它?

编辑:在扩展这个问题后,我发现如果我将对象存储在文件中并使用Jackson使用以下文件从文件中检索它:

Reader reader = new Reader();
MyClass[] instances = reader.readValue(fileName);
Run Code Online (Sandbox Code Playgroud)

MyClass定义为:

class MyClass{

  List<Object> fields;
  .
  .
  .
}
Run Code Online (Sandbox Code Playgroud)

现在我正在迭代fields并使用代码将它们转换为适当的对象:

public static Class<?> getTypeForObject(Object field) {

    Field returnType = null;
    try {
        returnType = field.getClass().getDeclaredField("type");
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    return returnType.getType();
}

public static Object getValueForObject(Object field) {

    Object obj = null;
    try {
        obj = field.getClass().getDeclaredField("value").get(field);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    }
    return obj;
}
Run Code Online (Sandbox Code Playgroud)

但是,当我看到这个表达时field.getClass(),它就会让我LinkedHashMap成为它的阶级.我很困惑为什么以及如果它在内部被视为Map我留下的选项,如果我想用反射做而不使用具体的数据结构,以便一切都是通用的.

icz*_*cza 8

是的你可以.但由于类型obj是一个匿名类扩展java.lang.Object,你不能直接引用它的字段(typevalue),只能通过反射.

以下是您可以执行此操作的代码:

    String type = (String) obj.getClass().getDeclaredField("type").get(obj);
    Object value = obj.getClass().getDeclaredField("value").get(obj);

    // Type can be anything, so in order to instantiate it,
    // we have to assume something. We assume it has a constructor
    // which takes only a String value.
    Object recreated = Class.forName(type).getConstructor(String.class)
            .newInstance(value == null ? null : value.toString());
    System.out.println(recreated);
Run Code Online (Sandbox Code Playgroud)


Ban*_*nan 1

type这将从您的对象中检索字段的值: obj.getClass().getDeclaredField("type").get(obj);