Java:运行时方法解析 [英] Java: runtime method resolution

查看:126
本文介绍了Java:运行时方法解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过解释器进行一些动态的代码调用,我正在进入方法解析的棘手方面,如 JLS第15.12节

I'm working on some dynamic invocation of code via an interpreter, and I'm getting into the sticky ugly areas of method resolution as discussed in JLS section 15.12.

简单的方式选择一个方法就是当你知道所有参数的确切类型时,你可以使用 Class.getDeclaredMethod(String name,Class [] parameterTypes) 。也许你必须检查方法可访问性和类的超类/超接口。

The "easy" way to choose a method is when you know the exact types of all the arguments, at which point you can use Class.getDeclaredMethod(String name, Class[] parameterTypes). Maybe you have to check method accessibility and the class's superclasses/superinterfaces.

但是这不包括以下任何一种情况,所以它有点无用:

But this doesn't cover any of the following cases, so it's kind of useless:


  • 装箱/拆箱原语

  • 子类型

  • varargs

  • 一个null参数(可以是任何类型,除非解释器另有说明;在编译时,任何歧义都会通过将null转换为类/接口来消除)

  • 原始类型转换(不是Java的一部分,但在语言环境中是允许的 - 例如Rhino Javascript,其中所有数字都是浮点数,因此Java代码可能需要 int 但调用者传入的数字是 int double

  • boxing/unboxing primitives
  • subtypes
  • varargs
  • a null argument (which can be any type, unless the interpreter knows otherwise; at compile-time any ambiguity would be eliminating by casting null to a class/interface)
  • primitive type conversion (not part of Java, but allowable in the context of languages -- e.g. Rhino Javascript where all numbers are floating-point, so the Java code might take an int but the caller passes in a number which is either an int or a double)

(参见下面的前三个快速示例)

(see below for a quick example of the first three)

所以现在我必须编写我自己的方法解析库...

So now I have to write my own method resolution library...

有没有任何知名的框架图书馆有助于此吗?

package com.example.test.reflect;

import java.lang.reflect.Method;

public class MethodResolutionTest {
    public void compute(int i)              { /* implementation... */ }
    public void compute(Long l)             { /* implementation... */ }
    public void compute(Object obj)         { /* implementation... */ }
    public void compute(String... strings)  { /* implementation... */ }

    public static void main(String[] args) {
        Class<?> cl = MethodResolutionTest.class;

        /* these succeed */
        findAndPrintMethod(cl, "compute", int.class);
        findAndPrintMethod(cl, "compute", Long.class);
        findAndPrintMethod(cl, "compute", Object.class);
        findAndPrintMethod(cl, "compute", String[].class);

        /* these fail */
        findAndPrintMethod(cl, "compute", Integer.class);
        findAndPrintMethod(cl, "compute", long.class);
        findAndPrintMethod(cl, "compute", MethodResolutionTest.class);
        findAndPrintMethod(cl, "compute", String.class, String.class);
    }
    private static void findAndPrintMethod(Class<?> objectClass, 
            String methodName, Class<?>... parameterTypes) 
    {
        try {
            Method method = findMethod(objectClass, methodName, 
                   parameterTypes);
            System.out.println(method.toString());
        }
        catch (SecurityException e) {
            e.printStackTrace();
        }
        catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    private static Method findMethod(Class<?> objectClass, 
            String methodName, Class<?>[] parameterTypes) 
        throws SecurityException, NoSuchMethodException 
    {
        return objectClass.getDeclaredMethod(methodName, parameterTypes);
    }
}


推荐答案

你可能想阅读此博客文章并查看 ClassMate 。它应该为你做大部分蹩脚的工作。

You may want to read this blog post and check out ClassMate. It should do most of the grungy work for you.

这篇关于Java:运行时方法解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆