find Setter - Java Reflection

Java examples for Reflection:Setter

Description

find Setter

Demo Code


import java.lang.reflect.Method;
import org.apache.log4j.Logger;

public class Main{
    public static Method findSetter(Class<?> onClass, String field) {
        String methodName = "set" + field.substring(0, 1).toUpperCase()
                + field.substring(1);//from ww w . j  a  v a  2  s .c  o m
        Method[] methods = onClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        methodName = field;
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName)) {
                return method;
            }
        }
        return null;
    }
}

Related Tutorials