Saturday, September 4, 2010

Reflection in Java, access to the private constructor, properties, methods

I always thought the private constructor Lei, Zhu Xing, methods, in addition to Class Zi Shen other class is not accessible, Qian Xue Xi Spring Kuangjia just a few days, Zai learn Spring framework based Bao Bean, wrote a simple example, Lei Si as follows: package study.spring.bean; public class SimpleBean (priva ...
I always thought that the private class constructor, property, method, in addition to the class itself is not accessible to other classes, just a few days before Spring Kuangjia Xue Xi, learning Spring frame foundation Zai Bao Bean, wrote a simple example, similar Ruxia:
package study.spring.bean;
public class SimpleBean

(
private String beanName;
private SimpleBean () (
System.out.println ("SimpleBean");
)
/ ** * / / **
* @ Return Returns the beanName.
* /
public String getBeanName ()
(
return beanName;
)
/ ** * / / **
* @ Param beanName The beanName to set.
* /
public void setBeanName (String beanName)
(
this. beanName = beanName;
)
)
Found that actually can call success, was very surprised, reflection usually in previous projects often used, but only a private constructor can not construct the class.
Himself made a simple example:
package study.spring.bean;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class SimpleTest
(
/ ** *//**
* @ Param args
* /
public static void main (String [] args)
(
/ / TODO Auto-generated method stub
try
(
Constructor [] cts = Class.forName ("study.spring.bean.SimpleBean"). GetDeclaredConstructors ();
for (int i = 0; i
cts [i]. newInstance (null);
)
)
catch (SecurityException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
catch (ClassNotFoundException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
catch (IllegalArgumentException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
catch (InstantiationException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
catch (IllegalAccessException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
catch (InvocationTargetException e)
(
/ / TODO Auto-generated catch block
e.printStackTrace ();
)
)
)
Also, as I would like to throw java.lang.IllegalAccessException exception, when it suspected Spring framework using reflection some of the features, then check the relevant documentation to know what the reasons are:
Java reflection effect created in the instance of a class, the default will detect whether the related security, the detection switch can be turned off.
Constructor, Field, Method is inherited from AccessibleObject, the corresponding instance of the call setAccessible (true) to close the switch
If the above example, the code cts [i]. NewInstance (null); line before calling the method: cts [i]. SetAccessible (true);
This can create only instance of the constructor, call the private constructor, private property to access the class.
Oh, so java security seems to be much lower. If you attach great importance to the security application, java, of course take into account this connection, you can increase the JVM startup parameter-Djava.security.manager enabled security manager, if the parameters It will test the code is being shut down access to test whether the permission to do so, the code execution will throw java.security.AccessControlException exception.

No comments:

Post a Comment