Java Reflection Method Getter Get getGetterFieldName(Method method)

Here you can find the source of getGetterFieldName(Method method)

Description

Extract field name from the getter method

License

Open Source License

Declaration

public static String getGetterFieldName(Method method) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012-2017 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from www .j  a  v  a 2 s  .c om*/
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/

import java.lang.reflect.Method;

public class Main {
    /**
     * Extract field name from the getter method
     */
    public static String getGetterFieldName(Method method) {
        String methodName = method.getName();
        if (methodName.startsWith("get")) {
            return getFieldName(methodName.substring(3));
        } else if (methodName.startsWith("is")) {
            return getFieldName(methodName.substring(2));
        }
        throw new IllegalArgumentException("Invalid getter method" + method.getName());
    }

    /**
     * Compute field name from the stringified string type
     */
    public static String getFieldName(String type) {
        char[] c = type.toCharArray();
        c[0] = Character.toLowerCase(c[0]);
        return new String(c);
    }
}

Related

  1. getGetter(Object object, String name)
  2. getGetter(String key, Class clazz)
  3. getGetter(String name, Type type)
  4. getGetter(String propertyName, Class clazz)
  5. getGetterAttributeType(Method m)
  6. getGetterFields(final Class clazz)
  7. getGetterFor(Field field)
  8. getGetterFor(Object obj, Field field, Class objClass)
  9. getGetterFromCache(Class clazz)