List of utility methods to do Reflection Method Setter Get
Method | getSetter(final Object o, final String fieldName) Returns the method of a given object which is the setter-method of a given field whose name is expected to be like setFieldname. return getMethodWithPrefix(o, fieldName, "set").orElse(null); |
Method | getSetter(Method m) get Setter final int index = getterIndex(m.getName()); if (index != -1) { String setterName = "set" + m.getName().substring(index); Class<?> paramTypes = m.getReturnType(); return getDeclaredMethod(m.getDeclaringClass(), setterName, paramTypes); return null; |
Method | getSetter(Object instance, String methodName) get Setter bld.setLength(0); String key = bld.append(instance.getClass().getName()).append(".").append(methodName).toString(); Method setter = setters.get(key); if (setter == null) { Method[] ms = instance.getClass().getMethods(); for (Method m : ms) { if (m.getName().equals(methodName) && m.getParameterTypes().length == 1) { setter = m; ... |
Method | getSetter(Object object, String name, Class type) get setter method by property name String setterName = "set" + capitaliseName(name); return getMethod(object, setterName, type); |
Method | getSetter(Object target, String property, Class> parameterType) Get setter method String setterMethodName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); Method setter = getMethod(target, setterMethodName, parameterType); setter.setAccessible(true); return setter; |
Method | getSetter(String fieldName, Class> clazz, Class> fieldType) Returns the setter-method for the given field name or null if no setter exists. String setterName = "set" + Character.toTitleCase(fieldName.charAt(0)) + fieldName.substring(1, fieldName.length()); try { Method[] methods = clazz.getMethods(); for (Method method : methods) { if (method.getName().equals(setterName)) { Class<?>[] paramTypes = method.getParameterTypes(); if (paramTypes != null && paramTypes.length == 1 && paramTypes[0].isAssignableFrom(fieldType)) { ... |
Method | getSetter(String key, Class> clazz, Class> paramClazz) get Setter if (!key.startsWith("set")) { key = "set" + getDefaultProperty(key); try { return clazz.getMethod(key, new Class[] { paramClazz }); } catch (Exception e) { return null; |
Method | getSetter(String property, Object o) get Setter Method result = null; Method[] methods = o.getClass().getMethods(); String setterName = "set" + property.substring(0, 1).toUpperCase() + property.substring(1); for (int i = 0; i <= methods.length; i++) { if (methods[i].getName().equals(setterName)) { result = methods[i]; break; return result; |
Method | getSetter(String propertyName, Class> clazz, Class> arg) get Setter return clazz.getMethod(String.format("set", capitalize(propertyName)), arg); |
String | getSetter(String s) This new method 'slightly' outperforms the old method, it was essentially a perfect example of me wasting my time and a premature optimization. char[] chars = new char[s.length() + 3]; chars[0] = 's'; chars[1] = 'e'; chars[2] = 't'; chars[3] = toUpperCase(s.charAt(0)); for (int i = s.length() - 1; i != 0; i--) { chars[i + 3] = s.charAt(i); return new String(chars); |