get All Pair Methods By View target - Android User Interface

Android examples for User Interface:View Property

Description

get All Pair Methods By View target

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> getAllPairMethodsByView(View target) {
        return getAllPairMethods(target.getClass());
    }/*  w ww . j a v  a2 s  .c o  m*/

    public static Set<String> getAllPairMethods(Class<? extends View> clazz) {
        Method[] methods = clazz.getMethods();
        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")) {
                            //LogUtil.e("Reflect", "Setter: " + method.getName() + "," + param.getSimpleName());
                            setter.add(method.getName().substring(3));
                        }
                    }
                } 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")) {
                            //LogUtil.e("Reflect", "Getter: " + method.getName() + "," + method.getReturnType().getSimpleName());
                            getter.add(method.getName().substring(3));
                        }
                    }
                }
            }
        }
        getter.retainAll(setter);
        return getter;
    }
}

Related Tutorials