Hi i want to get the object of inner class using reflection but i am getting some error in it.
code is:-
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();
}
class TestReflection {
public void demo(){
System.out.println("reflection occurs");
}
}
}
and the error is :--
Exception in thread "main" java.lang.InstantiationException: reflaction.MyReflection$TestReflection
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at reflaction.MyReflection.main(MyReflection.java:10)
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"
来源:https://stackoverflow.com/questions/19094490/java-reflection-innerclass