使用MethodHandle查找最具体的重载方法

Arj*_*jms 25 java reflection dynamic methodhandle

假设我在给定类型(类/接口)中有三个方法:

public void foo(Integer integer);
public void foo(Number number);
public void foo(Object object);
Run Code Online (Sandbox Code Playgroud)

使用MethodHandle或反射,我想找到一个对象的最具体的重载方法,该类型的类型仅在运行时已知.即我想在运行时执行JLS 15.12.

例如,假设我在上述类型的方法中包含以下三种方法:

Object object = getLong(); // runtime type is Long *just an example*

MethodHandles.lookup()
             .bind(this, "foo", methodType(Void.class, object.getClass()))
             .invoke(object);
Run Code Online (Sandbox Code Playgroud)

然后我在概念上会想要foo(Number number)被选中,但上面会抛出一个异常,因为API只会寻找一个foo(Long)方法,而不是别的.请注意,Long此处的用法仅作为示例.对象的类型可以是实践中的任何东西; String,MyBar,Integer,...等等

MethodHandle API中是否有一些东西能够在运行时自动执行与JLS 15.12之后的编译器相同的分辨率?

Pau*_*ulo 10

基本上我搜索了所有可以用一组参数执行的方法.所以,我按照parameterType和methodParameterType之间的距离对它们进行了排序.这样做,我可以获得最具体的重载方法.

去测试:

@Test
public void test() throws Throwable {
    Object object = 1;

    Foo foo = new Foo();

    MethodExecutor.execute(foo, "foo", Void.class, object);
}
Run Code Online (Sandbox Code Playgroud)

Foo:

class Foo {
    public void foo(Integer integer) {
        System.out.println("integer");
    }

    public void foo(Number number) {
        System.out.println("number");
    }

    public void foo(Object object) {
        System.out.println("object");
    }
}
Run Code Online (Sandbox Code Playgroud)

MethodExecutor:

public class MethodExecutor{
    private static final Map<Class<?>, Class<?>> equivalentTypeMap = new HashMap<>(18);
    static{
        equivalentTypeMap.put(boolean.class, Boolean.class);
        equivalentTypeMap.put(byte.class, Byte.class);
        equivalentTypeMap.put(char.class, Character.class);
        equivalentTypeMap.put(float.class, Float.class);
        equivalentTypeMap.put(int.class, Integer.class);
        equivalentTypeMap.put(long.class, Long.class);
        equivalentTypeMap.put(short.class, Short.class);
        equivalentTypeMap.put(double.class, Double.class);
        equivalentTypeMap.put(void.class, Void.class);
        equivalentTypeMap.put(Boolean.class, boolean.class);
        equivalentTypeMap.put(Byte.class, byte.class);
        equivalentTypeMap.put(Character.class, char.class);
        equivalentTypeMap.put(Float.class, float.class);
        equivalentTypeMap.put(Integer.class, int.class);
        equivalentTypeMap.put(Long.class, long.class);
        equivalentTypeMap.put(Short.class, short.class);
        equivalentTypeMap.put(Double.class, double.class);
        equivalentTypeMap.put(Void.class, void.class);
    }

    public static <T> T execute(Object instance, String methodName, Class<T> returnType, Object ...parameters) throws InvocationTargetException, IllegalAccessException {
        List<Method> compatiblesMethods = getCompatiblesMethods(instance, methodName, returnType, parameters);
        Method mostSpecificOverloaded = getMostSpecificOverLoaded(compatiblesMethods, parameters);
        //noinspection unchecked
        return (T) mostSpecificOverloaded.invoke(instance, parameters);
    }

    private static List<Method> getCompatiblesMethods(Object instance, String methodName, Class<?> returnType, Object[] parameters) {
        Class<?> clazz = instance.getClass();
        Method[] methods = clazz.getMethods();

        List<Method> compatiblesMethods = new ArrayList<>();

        outerFor:
        for(Method method : methods){
            if(!method.getName().equals(methodName)){
                continue;
            }

            Class<?> methodReturnType = method.getReturnType();
            if(!canBeCast(returnType, methodReturnType)){
                continue;
            }

            Class<?>[] methodParametersType = method.getParameterTypes();
            if(methodParametersType.length != parameters.length){
                continue;
            }

            for(int i = 0; i < methodParametersType.length; i++){
                if(!canBeCast(parameters[i].getClass(), methodParametersType[i])){
                    continue outerFor;
                }
            }

            compatiblesMethods.add(method);
        }

        if(compatiblesMethods.size() == 0){
            throw new IllegalArgumentException("Cannot find method.");
        }

        return compatiblesMethods;
    }

    private static Method getMostSpecificOverLoaded(List<Method> compatiblesMethods, Object[] parameters) {
        Method mostSpecificOverloaded = compatiblesMethods.get(0);
        int lastMethodScore = calculateMethodScore(mostSpecificOverloaded, parameters);

        for(int i = 1; i < compatiblesMethods.size(); i++){
            Method method = compatiblesMethods.get(i);
            int currentMethodScore = calculateMethodScore(method, parameters);
            if(lastMethodScore > currentMethodScore){
                mostSpecificOverloaded = method;
                lastMethodScore = currentMethodScore;
            }
        }

        return mostSpecificOverloaded;
    }

    private static int calculateMethodScore(Method method, Object... parameters){
        int score = 0;

        Class<?>[] methodParametersType = method.getParameterTypes();
        for(int i = 0; i < parameters.length; i++){
            Class<?> methodParameterType = methodParametersType[i];
            if(methodParameterType.isPrimitive()){
                methodParameterType = getEquivalentType(methodParameterType);
            }
            Class<?> parameterType = parameters[i].getClass();

            score += distanceBetweenClasses(parameterType, methodParameterType);
        }

        return score;
    }

    private static int distanceBetweenClasses(Class<?> clazz, Class<?> superClazz){
        return distanceFromObjectClass(clazz) - distanceFromObjectClass(superClazz);
    }

    private static int distanceFromObjectClass(Class<?> clazz){
        int distance = 0;
        while(!clazz.equals(Object.class)){
            distance++;
            clazz = clazz.getSuperclass();
        }

        return distance;
    }

    private static boolean canBeCast(Class<?> fromClass, Class<?> toClass) {
        if (canBeRawCast(fromClass, toClass)) {
            return true;
        }

        Class<?> equivalentFromClass = getEquivalentType(fromClass);
        return equivalentFromClass != null && canBeRawCast(equivalentFromClass, toClass);
    }

    private static boolean canBeRawCast(Class<?> fromClass, Class<?> toClass) {
        return fromClass.equals(toClass) || !toClass.isPrimitive() && toClass.isAssignableFrom(fromClass);
    }

    private static Class<?> getEquivalentType(Class<?> type){
        return equivalentTypeMap.get(type);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,可以通过一些重构和评论来改进它.

  • 这绝对是非常有用的,登陆这个问题的人肯定会想要复制你的代码,但问题实际上是关于MethodHandle API中是否有某些东西可以做这样的事情*没有有效地重新实现JLS 15.12*.答案可能很简单:"不,确实没有".我大多想知道标准API中是否没有这样做我完全没有. (2认同)

man*_*uti 8

我找不到用MethodHandles 做这个的方法,但有一个有趣的java.beans.Statement实现根据Javadocs找到JLS最具体的方法:

execute方法找到一个名称与methodName属性相同的方法,并在目标上调用该方法.当目标的类定义了许多具有给定名称的方法时,实现应该使用Java语言规范(15.11)中指定的算法选择最具体的方法.

要检索Method自身,我们可以使用反射来完成.这是一个有效的例子:

import java.beans.Statement;
import java.lang.reflect.Method;

public class ExecuteMostSpecificExample {
    public static void main(String[] args) throws Exception {
        ExecuteMostSpecificExample e = new ExecuteMostSpecificExample();
        e.process();
    }

    public void process() throws Exception {
        Object object = getLong();
        Statement s = new Statement(this, "foo", new Object[] { object });

        Method findMethod = s.getClass().getDeclaredMethod("getMethod", Class.class,
                                                           String.class, Class[].class);
        findMethod.setAccessible(true);
        Method mostSpecificMethod = (Method) findMethod.invoke(null, this.getClass(),
                                              "foo", new Class[] { object.getClass() });

        mostSpecificMethod.invoke(this, object);
    }

    private Object getLong() {
        return new Long(3L);
    }

    public void foo(Integer integer) {
        System.out.println("Integer");
    }

    public void foo(Number number) {
        System.out.println("Number");
    }

    public void foo(Object object) {
        System.out.println("Object");

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不知道,看起来对我来说是合法的.虽然使用`MethodFinder`表示`Statement.getMethod`方法调用会更清晰,两者都使用内部API(如`sun.misc.Unsafe`,它将被隔离).但是我猜`Statement.execute`是主题入门者正在寻找的东西,它存在于std lib中是令人惊奇的事情.谢谢 (2认同)
  • 这段代码中的异味是使用反射访问私有静态`getMethod`,但是如果用户_only_打算调用该方法,则不需要这样做,只需要在`Statement`上调用`execute()`.宾语. (2认同)
  • 如果使用`java.beans.Expression`,您甚至可以获得调用的结果(使用Statement无法获得) (2认同)