Here you can find the source of invokeGetter(Object entity, String propertyName)
Parameter | Description |
---|---|
entity | The entity. |
propertyName | The property name. |
Parameter | Description |
---|---|
Exception | an exception |
public static Object invokeGetter(Object entity, String propertyName) throws Exception
//package com.java2s; /**//w ww .j av a2 s . c o m * Copyright 2005-2014 Restlet * * The contents of this file are subject to the terms of one of the following * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can * select the license that you prefer but you may not use this file except in * compliance with one of these Licenses. * * You can obtain a copy of the Apache 2.0 license at * http://www.opensource.org/licenses/apache-2.0 * * You can obtain a copy of the EPL 1.0 license at * http://www.opensource.org/licenses/eclipse-1.0 * * See the Licenses for the specific language governing permissions and * limitations under the Licenses. * * Alternatively, you can obtain a royalty free commercial license with less * limitations, transferable or non-transferable, directly at * http://restlet.com/products/restlet-framework * * Restlet is a registered trademark of Restlet S.A.S. */ import java.lang.reflect.Method; public class Main { /** * Returns the value of a property on an entity based on its name. * * @param entity * The entity. * @param propertyName * The property name. * @return The value of a property for an entity. * @throws Exception */ public static Object invokeGetter(Object entity, String propertyName) throws Exception { Object result = null; if (propertyName != null && entity != null) { propertyName = propertyName.replaceAll("/", "."); Object o = entity; String pty = propertyName; int index = propertyName.indexOf("."); if (index != -1) { o = invokeGetter(entity, propertyName.substring(0, index)); pty = propertyName.substring(index + 1); result = invokeGetter(o, pty); } else { String getterName = null; char firstLetter = propertyName.charAt(0); if (Character.isLowerCase(firstLetter)) { getterName = "get" + Character.toUpperCase(firstLetter) + pty.substring(1); } else { getterName = "get" + pty; } Method getter = null; Method method; for (int i = 0; (getter == null) && (i < entity.getClass().getDeclaredMethods().length); i++) { method = entity.getClass().getDeclaredMethods()[i]; if (method.getName().equals(getterName)) { getter = method; } } if (getter != null) { result = getter.invoke(o); } } } return result; } }