public static String getMethodPropertyName(String methodName) { String methodPropertyName = null; if (methodName.startsWith("get")) { methodPropertyName = decapitalize(methodName.substring(3)); } else if (methodName.startsWith("is")) { methodPropertyName = decapitalize(methodName.substring(2)); } else if (methodName.startsWith("set")) { methodPropertyName = decapitalize(methodName.substring(3)); } return methodPropertyName; }
public static Method getSetterMethod(Class clazz, String name) { Method[] methods = clazz.getMethods(); for (Method method : methods) { String methodName = method.getName(); if (methodName.startsWith("set") && method.getParameterTypes().length == 1) { if (decapitalize(methodName.substring(3)).equals(name)) { return method; } } } throw new IllegalArgumentException("no such setter method: " + clazz.getName() + '.' + name); }
public static Method getGetterMethod(Class clazz, String name) { for (Class superClass = clazz; superClass != null && superClass != Object.class; superClass = superClass.getSuperclass()) { for (Method method : superClass.getDeclaredMethods()) { String methodName = method.getName(); if (method.getParameterTypes().length != 0) continue; if (methodName.startsWith("get")) { if (decapitalize(methodName.substring(3)).equals(name)) return method; } else if (methodName.startsWith("is")) { if (decapitalize(methodName.substring(2)).equals(name)) return method; } } } return null; }