get Method Best Match - Java Reflection

Java examples for Reflection:Method

Description

get Method Best Match

Demo Code


//package com.java2s;

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

public class Main {
    private static HashMap<String, Method> sMethodCache = new HashMap<String, Method>();
    private static HashMap<Class<?>, Class<?>> primativeClassMap = new HashMap<Class<?>, Class<?>>();

    private static Method getMethodBestMatch(Class<?> clazz,
            String methodName, Class<?>... argClasses) throws Throwable {
        String methodFullName = genMethodFullName(clazz, methodName,
                argClasses);//ww w. ja va 2 s .c  om
        if (sMethodCache.containsKey(methodFullName))
            return sMethodCache.get(methodFullName);
        for (Method method : clazz.getDeclaredMethods()) {
            if (method.getName().equals(methodName)) {
                Class<?>[] tmpArgs = method.getParameterTypes();
                if (compareArgs(tmpArgs, argClasses)) {
                    method.setAccessible(true);
                    sMethodCache.put(methodFullName, method);
                    return method;
                }
            }
        }
        String msg = "";
        for (Class<?> cls : argClasses) {
            msg += cls.getSimpleName() + ",";
        }
        msg = "Can't get method: " + clazz.getSimpleName() + "."
                + methodName + "(" + msg + ")";
        throw new Exception(msg);
    }

    private static String genMethodFullName(Class<?> clazz,
            String methodName, Class<?>... argClasses) {
        StringBuilder name = new StringBuilder();
        name.append(clazz.getName());
        name.append(":");
        name.append(methodName);
        for (int i = 0; i < argClasses.length; i++) {
            if (i == (argClasses.length - 1)) {
                name.append(argClasses[i].getName());
            } else {
                name.append(argClasses[i].getName());
                name.append(",");
            }
        }
        return name.toString();
    }

    private static boolean compareArgs(Class<?>[] dstArgs,
            Class<?>[] origArgs) {
        if (dstArgs.length != origArgs.length)
            return false;
        for (int i = 0; i < dstArgs.length; i++) {
            if (!isChildClass(dstArgs[i], origArgs[i]))
                return false;
        }
        return true;
    }

    private static boolean isChildClass(Class<?> origClass,
            Class<?> dstClass) {
        if (dstClass == null)
            return true;
        if (origClass.isInterface()) {
            for (Class<?> i : dstClass.getInterfaces()) {
                if (origClass == i)
                    return true;
            }
        }
        if (origClass.isPrimitive()
                && (primativeClassMap.get(origClass) == dstClass))
            return true;
        for (; dstClass != Object.class; dstClass = dstClass
                .getSuperclass()) {
            if (dstClass == origClass)
                return true;
        }
        return false;
    }
}

Related Tutorials