get Getter Method Name For Field - Java Reflection

Java examples for Reflection:Getter

Description

get Getter Method Name For Field

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static String getGetterMethodNameForField(Object obj, Field field) {
        if (field.getType() == Boolean.class
                || field.getType() == boolean.class) {
            String isGetterName = "is"
                    + field.getName().substring(0, 1).toUpperCase()
                    + field.getName().substring(1);
            try {
                obj.getClass().getDeclaredMethod(isGetterName);
                return isGetterName;
            } catch (NoSuchMethodException nsme) {
            }/*from w w  w  . ja  v a 2s.  c o  m*/
        }
        return "get" + field.getName().substring(0, 1).toUpperCase()
                + field.getName().substring(1);
    }
}

Related Tutorials