Java Utililty Methods Reflection Method Setter Get

List of utility methods to do Reflection Method Setter Get

Description

The list of methods to do Reflection Method Setter Get are organized into topic(s).

Method

SetgetSetters(Class klass)
get Setters
Set<Method> getters = new HashSet<Method>();
for (Method m : Arrays.asList(klass.getDeclaredMethods())) {
    if (m.getName().startsWith("set") && m.getParameterTypes().length == 1
            && void.class.equals(m.getReturnType())) {
        getters.add(m);
return getters;
...
ListgetSetters(Class c)
Finds the setters of a class.
return getSetters(c, false);
ListgetSetters(Class clazz)
Finds all setters in the given class and super classes.
Method[] methods = clazz.getMethods();
List<Method> list = new ArrayList<Method>();
for (Method method : methods) {
    if (isSetter(method)) {
        list.add(method);
return list;
...
MapgetSetters(Class clazz)
get a map of public setters (propertyName -> Method)
Map<String, Method> setters = new HashMap<String, Method>();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
    String methodName = method.getName();
    if (method.getParameterTypes().length == 1 && methodName.startsWith("set") && methodName.length() > 3
            && method.getReturnType() == Void.TYPE) {
        String propertyName = methodName.substring(3, 4).toLowerCase();
        if (methodName.length() > 4) {
...
MapgetSetters(Class klass)
get Setters
Map<String, Method> methods = new HashMap<String, Method>();
for (Method method : klass.getMethods()) {
    String name = method.getName();
    if (name.startsWith("set") && name.length() > 3 && Modifier.isPublic(method.getModifiers())
            && !Modifier.isStatic(method.getModifiers()) && Void.TYPE.equals(method.getReturnType())
            && method.getParameterTypes().length == 1) {
        methods.put(name.substring(3, 4).toLowerCase(Locale.ENGLISH) + name.substring(4), method);
return methods;
ListgetSetters(Object obj)
get Setters
List<Method> ret = new ArrayList<Method>();
for (Method method : obj.getClass().getMethods()) {
    int mod = method.getModifiers();
    if (Modifier.isStatic(mod) || !Modifier.isPublic(mod))
        continue;
    if (method.getParameterTypes().length != 1 || method.getReturnType() != Void.TYPE)
        continue;
    String name = method.getName();
...
Method[]getSettersForType(Class clazz, String typeName)
Gets the setter methods of a class that take a parameter of a certain type
Set allMethods = new HashSet();
Class checkClass = clazz;
while (checkClass != null) {
    Method[] classMethods = checkClass.getDeclaredMethods();
    for (int i = 0; i < classMethods.length; i++) {
        Method current = classMethods[i];
        if (current.getName().startsWith("set")) {
            if (Modifier.isPublic(current.getModifiers())) {
...
StringgetSetterShorthandName(Method method)
get Setter Shorthand Name
if (!isSetter(method)) {
    return method.getName();
String name = method.getName();
if (name.startsWith("set")) {
    name = name.substring(3);
    name = name.substring(0, 1).toLowerCase(Locale.ENGLISH) + name.substring(1);
return name;
ClassgetSetterType(Method method)
Returns the class of the argument to the setter.
if (!isSetter(method)) {
    throw new RuntimeException("The method " + method.getDeclaringClass().getName() + "." + method.getName()
            + " is not a setter.");
return method.getParameterTypes()[0];