Gets property name from getter name by cutting leading "get" or "is" and lowering the first letter of the rest via regex - Java Reflection

Java examples for Reflection:Getter

Description

Gets property name from getter name by cutting leading "get" or "is" and lowering the first letter of the rest via regex

Demo Code


//package com.java2s;

public class Main {


    /**//from   w ww. ja  v  a 2s.c  om
     * Gets property name from getter name by cutting leading "get" or "is" and lowering the first letter of the rest
     *
     * @param methodName Getter name (e.g. "getFirstName" or "isHot")
     * @return Property name (e.g. "firstName" or "hot")
     */
    public static String getPropertyNameByMethodName(String methodName) {
        String propertyName = methodName.replaceFirst("^(get|is)", "");
        return propertyName.substring(0, 1).toLowerCase()
                + propertyName.substring(1);
    }
}

Related Tutorials