Java Reflection Method Getter Get getGetters(Class clazz)

Here you can find the source of getGetters(Class clazz)

Description

get Getters

License

Open Source License

Declaration

public static Map<String, Method> getGetters(Class<?> clazz) 

Method Source Code


//package com.java2s;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedHashMap;
import java.util.Map;

public class Main {
    public static Map<String, Method> getGetters(Class<?> clazz) {
        Map<String, Method> map = new LinkedHashMap<String, Method>();
        Class<?> searchType = clazz;
        while (searchType != null && searchType != Object.class) {
            Method[] methods = (searchType.isInterface()) ? new Method[0] : searchType.getDeclaredMethods();
            for (Method method : methods) {
                String name = method.getName();
                int modifiers = method.getModifiers();
                if (modifiers - Modifier.PUBLIC == 0 && method.getParameterTypes().length == 0) {
                    method.setAccessible(true);
                    if (name.length() > 3 && name.startsWith("get")) {
                        name = Character.toLowerCase(name.charAt(3)) + name.substring(4, name.length());
                        map.put(name, method);
                    } else if (name.length() > 2 && name.startsWith("is")) {
                        name = Character.toLowerCase(name.charAt(2)) + name.substring(3, name.length());
                        map.put(name, method);
                    }// w  w  w .j a  v a 2s .  c o  m
                }
            }
            searchType = searchType.getSuperclass();
        }
        return map;
    }
}

Related

  1. getGetterPropertyName(Method getterMethod)
  2. getGetters(Class c)
  3. getGetters(Class clazz)
  4. getGetters(Class klass)
  5. getGetters(Class c)
  6. getGetters(Class clazz)
  7. getGetters(final Class clazz)
  8. getGetters(Object bean)
  9. getGetters(Object obj)