java, reflection , innerclass,

一个人想着一个人 提交于 2019-12-01 20:03:44

Use this:

public class MyReflection {

public static void main(String[] args) throws ClassNotFoundException,
        InstantiationException, IllegalAccessException,
        NoSuchMethodException, SecurityException, IllegalArgumentException,
        InvocationTargetException {
    Class outer = Class.forName("reflaction.MyReflection");
    Object outerInstance = outer.newInstance();

    Class<?> inner = Class
            .forName("reflaction.MyReflection$TestReflection");
    Constructor<?> constructor = inner.getDeclaredConstructor(outer);

    TestReflection innerInstance = (TestReflection) constructor
            .newInstance(outerInstance);

    innerInstance.demo();
}

class TestReflection {

    public void demo() {
        System.out.println("reflection occurs");
    }
}

Have a look at the Javadoc of getDeclaredConstructor(Class<?>... parameterTypes). It says:

... 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.

So, giving the enclosing instance as first parameter will create a new instance of the inner class:

    TestReflection innerInstance = (TestReflection) constructor
            .newInstance(outerInstance);

Because TestReflection is an inner class it can only exist within an instance of the outer class TestReflection. Thus you have to provide an instance of TestReflection when instantiating the inner class.

Use this:

public class MyReflection {

    public static void main(String[] args) throws Exception {
        Class<?> testReflectionClass = Class
                .forName("reflection.MyReflection$TestReflection");
        MyReflection myReflection = new MyReflection();
        Object newInstance = testReflectionClass.getDeclaredConstructor(
                MyReflection.class).newInstance(myReflection);
        TestReflection testReflection = (TestReflection) newInstance;
        testReflection.demo();
    }

    class TestReflection {

        public void demo() {
            System.out.println("reflection occurs");
        }
    }

}

Firstly, the non-static inner class can only be instantiated using the outer class instance, like

Outer outer = new Outer();
Inner inner = outer.new Inner();

Secondly - as far as i know, java reflection api does not provide an ability to instantiate the non-static inner classes. It is possible only for static ones. So if you dont need the inner class to be dynamic and referenced with outer instance - just add a 'static' modifier for inner class and your code will work.

set TestReflection class static, like:

package reflaction;

public class MyReflection {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException  {

    Class<?> obj = Class.forName("reflaction.MyReflection$TestReflection");
    TestReflection a = (TestReflection) obj.newInstance();
    a.demo();
}

static class TestReflection {

    public void demo(){
        System.out.println("reflection occurs");
    }
}
}

The other way is to create MyReflection instance aka: Class.forName("reflaction.MyReflection").newInstance(); and after from that instance load "reflaction.MyReflection$TestReflection"

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