首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java反射:如何获取java类的所有getter方法并调用它们

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

Stack Overflow用户
提问于 2011-12-16 01:15:21
回答 6查看 104.4K关注 0票数 79

我写了一个有许多getters..now的java类,我想获取所有的getter方法并在某个时候调用它们。我知道有一些方法,比如getMethods()或getMethod(String name,Class...parameterTypes),但我真的只想得到getter ...,使用正则表达式?有人能告诉我吗?谢谢!

EN

回答 6

Stack Overflow用户

发布于 2013-12-02 22:11:13

为此,您可以使用Reflections框架

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

Stack Overflow用户

发布于 2015-04-14 16:30:51

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

PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property);
Method getter = pd.getReadMethod();
票数 12
EN

Stack Overflow用户

发布于 2011-12-16 01:22:51

 // 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());
    }
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8524011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档