get All Pair Declared Methods - Android java.lang.reflect

Android examples for java.lang.reflect:Java Bean

Description

get All Pair Declared Methods

Demo Code


//package com.java2s;
import android.view.View;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static Set<String> getAllPairDeclaredMethods(
            Class<? extends View> clazz) {
        Method[] methods = clazz.getDeclaredMethods();
        Set<String> getter = new HashSet<>();
        Set<String> setter = new HashSet<>();
        for (Method method : methods) {
            if (Modifier.isPublic(method.getModifiers())) {
                if (method.getParameterTypes().length == 1) {
                    Class param = method.getParameterTypes()[0];
                    if (method.getName().startsWith("set")) {
                        if (param.isPrimitive()
                                || CharSequence.class
                                        .isAssignableFrom(param.getClass())
                                && !param.getSimpleName().equals("Object")) {
                            setter.add(method.getName().substring(3));
                        }/*from   w  ww.j  av  a2 s  .c  o  m*/
                    }
                } else if (method.getParameterTypes().length == 0) {
                    if (method.getName().startsWith("get")) {
                        if (method.getReturnType().isPrimitive()
                                || method.getReturnType().isAssignableFrom(
                                        CharSequence.class)
                                && !method.getReturnType().getSimpleName()
                                        .equals("Object")) {
                            getter.add(method.getName().substring(3));
                        }
                    }
                }
            }
        }
        getter.retainAll(setter);
        return getter;
    }
}

Related Tutorials