Return name of attribute assuming method is a mutator or accessor. - Java Reflection

Java examples for Reflection:Method Name

Description

Return name of attribute assuming method is a mutator or accessor.

Demo Code


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

public class Main {
    /** Prefix for all isser accessor methods. */
    private static final String IS_ACCESSOR_PREFIX = "is";

    /**//from w w  w  .j av  a2  s  . c  o  m
     * Return name of attribute assuming method is a mutator or accessor.
     *
     * @param method the method.
     * @return the attribute name.
     */
    public static String getAttributeName(final Method method) {
        final String methodName = method.getName();
        final boolean isserAccessor = methodName
                .startsWith(IS_ACCESSOR_PREFIX);
        final String name;
        if (isserAccessor) {
            name = Character.toLowerCase(methodName.charAt(2))
                    + methodName.substring(3);
        } else {
            name = Character.toLowerCase(methodName.charAt(3))
                    + methodName.substring(4);
        }
        return name;
    }
}

Related Tutorials