Java反射:如何获取java类的所有getter方法并调用它们

问题描述 投票:64回答:6

我写了一个有很多getter的java类..现在我想得到所有getter方法并在某个时候调用它们。我知道有一些方法,比如getMethods()或getMethod(String name,Class ... parameterTypes),但是我只是想得到吸气剂...,使用正则表达式?谁能告诉我?谢谢!

java reflection
6个回答
149
投票

不要使用正则表达式,使用Introspector

for(PropertyDescriptor propertyDescriptor : 
    Introspector.getBeanInfo(yourClass).getPropertyDescriptors()){

    // propertyEditor.getReadMethod() exposes the getter
    // btw, this may be null if you have a write-only property
    System.out.println(propertyDescriptor.getReadMethod());
}

通常您不需要Object.class中的属性,因此您将使用具有两个参数的方法:

Introspector.getBeanInfo(yourClass, stopClass)
// usually with Object.class as 2nd param
// the first class is inclusive, the second exclusive

顺便说一句:有一些框架可以帮助您,并为您提供高级视图。例如。 commons / beanutils有这个方法

Map<String, String> properties = BeanUtils.describe(yourObject);

docs here)就是这样做的:找到并执行所有的getter并将结果存储在map中。不幸的是,BeanUtils.describe()在返回之前将所有属性值转换为字符串。 WTF。谢谢@danw


更新:

这是一个基于对象的bean属性返回Map<String, Object>的Java 8方法。

public static Map<String, Object> beanProperties(Object bean) {
  try {
    return Arrays.asList(
         Introspector.getBeanInfo(bean.getClass(), Object.class)
                     .getPropertyDescriptors()
      )
      .stream()
      // filter out properties with setters only
      .filter(pd -> Objects.nonNull(pd.getReadMethod()))
      .collect(Collectors.toMap(
        // bean property name
        PropertyDescriptor::getName,
        pd -> { // invoke method to get value
            try { 
                return pd.getReadMethod().invoke(bean);
            } catch (Exception e) {
                // replace this with better error handling
               return null;
            }
        }));
  } catch (IntrospectionException e) {
    // and this, too
    return Collections.emptyMap();
  }
}

但是,您可能希望使错误处理更加健壮。对于样板,很抱歉,检查异常会阻止我们在这里完全正常运行。


事实证明Collectors.toMap()讨厌空值。以下是上述代码的更为命令式版本:

public static Map<String, Object> beanProperties(Object bean) {
    try {
        Map<String, Object> map = new HashMap<>();
        Arrays.asList(Introspector.getBeanInfo(bean.getClass(), Object.class)
                                  .getPropertyDescriptors())
              .stream()
              // filter out properties with setters only
              .filter(pd -> Objects.nonNull(pd.getReadMethod()))
              .forEach(pd -> { // invoke method to get value
                  try {
                      Object value = pd.getReadMethod().invoke(bean);
                      if (value != null) {
                          map.put(pd.getName(), value);
                      }
                  } catch (Exception e) {
                      // add proper error handling here
                  }
              });
        return map;
    } catch (IntrospectionException e) {
        // and here, too
        return Collections.emptyMap();
    }
}

使用JavaSlang,以更简洁的方式使用相同的功能:

public static Map<String, Object> javaSlangBeanProperties(Object bean) {
    try {
        return Stream.of(Introspector.getBeanInfo(bean.getClass(), Object.class)
                                     .getPropertyDescriptors())
                     .filter(pd -> pd.getReadMethod() != null)
                     .toJavaMap(pd -> {
                         try {
                             return new Tuple2<>(
                                     pd.getName(),
                                     pd.getReadMethod().invoke(bean));
                         } catch (Exception e) {
                             throw new IllegalStateException();
                         }
                     });
    } catch (IntrospectionException e) {
        throw new IllegalStateException();

    }
}

这是一个番石榴版本:

public static Map<String, Object> guavaBeanProperties(Object bean) {
    Object NULL = new Object();
    try {
        return Maps.transformValues(
                Arrays.stream(
                        Introspector.getBeanInfo(bean.getClass(), Object.class)
                                    .getPropertyDescriptors())
                      .filter(pd -> Objects.nonNull(pd.getReadMethod()))
                      .collect(ImmutableMap::<String, Object>builder,
                               (builder, pd) -> {
                                   try {
                                       Object result = pd.getReadMethod()
                                                         .invoke(bean);
                                       builder.put(pd.getName(),
                                                   firstNonNull(result, NULL));
                                   } catch (Exception e) {
                                       throw propagate(e);
                                   }
                               },
                               (left, right) -> left.putAll(right.build()))
                      .build(), v -> v == NULL ? null : v);
    } catch (IntrospectionException e) {
        throw propagate(e);
    }
}

20
投票

你可以使用Reflections框架

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

9
投票
 // Get the Class object associated with this class.
    MyClass myClass= new MyClass ();
    Class objClass= myClass.getClass();

    // Get the public methods associated with this class.
    Method[] methods = objClass.getMethods();
    for (Method method:methods)
    {
        System.out.println("Public method found: " +  method.toString());
    }

8
投票

Spring为Bean内省提供了一个简单的BeanUtil method

PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property);
Method getter = pd.getReadMethod();

1
投票

这段代码测试好了。

private void callAllGetterMethodsInTestModel(TestModel testModelObject) {
        try {
            Class testModelClass = Class.forName("com.example.testreflectionapi.TestModel");
            Method[] methods = testModelClass.getDeclaredMethods();
            ArrayList<String> getterResults = new ArrayList<>();
            for (Method method :
                    methods) {
                if (method.getName().startsWith("get")){
                    getterResults.add((String) method.invoke(testModelObject));
                }
            }
            Log.d("sayanReflextion", "==>: "+getterResults.toString());
        } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

-4
投票

您应该在每个bean中维护一个通用的getter,以便调用getAttribute1(),您应该能够调用一个通用的getter get(“Attribute1”)

这个通用的getter会调用正确的getter

Object get(String attribute)
{
    if("Attribute1".equals(attribute)
    {
        return getAttribute1();
    }
}

这种方法涉及到在每个bean中维护这个单独的列表,但这样可以避免出现性能问题的反射,因此如果编写需要具有良好性能的生产代码,则可以对所有bean使用上述模式。

如果它是一些没有高性能要求的测试代码或实用程序代码,那么你最好采用其他方法,因为这种方法容易出错,除非你可以编写某种编译时检查器来确保这个通用的getter函数适用于所有属性。

© www.soinside.com 2019 - 2024. All rights reserved.