Java Method Call invokeProperty(Object obj, String property)

Here you can find the source of invokeProperty(Object obj, String property)

Description

Invoke the method/field getter on the Object.

License

Apache License

Declaration

private static Object invokeProperty(Object obj, String property) 

Method Source Code

//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);
    }
}

Related

  1. invokePrivate(String name, final Object obj, Object[] objects)
  2. invokePrivateMethod(Method method, Object object, Object... args)
  3. invokePrivateMethod(Object instance, String name, Object... args)
  4. invokePrivateMethod(Object obj, String methodName, Class[] parameterTypes, Object[] args)
  5. invokePrivateMethod(String methodName, Class clazz, Object object)
  6. invokeProtectedMethod(Class c, String method, Object... args)
  7. invokeProtectedMethod(Object o, Object[] args, String methodName, Class[] types)
  8. invokeProxied(final Callable callable, final ClassLoader classLoader)
  9. invokePublicMethod(Object o, String methodName, Class[] classes, Object[] objects)