Java:反射调用实现公共接口的非公共类中的方法

发布于 2024-11-07 02:08:11 字数 568 浏览 1 评论 0原文

我尝试使用反射来调用名称和参数在运行时已知的方法,但失败并出现 IllegalAccessException

这是一个对象,该对象是实现公共接口的非公共类的实例,我在试图记住调用此类方法的正确方法时感到脑抽筋。

public interface Foo
{
    public int getFooValue();
}

class FooImpl implements Foo
{
    @Override public int getFooValue() { return 42; }
}

Object foo = new FooImpl();

给定 foo 对象,我如何反射性地调用 foo.getFooValue() ?

如果我查看 foo.getClass().getMethods() 的结果,这应该可以工作,但我认为它会导致 IllegalAccessException 这是我必须调用的情况吗getDeclaredMethods()?或者我是否必须遍历公共接口/超类并在那里调用 getDeclaredMethods ?

I'm trying to use reflection to invoke a method whose name and arguments are known at runtime, and I'm failing with an IllegalAccessException.

This is on an object that is an instance of a nonpublic class which implements a public interface, and I've got a brain cramp trying to remember the right way to invoke such a method.

public interface Foo
{
    public int getFooValue();
}

class FooImpl implements Foo
{
    @Override public int getFooValue() { return 42; }
}

Object foo = new FooImpl();

Given the foo object, how would I call foo.getFooValue() reflectively?

If I look through the results of foo.getClass().getMethods(), this should work but I think it causes the IllegalAccessException Is this a case where I have to call getDeclaredMethods()? Or do I have to walk through the public interfaces/superclasses and call getDeclaredMethods there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-14 02:08:11

这有效:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Ex
{
    public static void main(String[] args) throws Exception
    {
        final String methodName = "getFooValue";
        Object foo = new FooImpl();
        Class<?> c = foo.getClass();
        Method m = c.getDeclaredMethod(methodName, null);
        System.out.println(m.invoke(foo));
    }
}

interface Foo
{
    public int getFooValue();
}

class FooImpl implements Foo
{
    @Override public int getFooValue() { return 49; }
}

This works:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Ex
{
    public static void main(String[] args) throws Exception
    {
        final String methodName = "getFooValue";
        Object foo = new FooImpl();
        Class<?> c = foo.getClass();
        Method m = c.getDeclaredMethod(methodName, null);
        System.out.println(m.invoke(foo));
    }
}

interface Foo
{
    public int getFooValue();
}

class FooImpl implements Foo
{
    @Override public int getFooValue() { return 49; }
}
柠檬心 2024-11-14 02:08:11

我认为你应该调用 getDeclaredMethods()。

这里是一个示例:

Method methods[] = secretClass.getDeclaredMethods(); 
System.out.println("Access all the methods"); 
for (int i = 0; i < methods.length; i++) { 
   System.out.println("Method Name: " + methods[i].getName());
   System.out.println("Return type: " + methods[i].getReturnType());
   methods[i].setAccessible(true);
   System.out.println(methods[i].invoke(instance, EMPTY) + "\n");
}

顺便说一句,有一篇文章引用了 '<一个href="https://stackoverflow.com/questions/5951197/accessing-private-inner-class-in-the-same-package/5952096#5952096">私有类反射':

当涉及到字节码(即运行时)时,不存在私有类这样的东西。这是编译器维护的虚构。对于反射 API,有一个具有公共成员方法的包可访问类型。

I think that you should call the getDeclaredMethods().

Here's an example:

Method methods[] = secretClass.getDeclaredMethods(); 
System.out.println("Access all the methods"); 
for (int i = 0; i < methods.length; i++) { 
   System.out.println("Method Name: " + methods[i].getName());
   System.out.println("Return type: " + methods[i].getReturnType());
   methods[i].setAccessible(true);
   System.out.println(methods[i].invoke(instance, EMPTY) + "\n");
}

By the way, a post refering to 'private classes reflection':

When it comes to bytecode (i.e. runtime) there is no such thing as a private class. This is a fiction maintained by the compiler. To the reflection API, there's a package-accessible type with a public member method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文