Java 反射,getMethod()

发布于 2024-12-19 09:28:18 字数 1037 浏览 1 评论 0原文

我正在研究 Java 反射的基础知识并观察类方法的信息。我需要获取一个与 getMethod() 函数所描述的规范相匹配的方法。然而,当我这样做时,我得到一个 NoSuchMethodException,我希望你能告诉我为什么我的实现不正确。

static void methodInfo2(String className) throws ClassNotFoundException, 

NoSuchMethodException{

        Class cls = null;
        try{
            cls = Class.forName(className);
        } catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        System.out.println("Cls:  "+cls);


        Method method1 = cls.getMethod("test", null);
        System.out.println("method1:  "+method1);


    }

EDIT1:当我打印出“Cls:”+cls时,输出是“Cls:class a8.myclass2”。为什么它附加类部分? (a8是正确的,所以不用担心) /EDIT1

这是我用来从我的主函数中读取类的函数,然后我想要带有参数“test”和null的getMethod(),其中“test”是方法的名称,null 表示该方法没有参数。我正在阅读的类称为 myclass2,如下所示:

package a8;

public class myclass2 {

    void test(){
        //"takes no parameters"
        //"returns bool"
        //"name starts with test"
        //return true;
    }

}

正如您所看到的,该方法确实存在于该类中。如果您能指出我的错误,我将不胜感激。

I'm working with the basics of Java reflection and observing information on methods of classes. I need to get a method that matches specifications as described by the getMethod() function. However, when I do this I get a NoSuchMethodException, and I was hoping you could tell me why my implementation is incorrect.

static void methodInfo2(String className) throws ClassNotFoundException, 

NoSuchMethodException{

        Class cls = null;
        try{
            cls = Class.forName(className);
        } catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        System.out.println("Cls:  "+cls);


        Method method1 = cls.getMethod("test", null);
        System.out.println("method1:  "+method1);


    }

EDIT1:When I print out "Cls: "+cls, the output is "Cls: class a8.myclass2". Why does it append the class part? (the a8 is correct, so don't worry about that) /EDIT1

This is the function I use to read in a class from my main function, and then I want to getMethod() with the parameters "test" and null, where "test" is the name of the method and null means the method has no parameters. The class I am reading in is called myclass2 which is here:

package a8;

public class myclass2 {

    void test(){
        //"takes no parameters"
        //"returns bool"
        //"name starts with test"
        //return true;
    }

}

As you can see, the method does infact exist in the class. If you could point out my mistake, I would really appreciate it.

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

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

发布评论

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

评论(2

凉栀 2024-12-26 09:28:18

公开您的测试方法。我相信 Class.getMethod() 仅限于公共方法。

Make your test method public. I believe Class.getMethod() is limited to public methods.

So要识趣 2024-12-26 09:28:18

如果您没有发布确切的异常和输出,则很难判断,但我怀疑这是因为这些类位于两个单独的包中,并且由于方法的默认修饰符只是受保护的,所以它会失败。

使用 getDeclaredMethod() 获取通常不可见的方法。

Without you posting the exact exception and your output, its hard to tell, but I suspect it is because the classes are in two separate packages, and since the default modifiers for a method are just protected it fails.

Use getDeclaredMethod() to get a method that isn't normally visible.

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