Here you can find the source of getGetterMethods(Class> clazz)
Parameter | Description |
---|---|
clazz | The class from which it will detect all the getter methods. |
public static Map<String, Method> getGetterMethods(Class<?> clazz)
//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; } }