Java examples for Reflection:Method
get Class Accessor Method
import java.lang.reflect.Method; public class Main{ public static void main(String[] argv) throws Exception{ Class clazz = String.class; String propertyName = "java2s.com"; System.out.println(getAccessor(clazz,propertyName)); }/*www . j a va 2s .c o m*/ public static final String[] ACCESSOR_PREFIXES = new String[] { "is", "get", "has" }; public static Method getAccessor(Class clazz, String propertyName) { propertyName = StringHelper.capitalize(propertyName); for (String prefix : ACCESSOR_PREFIXES) { try { return clazz.getMethod(prefix + propertyName); } catch (NoSuchMethodException ignored) { // ignore } } return null; } }