generate Getter Name - Android java.lang.reflect

Android examples for java.lang.reflect:Method Getter Setter

Description

generate Getter Name

Demo Code


//package com.java2s;

public class Main {
    public static String generateGetterName(String fieldName) {
        return generateGetterName(fieldName, false);
    }//from ww w  . j  av a  2  s .co  m

    public static String generateGetterName(String fieldName,
            boolean isPrimitiveBoolean) {
        String firstCH = "" + fieldName.charAt(0);
        Character secondCH = null;
        if (fieldName.length() > 1) {
            secondCH = Character.valueOf(fieldName.charAt(1));
        }
        String getterName = null;
        String replaceStr = null;
        if ((secondCH == null)
                || (!Character.isUpperCase(secondCH.charValue()))) {
            replaceStr = fieldName.replaceFirst(firstCH,
                    firstCH.toUpperCase());
        } else {
            replaceStr = fieldName;
        }
        if (isPrimitiveBoolean) {
            getterName = "is" + replaceStr;
        } else {
            getterName = "get" + replaceStr;
        }
        return getterName;
    }
}

Related Tutorials