Convert method name To Property Name - Java Reflection

Java examples for Reflection:Method Name

Description

Convert method name To Property Name

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s";
        System.out.println(methodToPropertyName(s));
    }/*from www. ja  va2  s  . c  om*/

    public static String methodToPropertyName(String s) {
        s = removeMethodAccessor(s);
        return firstCharLowerCase(s);
    }

    public static String removeMethodAccessor(String s) {
        if (isNotEmptyWithTrim(s)) {
            if (s.startsWith("get")) {
                return s.length() > 3 ? s.substring(3) : s;
            } else if (s.startsWith("is")) {
                return s.length() > 2 ? s.substring(2) : s;
            } else if (s.startsWith("set")) {
                return s.length() > 3 ? s.substring(3) : s;
            }
        }
        return s;
    }

    public static String firstCharLowerCase(String s) {
        if (isNotEmptyWithTrim(s)) {
            return Character.toLowerCase(s.charAt(0))
                    + (s.length() > 1 ? s.substring(1) : "");
        }
        return s;
    }

    public static boolean isNotEmptyWithTrim(String s) {
        return !isEmptyWithTrim(s);
    }

    public static boolean isEmptyWithTrim(String s) {
        return s == null || s.trim().length() == 0;
    }
}

Related Tutorials