Java Reflection Method Getter Get getGetterMethods(Class clazz)

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

Description

get Getter Methods

License

Apache License

Parameter

Parameter Description
clazz The class from which it will detect all the getter methods.

Return

A map with the names of the members as key and the as value.

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

public class Main {
    private static WeakHashMap<Class<?>, Map<String, Method>> cache = new WeakHashMap<Class<?>, Map<String, Method>>();

    /**//from   www  .  java 2s .  com
     * @param clazz
     *            The class from which it will detect all the getter methods.
     * @return A map with the names of the members as key and the {@link Method} as value.
     */
    public static Map<String, Method> getGetterMethods(Class<?> clazz) {
        if (isPrimite(clazz)) {
            return Collections.emptyMap();
        }

        Map<String, Method> result = cache.get(clazz);
        if (result == null) {
            result = createGetterMethods(clazz);
            cache.put(clazz, result);
        }
        return result;
    }

    /**
     * @param type
     *            The class that will be checked if it is a primitive class
     * @return true when the class is either a java primitive (e.g. int, long, double, etc.), an array, an enumeration
     *         or a class from the java library.
     */
    private static boolean isPrimite(Class<?> type) {
        return type.isPrimitive() || type.isArray() || type.isEnum()
                || type.getPackage().getName().startsWith("java.");
    }

    private static Map<String, Method> createGetterMethods(Class<?> clazz) {
        Map<String, Method> result = new HashMap<String, Method>();
        for (Method method : clazz.getMethods()) {
            if (isGetter(method)) {
                String name = null;
                if (name == null || name.isEmpty()) {
                    name = method.getName();
                    name = name.startsWith("get") ? name.substring("get".length()) : name.substring("is".length());
                    name = name.replaceAll("([A-Z])", "_$1").toLowerCase();
                    if (name.charAt(0) == '_') {
                        name = name.substring(1);
                    }
                }

                result.put(name, method);
            }
        }
        return result;
    }

    private static boolean isGetter(Method method) {
        String name = method.getName();
        if (method.getParameterTypes().length == 0 && method.getReturnType() != Void.TYPE) {
            if (name.startsWith("get") || name.startsWith("is")) {
                return !name.equals("getClass");
            }
        }
        return false;
    }
}

Related

  1. getGetterMethodByProperty(String propertyName, Class beanClass, Class returnType)
  2. getGetterMethodForClass(Class cls, String beanName)
  3. getGetterMethodName(String fieldName, java.lang.Class fieldType)
  4. getGetterMethodName(String property, String javaType)
  5. getGetterMethodNames(Object o)
  6. getGetterMethods(Class clazz)
  7. getGetterMethods(Class clazz)
  8. getGetterMethods(Class objectType)
  9. getGetterMethods(Class pojoClass)