我可以在Java中获取方法参数列表吗?

时间:2018-02-19 23:35:53

标签: java reflection

我正在编写像这样的 ton 样板文件。我觉得必须有更好的方法。我正在寻找的是从方法调用中自动获取新的Object []。像Reflection.getCurrentMethodCall()。getArgumentsAsList()。toArray()之类的东西,但我不确定我应该寻找哪些关键字

谢谢!

[ ... 50 identical methods ... ]
                @Override
            public int get_ProxyFactoryMapping(PointerByReference factoryMapping) {
                Function f = Function.getFunction(vTable[48], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), factoryMapping});
            }

            @Override
            public int GetPropertyProgrammaticName(int property, PointerByReference name) {
                Function f = Function.getFunction(vTable[49], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), property, name});
            }

            @Override
            public int GetPatternProgrammaticName(int pattern, PointerByReference name) {
                Function f = Function.getFunction(vTable[50], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), pattern, name});
            }
            @Override
            public int GetPatternProgrammaticName(0..n arguments) {
                Function f = Function.getFunction(vTable[running index], Function.ALT_CONVENTION);
                return f.invokeInt(new Object[]{getInterface(), the same arguments as this object was called with});
            }
[ ... 150 identical methods ... ]

1 个答案:

答案 0 :(得分:2)

您是否尝试过java.lang.reflect.Proxyjava.lang.reflect.InvocationHandler

基本上你实现了InvocationHandler接口,它有一个方法invoke可以为你提供你想要的东西:

Object invoke(Object proxy, // the proxy object
              Method method, // the method called
              Object[] args) // the argument list
       throws Throwable

然后将处理程序的实例传递给Proxy类以获取代理对象(从Java Doc复制的代码):

Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(),
                                      new Class[] { Foo.class },
                                      handler);

Foo是一个接口。