Java Reflection Method Getter Get getGetter(final Class clazz, final String fieldName)

Here you can find the source of getGetter(final Class clazz, final String fieldName)

Description

get Getter

License

Apache License

Declaration

public static Method getGetter(final Class<?> clazz, final String fieldName) 

Method Source Code

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

import java.lang.reflect.Method;

public class Main {

    public static Method getGetter(final Class<?> clazz, final String fieldName) {

        final String methodName = "get" + capitalize(fieldName);

        // public method
        try {/* www.  j  a  v a  2 s  .c  om*/
            final Method method = clazz.getMethod(methodName, Void.class);
            return method;
        } catch (SecurityException | NoSuchMethodException e) {
        }

        // private / protected method
        try {
            final Method method = clazz.getDeclaredMethod(methodName, Void.class);
            method.setAccessible(true);
            return method;
        } catch (SecurityException | NoSuchMethodException e) {
        }

        return null;
    }

    public static String capitalize(final String str) {
        final int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }

        return new StringBuilder(strLen).append(String.valueOf(str.charAt(0)).toUpperCase())
                .append(str.substring(1)).toString();
    }
}

Related

  1. getGetter(Class clazz, Field field)
  2. getGetter(Class clazz, Field field)
  3. getGetter(Class cls, String name, Class type)
  4. getGetter(Class realClass, String pAttributeName)
  5. getGetter(final Class clazz, final String propertyName)
  6. getGetter(final Class clz, final String propertyName)
  7. getGetter(final Object o, final String fieldName)
  8. getGetter(Object bean, String property)
  9. getGetter(Object instance, String methodName)