find Set Method - Android java.lang.reflect

Android examples for java.lang.reflect:Java Bean

Description

find Set Method

Demo Code


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

public class Main{
    /**//www .j  av a 2 s. c  o  m
     * set methods should have only one argument in type of fieldType they should be written as java standards set<FieldName> e.g for fieldName identity setMethod's name should be setIdentity
     * 
     * @param clazz target class
     * @param fieldType field's type
     * @param fieldName field's name
     * @return setter method for specified field.
     * @throws NoSuchMethodException
     * @throws SecurityException
     */
    public static Method findSetMethod(Class<?> clazz, Class<?> fieldType,
            String fieldName) throws SecurityException,
            NoSuchMethodException {
        String methodName = "set" + fieldName.substring(0, 1).toUpperCase()
                + fieldName.substring(1);
        return clazz.getMethod(methodName, fieldType);
    }
}

Related Tutorials