问题

我需要获取具有特定注释的字段的值,因此使用反射我可以获得此字段对象。问题是这个字段将永远是私有的,虽然我事先知道它总是有一个getter方法。我知道我可以使用setAccesible(true)并获取其值(当没有PermissionManager时),但我更喜欢调用它的getter方法。

我知道我可以通过查找"get fieldName"来寻找方法(虽然我知道例如布尔字段有时被命名为"is fieldName")。

我想知道是否有更好的方法来调用这个getter(许多框架使用getters / setters来访问属性,所以也许他们以另一种方式做)。

谢谢


#1 热门回答(204 赞)

我认为这应该指向正确的方向:

import java.beans.*

for (PropertyDescriptor pd : Introspector.getBeanInfo(Foo.class).getPropertyDescriptors()) {
  if (pd.getReadMethod() != null && !"class".equals(pd.getName()))
    System.out.println(pd.getReadMethod().invoke(foo));
}

请注意,你可以自己创建BeanInfo或PropertyDescriptor实例,即不使用Introspector。但是,Introspector在内部进行一些缓存,这通常是一件好事(tm)。如果你没有缓存感到满意,你甚至可以去寻找

// TODO check for non-existing readMethod
Object value = new PropertyDescriptor("name", Person.class).getReadMethod().invoke(person);

但是,有很多库可以扩展和简化java.beans API。 Commons BeanUtils是一个众所周知的例子。在那里,你只需:

Object value = PropertyUtils.getProperty(person, "name");

BeanUtils附带其他方便的东西。即,即时值转换(对象到字符串,字符串到对象),以简化用户输入的设置属性。


#2 热门回答(15 赞)

你可以使用Reflections框架

import static org.reflections.ReflectionUtils.*;
Set<Method> getters = ReflectionUtils.getAllMethods(someClass,
      withModifier(Modifier.PUBLIC), withPrefix("get"), withAnnotation(annotation));

#3 热门回答(4 赞)

命名约定是完善的JavaBeans规范的一部分,并由java.beanspackage中的类支持。


原文链接