get Class Mutator Method - Java Reflection

Java examples for Reflection:Method

Description

get Class Mutator Method

Demo Code


import java.lang.reflect.Method;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class clazz = String.class;
        String propertyName = "java2s.com";
        Class params = String.class;
        System.out.println(getMutator(clazz,propertyName,params));
    }//  w  ww  . j  ava2 s .  c o m
    public static final String[] MUTATOR_PREFIXES = new String[] { "set" };
    public static Method getMutator(Class clazz, String propertyName,
            Class... params) {
        propertyName = StringHelper.capitalize(propertyName);
        for (String prefix : MUTATOR_PREFIXES) {
            try {
                return clazz.getDeclaredMethod(prefix + propertyName,
                        params);
            } catch (NoSuchMethodException ignored) {
                // ignore
            }
        }
        return null;
    }
}

Related Tutorials