Java Reflection Method Getter Get getGetterMethods(Class pojoClass)

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

Description

Gets the getters of a pojo as a map of String as key and Method as value.

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
//Licensed under the Apache License, Version 2.0 (the "License");

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class Main {
    static final String GET = "get";
    static final String IS = "is";

    /**/*from   w w w  .j a v  a 2s.c  om*/
     * Gets the getters of a pojo as a map of {@link String} as key and 
     * {@link Method} as value.
     */
    public static Map<String, Method> getGetterMethods(Class<?> pojoClass) {
        HashMap<String, Method> methods = new HashMap<String, Method>();
        fillGetterMethods(pojoClass, methods);
        return methods;
    }

    private static void fillGetterMethods(Class<?> pojoClass, Map<String, Method> baseMap) {
        if (pojoClass.getSuperclass() != Object.class)
            fillGetterMethods(pojoClass.getSuperclass(), baseMap);

        Method[] methods = pojoClass.getDeclaredMethods();
        for (int i = 0; i < methods.length; i++) {
            Method m = methods[i];
            if (!Modifier.isStatic(m.getModifiers()) && m.getParameterTypes().length == 0
                    && m.getReturnType() != null && Modifier.isPublic(m.getModifiers())) {
                String name = m.getName();
                if (name.startsWith(IS))
                    baseMap.put(toProperty(IS.length(), name), m);
                else if (name.startsWith(GET))
                    baseMap.put(toProperty(GET.length(), name), m);
            }
        }
    }

    /**
     * Converts a method name into a camel-case field name, starting from {@code start}.
     */
    public static String toProperty(int start, String methodName) {
        char[] prop = new char[methodName.length() - start];
        methodName.getChars(start, methodName.length(), prop, 0);
        int firstLetter = prop[0];
        prop[0] = (char) (firstLetter < 91 ? firstLetter + 32 : firstLetter);
        return new String(prop);
    }
}

Related

  1. getGetterMethodNames(Object o)
  2. getGetterMethods(Class clazz)
  3. getGetterMethods(Class clazz)
  4. getGetterMethods(Class clazz)
  5. getGetterMethods(Class objectType)
  6. getGetterMethods(final Class clazz)
  7. getGetterName(Field field)
  8. getGetterName(Field field)
  9. getGetterName(final Field field)