Here you can find the source of invokeProperty(Object obj, String property)
private static Object invokeProperty(Object obj, String property)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.Method; public class Main { private static String GET = "get"; private static String IS = "is"; /**/*from w w w.j a va 2 s . c o m*/ * Invoke the method/field getter on the Object. It tries (in order) * obj.getProperty(), obj.isProperty(), obj.property(), obj.property. */ private static Object invokeProperty(Object obj, String property) { if ((property == null) || (property.length() == 0)) { return null; // just in case something silly happens. } Class cls = obj.getClass(); Object[] oParams = {}; Class[] cParams = {}; try { // First try object.getProperty() Method method = cls.getMethod(createMethodName(GET, property), cParams); return method.invoke(obj, oParams); } catch (Exception e1) { try { // First try object.isProperty() Method method = cls.getMethod(createMethodName(IS, property), cParams); return method.invoke(obj, oParams); } catch (Exception e2) { try { // Now try object.property() Method method = cls.getMethod(property, cParams); return method.invoke(obj, oParams); } catch (Exception e3) { try { // Now try object.property() Field field = cls.getField(property); return field.get(obj); } catch (Exception e4) { // oh well return null; } } } } } /** * Convert property name into getProperty name ( "something" -> * "getSomething" ) */ private static String createMethodName(String prefix, String propertyName) { return prefix + propertyName.toUpperCase().charAt(0) + propertyName.substring(1); } }