Java examples for Reflection:Java Bean
Return the named getter method on the bean or null if not found.
/*/* ww w.j a v a 2 s .co m*/ * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public License (LGPL), version 2.1 or later. * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. */ //package com.java2s; import java.lang.reflect.Method; public class Main { /** * Return the named getter method on the bean or null if not found. * * @param bean The bean * @param propertyName The property to get the getter for * * @return the named getter method */ private static Method getMethod(Object bean, String propertyName) { final StringBuilder sb = new StringBuilder("get").append(Character .toUpperCase(propertyName.charAt(0))); if (propertyName.length() > 1) { sb.append(propertyName.substring(1)); } final String getterName = sb.toString(); for (Method m : bean.getClass().getMethods()) { if (getterName.equals(m.getName()) && m.getParameterTypes().length == 0) { return m; } } return null; } }