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

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

Description

get Getter

License

Open Source License

Declaration

public static Method getGetter(Class c, Field field) 

Method Source Code


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

import java.lang.reflect.Field;

import java.lang.reflect.Method;

public class Main {
    public static Method getGetter(Class c, Field field) {
        String fieldName = field.getName();
        //if boolean
        String methodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        try {/*from  www.j av a2 s.c  om*/
            Method result = c.getMethod(methodName);
            return result;
        } catch (SecurityException e) {
            e.printStackTrace(); //?
            return null;
        } catch (NoSuchMethodException e) {

            methodName = "is" + methodName.substring(3);
            try {
                Method result = c.getMethod(methodName);
                return result;
            } catch (SecurityException ex) {
                e.printStackTrace(); //?
                return null;
            } catch (NoSuchMethodException ex) {
                return null;
            }

        }
    }
}

Related

  1. getGetter(A instance, String strAttributeName, Class clazz)
  2. getGetter(Class _class, String fieldName)
  3. getGetter(Class clazz, String name)
  4. getGetter(Class clazz, Field field)
  5. getGetter(Class clazz, Field field)
  6. getGetter(Class cls, String name, Class type)