Java Method Call invoke(Object source, String key)

Here you can find the source of invoke(Object source, String key)

Description

invoke

License

Open Source License

Declaration

@SuppressWarnings("rawtypes")
    public static Object invoke(Object source, String key) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;
import java.util.Map;

public class Main {
    @SuppressWarnings("rawtypes")
    public static Object invoke(Object source, String key) {
        if (source instanceof Map) {
            return valueOfMap((Map) source, key);
        } else {//from   www.j ava2 s . c o  m
            return valueOfObj(source, key);
        }
    }

    @SuppressWarnings("rawtypes")
    private static Object valueOfMap(Map map, String key) {
        if (map.isEmpty() || key == null || key.trim() == "") {
            return null;
        }
        return map.get(key);
    }

    private static Object valueOfObj(Object o, String property) {
        if (o == null) {
            return null;
        }
        if (property == null || "".equals(property.trim())) {
            return o;
        }
        String methodName = "get" + property.substring(0, 1).toUpperCase() + property.substring(1);

        Object result = null;
        try {
            Method method = o.getClass().getMethod(methodName);
            result = method.invoke(o, new Object[] {});
        } catch (Exception e) {
            result = null;
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. invoke(Object object, String methodName, Class returnType, Object... parameters)
  2. invoke(Object object, String methodName, Object[] args)
  3. invoke(Object objToInvoke, Class classToInvoke, String method, Class[] argumentClasses, Object[] arguments)
  4. invoke(Object owner, String methodName)
  5. invoke(Object proxy, java.lang.reflect.Method method, Object[] args)
  6. invoke(Object target, Class clazz, String methodName, Class[] parameterTypes, Object... args)
  7. invoke(Object target, Class clazz, String method)
  8. invoke(Object target, Method method)
  9. invoke(Object target, String methodName)