反思地实例化Java本地类

时间:2013-06-21 13:50:02

标签: java reflection

在Java中,有没有办法或黑客反复实例化local class

这是我的测试类:

package com.example.localclass;

import org.junit.Test;

public class LocalClassInstantiationTest {

    @Test
    public void testInstantiateLocalClassReflectively() {
        System.out.println("About to define local class");
        class LocalClass {
            void print() {
                System.out.println("Hello world");
            }
        }

        System.out.println("--------------------Direct instantiation------------------------");
        LocalClass localClassInstance;
        localClassInstance = new LocalClass();
        localClassInstance.print();

        System.out.println("--------------------Reflective instantiation--------------------");
        Class<LocalClass> localClazz = LocalClass.class;
        System.out.println("Local class name: " + localClazz.getName() );
        System.out.println("Local class canonical name: " + localClazz.getCanonicalName() );
        System.out.println("Local class constructors array length: " + localClazz.getConstructors().length);
        System.out.println("About to create local class instance reflectively");

        try {
            localClassInstance = localClazz.newInstance();
            localClassInstance.print();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }

}

当我运行它时,我得到以下结果:

About to define local class
--------------------Direct instantiation------------------------
Hello world
--------------------Reflective instantiation--------------------
Local class name: com.example.localclass.LocalClassInstantiationTest$1LocalClass
Local class canonical name: null
Local class constructors array length: 0
About to create local class instance reflectively
java.lang.InstantiationException: com.example.localclass.LocalClassInstantiationTest$1LocalClass
    at java.lang.Class.newInstance0(Class.java:357)
    at java.lang.Class.newInstance(Class.java:325)
    at com.example.localclass.LocalClassInstantiationTest.testInstantiateLocalClassReflectively(LocalClassInstantiationTest.java:32)

1 个答案:

答案 0 :(得分:4)

在您的情况下,问题是您的类没有空构造函数。 newInstance()假设您有一个,如果不这样做就会失败。

如果您在静态方法(例如main)中运行此方法,则不会出现此错误。

这是因为你有一个非静态匿名类,无论你是否使用它,都隐式地要求引用你的外部类LocalClassInstantiationTest。

你可以试试这个。

System.out.println("Local class constructors array length: " + 
                   localClazz.getDeclaredConstructors().length);
System.out.println("About to create local class instance reflectively");

try {
    localClassInstance = localClazz
            .getDeclaredConstructor(LocalClassInstantiationTest.class)
            .newInstance(this);
    localClassInstance.print();
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
    e.printStackTrace();
}

并打印您的程序

About to define local class
--------------------Direct instantiation------------------------
Hello world
--------------------Reflective instantiation--------------------
Local class name: LocalClassInstantiationTest$1LocalClass
Local class canonical name: null
Local class constructors array length: 1
About to create local class instance reflectively
Hello world