Reflection in Java: How to construct a load class
Package Com.ma.reflection;import Java.lang.reflect.constructor;import Java.lang.reflect.InvocationTargetException Import Org.junit.test;import com.ma.bean.userbean;/** * Reflection: Load Construction Method * @author MA * */public class Demo2 {/** * Reflection: The parameterless constructor of the load class Method */@Testpublic void test1 () {//1. Called with the Class.forName ("Class name") method; try {//Reflection class Class<?> Clazz = Class.forName (" Com.ma.bean.UserBean ");//Gets the constructor of the class constructor<?> C = Clazz.getconstructor (null); UserBean user = (UserBean) c.newinstance (null); SYSTEM.OUT.PRINTLN (user);} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SecurityException e) {e.printstacktrace ();} catch ( Nosuchmethodexception e) {e.printstacktrace (),} catch (IllegalArgumentException e) {e.printstacktrace ();} catch ( Instantiationexception e) {e.printstacktrace (),} catch (Illegalaccessexception e) {e.printstacktrace ();} catch ( InvocationTargetException e) {e.printstacktrace ();}} /** * Reflection: The method of constructing the loaded class with a parameter * @throws classnotfoundexception * @throws invocationtargetexception * @throws IllegaLaccessexception * @throws instantiationexception * @throws illegalargumentexception * @throws nosuchmethodexception * @throws SecurityException */@Testpublic void Test2 () throws ClassNotFoundException, IllegalArgumentException, Instantiationexception, Illegalaccessexception, InvocationTargetException, SecurityException, Nosuchmethodexception {//Reflection class Class<?> Clazz = Class.forName ("Com.ma.bean.UserBean"); constructor<?> C = clazz.getconstructor (String.class,string.class,string.class); UserBean user = (UserBean) c.newinstance ("001", "John Doe", "123abc"); SYSTEM.OUT.PRINTLN (user);} /** * Reflection: The private and constructive method of loading class * @throws classnotfoundexception * @throws nosuchmethodexception * @throws SecurityException * @t Hrows invocationtargetexception * @throws illegalaccessexception * @throws instantiationexception * @throws Illegalargu Mentexception */@Testpublic void Test3 () throws ClassNotFoundException, SecurityException, Nosuchmethodexception, IllegalArgumentException, Instantiationexception,Illegalaccessexception, invocationtargetexception {class<?> clazz = Class.forName ("Com.ma.bean.UserBean"); constructor<?> C = clazz.getdeclaredconstructor (String.class); c.setaccessible (true); UserBean user = (UserBean) c.newinstance ("Harry"); SYSTEM.OUT.PRINTLN (user);} /** * * Another method of non-parametric reflection * @throws classnotfoundexception * @throws illegalaccessexception * @throws instantiationexception * /@Testpublic void Test4 () throws ClassNotFoundException, Instantiationexception, illegalaccessexception {class<? > clazz = Class.forName ("Com.ma.bean.UserBean"); UserBean user = (UserBean) clazz.newinstance (); SYSTEM.OUT.PRINTLN (user);}}
Entity class
Package com.ma.bean;/** * UserBean entity class * @author MA * */public class UserBean {private string Id;private string name = "Zhang San" ;p rivate String password;public UserBean () {super (); System.out.println ("I am a non-parametric construction method");} Public UserBean (string ID, string name, string password) {super (); this.id = Id;this.name = Name;this.password = Password;s Ystem.out.println ("I am a constructive method");} Public String GetId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} @Overridepublic String toString () {return "UserBean [id=" + ID + ", name=" + name + ", password=" + password + "]";} Private UserBean (String name) {this.name = name; SYSTEM.OUT.PRINTLN (name);} }
Reflection in Java: How to construct a load class