获取现有对象的成员/字段 [英] Get Member/Fields of an existing Object

查看:116
本文介绍了获取现有对象的成员/字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通过以下示例说明我的问题:

i will discribe my problem with the following example:

public class Person{

  private int age;
  private String name;

  public Person(int age, String name){
    this.age = age;
    this.name = name;
  }
}

我和一些成员一起上了一堂课(在这种情况下,年龄和姓名),但我不知道我的课有哪些以及多少.另外我什至不关心数量或类型.我不会让只有一个班级的所有成员.像这样:

I ve a class with some Members (age and name in this case) but i don't know which and how much my class does have. Also i don't even care about the amount or the types. I wan't to get all members of only one class. like this:

private List<Object> getAll(Class searchedClass, Object from){
  // This is where the magic happens
}

此方法将返回一个List,其中包含每个不是null的对象,该对象是Class"searchedClass"的实例,并且是Object"from"的成员.

This method shall return a List with every not null object which is an instance of the Class "searchedClass" and is a member of the Object "from".

在我的情况下,我有一个名为Property和PropertyContainerList的类以及一个名为PropertyContainer的接口. PropertyContainerList只能包含实现我的接口PropertyContainer的对象.因此,一个类可以有10个属性作为成员,而另一个则有5个属性,但是可以同时添加两个对象.属性具有方法addListener(...).我希望每次将对象添加到我的列表时,都向该对象的每个属性"成员添加一个侦听器.像这样:

In my case i've classes called Property and PropertyContainerList and an interface called PropertyContainer. A PropertyContainerList can only contain objects which implements my interface PropertyContainer. So a class could've 10 Properties as members and another one cuold've 5 but objects of both can be added. A Property has the method addListener(...). I want, every time an object is added to my list, to add an listener to every "Property"-member of the object. so like this:

if(instance of PropertyContainer is added){
  List<Property> properties = getAll(Property.class, propertyContainerObject);
  for(Property property : properties)
    property.addListener(new Listener());
}

我尝试了一些事情,但是我不知道如何实现getAll(Class,Object)方法.请帮忙:)

I tried a few things but i've no idea how to realize the getAll(Class, Object) method. Please help :)

感谢答案

推荐答案

我建议您看看这篇文章:

I recomend you have a look at this post:

Java-获取所有列表在JVM中加载的类

有关如何在特定程序包或整个JVM中加载所有类的信息. 提供所有已加载/已知类的列表后,您将必须:

on how to load all classes in a certain package or the entire JVM. Once a List of all loaded/ known classes is available you will have to:

1.)检查列表中的每个类是否正在实现所需的接口

1.) Check per class in the list if it is implementing the desired Interface

2.)如果是这样,则根据

2.) If so use Reflection methods to read desired "members" (fields and/or methods) according to http://docs.oracle.com/javase/tutorial/reflect/class/classMembers.html

还要注意,通过反射(它与JDK 1.4 lol一起使用...),您仍然可以在运行时将private更改为public,以读取所有这些字段.

Also note that with reflection (it worked with JDK 1.4 lol...) you are possibly still able to change private to public at runtime to read all of those fields as well.

注意::要获取所有"类的列表,我将依赖于此Google库,而不是从头开始: http://code.google.com/p/reflections/downloads/detail?name=reflections-0.9.9-RC1-uberjar.jar&can=2&q=

NOTE: To get a list of "all" classes i will rely on this google library rather than doing it all from scratch: http://code.google.com/p/reflections/downloads/detail?name=reflections-0.9.9-RC1-uberjar.jar&can=2&q=

这篇关于获取现有对象的成员/字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆