我写了一个有许多getters..now的java类,我想获取所有的getter方法并在某个时候调用它们。我知道有一些方法,比如getMethods()或getMethod(String name,Class...parameterTypes),但我真的只想得到getter ...,使用正则表达式?有人能告诉我吗?谢谢!
发布于 2013-12-02 22:11:13
为此,您可以使用Reflections框架
import org.reflections.ReflectionUtils.*;
Set<Method> getters = ReflectionUtils.getAllMethods(someClass,
ReflectionUtils.withModifier(Modifier.PUBLIC), ReflectionUtils.withPrefix("get"));
发布于 2015-04-14 16:30:51
Spring为Bean自省提供了一个简单的BeanUtil method:
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property);
Method getter = pd.getReadMethod();
发布于 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());
}
https://stackoverflow.com/questions/8524011
复制相似问题