Java reflection get constructor from class name failed [closed]

时光毁灭记忆、已成空白 提交于 2019-12-10 20:17:42

问题


I've this code snippet:

class You{
    public You(String s){}
}

public static void main(String[] args)  throws NoSuchMethodException {
    Constructor[] constructors = You.class.getConstructors();
    for(Constructor constructor: constructors){
        Class[] parameterTypes = constructor.getParameterTypes();
        for(Class c: parameterTypes){
            System.out.println(c.getName());//print java.lang.String
        }
    }
    Constructor constructor =
            You.class.getConstructor(String.class);//NoSuchMethodException?
}

This is quite old, when I print the constructors, it has one constructor with java.lang.String as parameter. But when I tried to You.class.getConstructor it says no constructor with String.class as parameter.

I'm using java1.8 on mac. Would you help to explain and how to fix?

Thanks a lot.


回答1:


I'll delete this answer if it's not the case, but it looks like You is an inner class, and I suspect your actual code looks something like this:

public class Foo {

    class You {
        public You(String s){}
    }

    public static void main(String[] args)  throws NoSuchMethodException {
        Constructor[] constructors = You.class.getConstructors();

        for (Constructor constructor: constructors) {
            Class[] parameterTypes = constructor.getParameterTypes();

            for (Class c: parameterTypes){
                System.out.println(c.getName());//print java.lang.String
            }
        }

        Constructor constructor =
            You.class.getConstructor(String.class);//NoSuchMethodException?
    }
}

From the documentation of Class#getConstructor:

If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter.

For this reason, you'll have to use the following instead:

Constructor constructor = You.class.getConstructor(Foo.class, String.class);

But obviously replace Foo with the name of your enclosing class.




回答2:


If You is really an inner class then you need to get its constructor a little bit differently.

Here is the Javadoc snippet from Class.getConstructor

If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter.

This means that you need to also pass enclosing class to the Class.getConstructor call.

Here is an example (includes how to create an instance of inner class from its Constructor):

public class Test {
    class You{
        public You(String s){}
    }

    public static void main(String[] args)  throws NoSuchMethodException {
        Constructor constructor =
                You.class.getConstructor(Test.class, String.class);//NoSuchMethodException?

        try {
            You you = constructor.newInstance(new Test(), "");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



回答3:


getConstructors() returns visable constructors. Try getDeclaredConstructors() or make the class public.



来源:https://stackoverflow.com/questions/53229027/java-reflection-get-constructor-from-class-name-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!