Подтвердить что ты не робот

Поиск наиболее специфического перегруженного метода с помощью метода MethodHandle

Предположим, что у меня есть три метода внутри данного типа (класс/интерфейс):

public void foo(Integer integer);
public void foo(Number number);
public void foo(Object object);

Используя 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);

Тогда я концептуально хотел бы выбрать foo(Number number), но выше будет вызывать исключение, поскольку API будет искать только метод foo(Long) и ничего больше. Обратите внимание, что использование Long здесь просто как пример. Тип объекта может быть любым на практике; String, MyBar, Integer,... и т.д. И т.д.

Есть ли что-то в API MethodHandle, которое автоматически и во время выполнения делает то же разрешение, что и компилятор после JLS 15.12?

4b9b3361

Ответ 1

В основном я искал все методы, которые могут быть выполнены с помощью набора параметров. Итак, я отсортировал их на расстоянии между параметромType до методаParameterType. Сделав это, я мог бы получить наиболее специфический перегруженный метод.

Чтобы проверить:

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

    Foo foo = new Foo();

    MethodExecutor.execute(foo, "foo", Void.class, object);
}

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

МетодExecutor:

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

Конечно, его можно улучшить с помощью некоторых рефакторингов и комментариев.

Ответ 2

Я не мог найти способ сделать это с помощью MethodHandle s, но есть интересный java.beans.Statement, который реализует поиск самого специфического метода JLS в соответствии с Javadocs:

Метод 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");

    }
}

Ответ 3

Вы можете использовать MethodFinder.findMethod() для его достижения.

@Test
public void test() throws Exception {
    Foo foo = new Foo();

    Object object = 3L;
    Method method = MethodFinder.findMethod(Foo.class, "foo", object.getClass());
    method.invoke(foo, object);
}


public static 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");
    }
}

Поскольку он находится в корневой библиотеке java, он следует за JLS 15.12.

Ответ 4

Нет, я не видел ничего подобного в API MethodHandle. Аналогичная вещь существует в commons-beanutils как MethodUtils#getMatchingAccessibleMethod, поэтому вам не нужно ее реализовывать.

Он будет выглядеть примерно так:

Object object = getLong();
Method method = MethodUtils.getMatchingAccessibleMethod(this.getClass(), "foo", object.getClass());

Вы можете преобразовать в API MethodHandle или просто использовать Method напрямую:

MethodHandle handle = MethodHandles.lookup().unreflect(method);
handle.invoke(this, object);

Ответ 5

Учитывая ограничения, которые: a), тип параметра известен только во время выполнения, а b) существует только один параметр, простое решение может быть просто идя вверх по иерархии классов и сканируя реализованные интерфейсы, как в следующем примере.

public class FindBestMethodMatch {

    public Method bestMatch(Object obj) throws SecurityException, NoSuchMethodException {
        Class<?> superClss = obj.getClass();
        // First look for an exact match or a match in a superclass
        while(!superClss.getName().equals("java.lang.Object")) {
            try {
                return getClass().getMethod("foo", superClss);          
            } catch (NoSuchMethodException e) {
                superClss = superClss.getSuperclass();
            }
        }
        // Next look for a match in an implemented interface
        for (Class<?> intrface : obj.getClass().getInterfaces()) {
            try {
                return getClass().getMethod("foo", intrface);
            } catch (NoSuchMethodException e) { }           
        }
        // Last pick the method receiving Object as parameter if exists
        try {
            return getClass().getMethod("foo", Object.class);
        } catch (NoSuchMethodException e) { }

        throw new NoSuchMethodException("Method not found");
    }

    // Candidate methods

    public void foo(Map<String,String> map) { System.out.println("executed Map"); } 

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

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

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

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

    // Test if it works
    public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
        FindBestMethodMatch t = new FindBestMethodMatch();
        Object param = new Long(0);
        Method m = t.bestMatch(param);
        System.out.println("matched " + m.getParameterTypes()[0].getName());
        m.invoke(t, param);
        param = new HashMap<String,String>();
        m = t.bestMatch(param);
        m.invoke(t, param);
        System.out.println("matched " + m.getParameterTypes()[0].getName());
    }

}