List of utility methods to do Reflection Method Setter Get
Class> | getSetterAttributeType(Method m) get Setter Attribute Type return m.getParameterTypes()[0];
|
String | getSetterFieldName(Method method) Extract field name from the setter method String methodName = method.getName(); if (methodName.startsWith("set")) { return getFieldName(methodName.substring(3)); throw new IllegalArgumentException("Invalid setter method" + method.getName()); |
Method | getSetterForGetter(Method[] methods, Method getter) get Setter For Getter for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().startsWith("set")) { if (method.getName().substring(1).equals(getter.getName().substring(1))) { return method; return null; |
List | getSetterFromCache(Class clazz) get Setter From Cache List<Method> methods = setterCache.get(clazz); if (methods == null) { methods = new ArrayList<>(); for (Method method : clazz.getDeclaredMethods()) { String name = method.getName(); if (name.startsWith("set") && method.getParameterTypes().length == 1 && name.length() > 3) { methods.add(method); setterCache.put(clazz, methods); return methods; |
Map | getSetterMap(Class cls) get Setter Map TreeMap map = new TreeMap(); Method methods[] = getAllSetters(cls); String memberName; for (int i = 0; i < methods.length; i++) { memberName = methods[i].getName(); memberName = memberName.substring(3); map.put(memberName, methods[i]); return map; |
Method | getSetterMethod(Class beanClass, String fieldName, List get Setter Method for (Class clazz : supportedTypes) { try { Class[] argList = new Class[1]; argList[0] = clazz; return beanClass.getMethod("set" + fieldName, argList); } catch (NoSuchMethodException nsme) { return null; |
Method | getSetterMethod(Class c, String methodName) get Setter Method Method result = null; Method[] methods = c.getMethods(); for (Method m : methods) { String name = m.getName(); if (name.equalsIgnoreCase(methodName)) { Class[] params = m.getParameterTypes(); if (params.length == 1 && PRIMITIVES.contains(params[0])) { result = m; ... |
Method | getSetterMethod(Class containingClass, Class propertyType, String propertyName) get Setter Method String setterName = PROPERTY_MUTATOR_PREFIX + (propertyName.isEmpty() ? "" : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1)); try { return containingClass.getMethod(setterName, propertyType); } catch (NoSuchMethodException e) { return null; |
Method | getSetterMethod(Class type, String property) get Setter Method String setterName = getEtterMethodName(property, "set"); Method setterMethod = null; Method[] methods = type.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().equals(setterName)) { if (setterMethod != null) throw new IllegalStateException(String.format( ... |
Method | getSetterMethod(Class> beanClass, String property) Returns setter method for property in specified beanClass
Method getter = getGetterMethod(beanClass, property); if (getter == null) return null; else return getSetterMethod(beanClass, property, getter.getReturnType()); |