get All Setters Include Super Class - Java Reflection

Java examples for Reflection:Field Get

Description

get All Setters Include Super Class

Demo Code


//package com.java2s;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        System.out.println(getAllSettersIncludeSuperClass(clazz));
    }//from   w  w w  .  j  av  a  2s .  c o m

    private static final String SETMETHOD_PREFIX = "set";

    public static List<Method> getAllSettersIncludeSuperClass(
            final Class<?> clazz) {
        List<Method> ls = new ArrayList<Method>();
        for (Class<?> superClass = clazz; superClass != null
                && superClass != Object.class; superClass = superClass
                .getSuperclass()) {
            Method[] methods = superClass.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                Method m = methods[i];
                if (m.getName().startsWith(SETMETHOD_PREFIX)) {
                    Class<?>[] paramTypes = m.getParameterTypes();
                    if (paramTypes.length == 1) {
                        ls.add(m);
                    }
                }
            }
        }
        return ls;

    }
}

Related Tutorials