Java Reflection Method Getter Get getGetter(Class clazz, Field field)

Here you can find the source of getGetter(Class clazz, Field field)

Description

get Getter

License

Apache License

Declaration

public static Method getGetter(Class<?> clazz, Field field) throws SecurityException, NoSuchMethodException 

Method Source Code


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

import java.lang.reflect.Field;

import java.lang.reflect.Method;
import java.util.Locale;

public class Main {
    public static Method getGetter(Object object, Field field) throws SecurityException, NoSuchMethodException {
        return getGetter(object.getClass(), field);
    }//from  w  w  w . j ava  2  s.c  o m

    public static Method getGetter(Class<?> clazz, Field field) throws SecurityException, NoSuchMethodException {
        Method getter = clazz.getMethod(getGetterName(field));
        if (!getter.getReturnType().equals(field.getType())) {
            throw new NoSuchMethodException();
        }
        return getter;
    }

    public static String getGetterName(Field field) {
        String name = field.getName();
        if (field.getType().equals(boolean.class)) {
            return "is" + name.substring(0, 1).toUpperCase(Locale.US) + name.substring(1);
        } else {
            return "get" + name.substring(0, 1).toUpperCase(Locale.US) + name.substring(1);
        }
    }
}

Related

  1. getGetter(A instance, String strAttributeName, Class clazz)
  2. getGetter(Class _class, String fieldName)
  3. getGetter(Class c, Field field)
  4. getGetter(Class clazz, String name)
  5. getGetter(Class clazz, Field field)
  6. getGetter(Class cls, String name, Class type)
  7. getGetter(Class realClass, String pAttributeName)
  8. getGetter(final Class clazz, final String propertyName)