Here you can find the source of getGetterMethod(Class containingClass, String propertyName)
Parameter | Description |
---|---|
containingClass | never null |
propertyName | never null |
public static Method getGetterMethod(Class containingClass, String propertyName)
//package com.java2s; /*//w ww. j av a 2 s . com * Copied from the Hibernate Validator project * Original authors: Hardy Ferentschik, Gunnar Morling and Kevin Pollet. * * 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 { private static final String PROPERTY_ACCESSOR_PREFIX_GET = "get"; private static final String PROPERTY_ACCESSOR_PREFIX_IS = "is"; /** * @param containingClass never null * @param propertyName never null * @return true if that getter exists */ public static Method getGetterMethod(Class containingClass, String propertyName) { String getterName = PROPERTY_ACCESSOR_PREFIX_GET + (propertyName.isEmpty() ? "" : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1)); try { return containingClass.getMethod(getterName); } catch (NoSuchMethodException e) { // intentionally empty } String isserName = PROPERTY_ACCESSOR_PREFIX_IS + (propertyName.isEmpty() ? "" : propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1)); try { Method method = containingClass.getMethod(isserName); if (method.getReturnType() == boolean.class) { return method; } } catch (NoSuchMethodException e) { // intentionally empty } return null; } }