generate Setter Name - Android java.lang.reflect

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

Description

generate Setter Name

Demo Code


//package com.java2s;

import android.text.TextUtils;

public class Main {
    public static String generateSetterName(String fieldName) {
        if (TextUtils.isEmpty(fieldName)) {
            throw new IllegalArgumentException("invalid field.");
        }/*from  w  ww  . ja v  a2  s  . c o  m*/
        String firstCH = fieldName.substring(0);
        Character secondCH = null;
        if (fieldName.length() > 1) {
            secondCH = Character.valueOf(fieldName.charAt(1));
        }
        String setterName = null;
        if ((secondCH == null)
                || (!Character.isUpperCase(secondCH.charValue()))) {
            setterName = "set"
                    + fieldName
                            .replaceFirst(firstCH, firstCH.toUpperCase());
        } else {
            setterName = "set" + fieldName;
        }
        return setterName;
    }
}

Related Tutorials