Java Reflection Method Getter Get getGetterMethod(Class type, String property)

Here you can find the source of getGetterMethod(Class type, String property)

Description

get Getter Method

License

Apache License

Declaration

public static Method getGetterMethod(Class type, String property) throws NoSuchMethodException 

Method Source Code

//package com.java2s;
/*//w w w .  j  av  a2 s  . c  o  m
 * Copyright 2007-2013 Ben Galbraith.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Method;

public class Main {
    public static Method getGetterMethod(Class type, String property) throws NoSuchMethodException {
        String getName = getEtterMethodName(property, "get");
        try {
            return type.getMethod(getName);
        } catch (NoSuchMethodException e) {
            try {
                getName = getEtterMethodName(property, "is");
                return type.getMethod(getName);
            } catch (Exception e1) {
                throw new NoSuchMethodException(
                        "No getter found for property '" + property + "' using 'is', 'has', and 'get' prefixes");
            }
        }
    }

    public static String getEtterMethodName(String property, String type) {
        return type + property.substring(0, 1).toUpperCase() + property.substring(1);
    }
}

Related

  1. getGetterFor(Field field)
  2. getGetterFor(Object obj, Field field, Class objClass)
  3. getGetterFromCache(Class clazz)
  4. getGetterFromProperty(Class clazz, String property)
  5. getGetterMethod(Class containingClass, String propertyName)
  6. getGetterMethod(Class beanClass, String property)
  7. getGetterMethod(Class c, String field)
  8. getGetterMethod(Class clazz, String fieldName)
  9. getGetterMethod(Class clazz, String methodNameWithoutGetPrefix)