Java Reflection Method Setter Get getSettersForType(Class clazz, String typeName)

Here you can find the source of getSettersForType(Class clazz, String typeName)

Description

Gets the setter methods of a class that take a parameter of a certain type

License

BSD License

Parameter

Parameter Description
clazz The class to find setter methods on
typeName The name of the type to find setters for

Return

All setter methods which take a parameter of the named type

Declaration

public static Method[] getSettersForType(Class clazz, String typeName) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright SemanticBits, Northwestern University and Akaza Research
 * /*from w w w . j  a  va 2 s  .c om*/
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caaers/LICENSE.txt for details.
 ******************************************************************************/

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.HashSet;

import java.util.Set;

public class Main {
    /**
     * Gets the setter methods of a class that take a parameter of a certain type
     * 
     * @param clazz
     *       The class to find setter methods on
     * @param typeName
     *       The name of the type to find setters for
     * @return
     *       All setter methods which take a parameter of the named type
     */
    public static Method[] getSettersForType(Class clazz, String typeName) {
        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())) {
                        Class[] paramTypes = current.getParameterTypes();
                        if (paramTypes.length == 1) {
                            if (paramTypes[0].getName().equals(typeName)) {
                                allMethods.add(current);
                            }
                        }
                    }
                }
            }
            checkClass = checkClass.getSuperclass();
        }
        Method[] methodArray = new Method[allMethods.size()];
        allMethods.toArray(methodArray);
        return methodArray;
    }
}

Related

  1. getSetters(Class c)
  2. getSetters(Class clazz)
  3. getSetters(Class clazz)
  4. getSetters(Class klass)
  5. getSetters(Object obj)
  6. getSetterShorthandName(Method method)
  7. getSetterType(Method method)