java非静态内部类getConstructor抛出异常问题

           最近遇到一个问题,java非静态的内部类,在调用到java.lang.Class.getConstructor(Class<?>...)这个方法时,抛出了异常NoSuchMethodException,这是为什么呢?

          仔细看java.lang.Class.getConstructor(Class<?>...)这个方法的注释,如下:

 

    /**
     * Returns a <code>Constructor</code> object that reflects the specified
     * public constructor of the class represented by this <code>Class</code>
     * object. The <code>parameterTypes</code> parameter is an array of
     * <code>Class</code> objects that identify the constructor's formal
     * parameter types, in declared order.  
     *
     * If this <code>Class</code> object represents an inner class
     * declared in a non-static context, the formal parameter types
     * include the explicit enclosing instance as the first parameter.
     *
     * <p> The constructor to reflect is the public constructor of the class
     * represented by this <code>Class</code> object whose formal parameter
     * types match those specified by <code>parameterTypes</code>.
     *
     * @param parameterTypes the parameter array
     * @return the <code>Constructor</code> object of the public constructor that
     * matches the specified <code>parameterTypes</code>
     * @exception NoSuchMethodException if a matching method is not found.
     * @exception  SecurityException
     */

          这个方法的用途,是返回一个 Constructor 对象,它反映此 Class 对象所表示的类的指定公共构造方法。对于非静态上下文中声明的内部类,应该把外部包装类作为第一参数。也就是说虽然我们想要得到的是内部类的构造方法,但实际传入的应该是外部类的类型作为参数,否则会抛出了NoSuchMethodException异常。我们可以写代码测试一下。

 

示例一:传入空参数

package mytest;
import java.lang.reflect.Constructor;
public class ClassTest {
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        Constructor<InnerClass> constructor = InnerClass.class.getConstructor();
        System.out.println(constructor.getName());
    }
    public class InnerClass {
        public String name;
    }
}
运行结果:
Exception in thread "main" java.lang.NoSuchMethodException: mytest.ClassTest$InnerClass.<init>()
	at java.lang.Class.getConstructor0(Class.java:2715)
	at java.lang.Class.getConstructor(Class.java:1659)
	at mytest.ClassTest.main(ClassTest.java:8)
 
示例二:传入内部类作为参数
package mytest;
import java.lang.reflect.Constructor;
public class ClassTest {
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        Constructor<InnerClass> constructor = InnerClass.class.getConstructor(InnerClass.class);
        System.out.println(constructor.getName());
    }
    public class InnerClass {
        public String name;
    }
}
 运行结果:
Exception in thread "main" java.lang.NoSuchMethodException: mytest.ClassTest$InnerClass.<init>(mytest.ClassTest$InnerClass)
	at java.lang.Class.getConstructor0(Class.java:2715)
	at java.lang.Class.getConstructor(Class.java:1659)
	at mytest.ClassTest.main(ClassTest.java:8)
 
示例三:传入外部类作为参数
package mytest;
import java.lang.reflect.Constructor;
public class ClassTest {
    public static void main(String[] args) throws SecurityException, NoSuchMethodException {
        Constructor<InnerClass> constructor = InnerClass.class.getConstructor(ClassTest.class);
        System.out.println(constructor.getName());
    }
    public class InnerClass {
        public String name;
    }
}
 运行结果:
mytest.ClassTest$InnerClass
 

 

           由此可见,对于非静态内部类,想要得到Constructor,需要传入外部类作为参数。但是其中的实现原理是怎样的,还需要读源代码深入研究。

 

展开阅读全文

没有更多推荐了,返回首页