Java Reflection Method Get from Object getMethod(Object target, String methodName)

Here you can find the source of getMethod(Object target, String methodName)

Description

get Method

License

Open Source License

Declaration

private static Method getMethod(Object target, String methodName)
            throws Exception 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.lang.reflect.Method;

import java.util.Hashtable;

import java.util.Map;

public class Main {
    /** The classloader we should use to load javahelp classes */
    private static ClassLoader classloader = null;
    private static Map METHOD_ARGS = new Hashtable();
    private static Map METHODS = new Hashtable();

    private static Method getMethod(Object target, String methodName)
            throws Exception {
        Method result = (Method) METHODS.get(methodName);

        if (result == null) {
            Object[] argTypes = (Object[]) METHOD_ARGS.get(methodName);
            result = getMethod(target, methodName, argTypes);
            METHODS.put(methodName, result);
        }/*from www.  ja  v  a 2  s  .  c  o  m*/

        return result;
    }

    private static Method getMethod(Object target, String methodName,
            Object[] argTypes) throws Exception {
        Class c = resolveClass(target);

        Class[] paramTypes = new Class[argTypes.length];
        for (int i = 0; i < paramTypes.length; i++)
            paramTypes[i] = resolveClass(argTypes[i]);

        return c.getMethod(methodName, paramTypes);
    }

    private static Class resolveClass(Object obj) throws Exception {
        if (obj instanceof Class)
            return (Class) obj;
        else if (obj instanceof String)
            return classloader.loadClass((String) obj);
        else
            return obj.getClass();
    }
}

Related

  1. getMethod(Object obj, String name, Class... types)
  2. getMethod(Object object, String methodName, Class... arguments)
  3. getMethod(Object object, String name, Object... params)
  4. getMethod(Object oo, String mname)
  5. getMethod(Object pojo, String methodName, Class[] params)
  6. getMethod(Object target, String methodName, Class... parameterTypes)
  7. getMethod(Object target, String signature)
  8. getMethod(String methodName, Object instance)
  9. getMethodAnnotatedWith(final Class type, final Class annotation, String fieldName, Object fieldValue)