Java Reflection Method Getter Get getGetterFields(final Class clazz)

Here you can find the source of getGetterFields(final Class clazz)

Description

get Getter Fields

License

Open Source License

Declaration

private static List<Field> getGetterFields(final Class<?> clazz) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class Main {
    private static List<Field> getGetterFields(final Class<?> clazz) {
        final List<Field> list = new ArrayList<Field>();
        final Field[] fields = clazz.getDeclaredFields();
        for (final Field field : fields) {
            if (fieldHasGetter(field, clazz)) {
                list.add(field);//from  w  w  w  .  ja v a2s.com
            }
        }
        return list;
    }

    /**
     * Determines if the specified class 'clazz' has a getter method for the specified field
     * 'field'.
     * 
     * An associated getter method for a field with name 'foo' is a method with the folliwing
     * properties:
     * <ol>
     * <li>The name is 'getFoo'.</li>
     * <li>The method has no formal parameters.</li>
     * <li>The return type is the same as the fields return type.</li>
     * <li>The publicicity modifier is 'public'.</li>
     * </ol>
     * In case of a field of boolean type, the getter methods name may also be 'isFoo'.
     * 
     * @param field
     *            A field of the specified class.
     * @param clazz
     *            A class, in which a getter method is searched for the field.
     * @return If the specified class has a method with all the four getter properties for the
     *         specified field.
     */
    private static boolean fieldHasGetter(final Field field, final Class<?> clazz) {
        if (isBooleanType(field.getType())) {
            // If field type is boolean, check if there is a method called "isName"
            final Method getterMethod;
            try {
                getterMethod = clazz.getDeclaredMethod("is" + toFirstUpper(field.getName()));
                if (methodIsGetterForField(getterMethod, field)) {
                    return true;
                }
            } catch (final NoSuchMethodException e) {
            }
        }
        // else and anyway, check if there is a method named "getName"
        try {
            final Method getterMethod = clazz.getDeclaredMethod("get" + toFirstUpper(field.getName()));
            return methodIsGetterForField(getterMethod, field);
        } catch (final NoSuchMethodException e) {
        }
        return false;
    }

    private static boolean isBooleanType(final Class<?> type) {
        return Boolean.TYPE.equals(type) || Boolean.class.equals(type);
    }

    /**
     * Changes the input string with it's first character as a upper-case-character. If the string
     * has no first character, or the first character is already in upper-case, then the input
     * string itself is returned.
     * 
     * @param aString
     *            A string.
     * @return The same string, but with a upper-case first character.
     */
    public static String toFirstUpper(final String aString) {
        if (aString == null) {
            return null;
        }
        if ("".equals(aString)) {
            return "";
        }
        final char charAt0 = aString.charAt(0);
        if (Character.isUpperCase(charAt0)) {
            return aString;
        }
        final StringBuilder sb = new StringBuilder(aString);
        sb.setCharAt(0, Character.toUpperCase(charAt0));
        return sb.toString();
    }

    private static boolean methodIsGetterForField(final Method aMethod, final Field aField) {
        if (aMethod == null || aField == null) {
            return false;
        }
        return aField.getType().equals(aMethod.getReturnType()) && aMethod.getParameterTypes().length == 0
                && ((aMethod.getModifiers() & Modifier.PUBLIC) > 0);
    }
}

Related

  1. getGetter(String key, Class clazz)
  2. getGetter(String name, Type type)
  3. getGetter(String propertyName, Class clazz)
  4. getGetterAttributeType(Method m)
  5. getGetterFieldName(Method method)
  6. getGetterFor(Field field)
  7. getGetterFor(Object obj, Field field, Class objClass)
  8. getGetterFromCache(Class clazz)
  9. getGetterFromProperty(Class clazz, String property)