Hello everyone, Xiaole continues to the previous episode: lebyte Java reflection: Reflection concept and access to reflection source Class
This time it's two: instantiating objects, interfaces and parents, modifiers, and properties
One: instanced object
We explained how to create objects before, including new, clone, deserialization, and another way to instantiate objects using newInstance() or constructor according to Class objects. Call the following api
//Get the source Class<?> clz = Class.forName("com.shsxt.ref.simple.User"); //First: create objects through newInstance(). User user=(User)clz.newInstance(); user.setUname("sxt"); user.setUpwd("good"); //Second, create objects through getDeclaredConstructors() and get all constructors (note the order) Constructor<?>[] cons=clz.getDeclaredConstructors(); for(Constructor<?>c:cons){ System.out.println(c); } //Pay attention to the above output results, and then instantiate them. Otherwise, the parameters are prone to error. User u1=(User)cons[0].newInstance("shsxt","good"); User u2=(User)cons[1].newInstance("sxt"); User u3=(User)cons[2].newInstance(); System.out.println(u1.getUname()+u1.getUpwd());
Note: newInstance() is a call to an empty construct. If the empty construct does not exist, an exception will occur. It can be seen that using other constructors to create objects is cumbersome, and using empty constructs is very simple. Make sure that the empty construct exists.
Interface and parent class
Get interface and parent class through api
//Get the source Class<?> clz =Class.forName("com.shsxt.ref.simple.User"); //Get all interfaces Class<?>[] inters=clz.getInterfaces(); for(Class<?> in:inters){ System.out.println(in.getName()); } //Get parent class Class<?> cls=clz.getSuperclass(); System.out.println("The inherited parent class is:"+cls.getName());
III. modifier
Get the Modifier. Use the Modifier.
Class<?>clz=Class.forName("com.shsxt.ref.simple.User"); //Get modifier int n=clz.getModifiers(); //Use the Modifier to convert to the corresponding string System.out.println(Modifier.toString(n));
IV. properties
Get all properties (including parent class or interface), and use Field to operate
Class<?> clz = Class.forName("com.shsxt.ref.simple.User"); //get attribute System.out.println("===============Attributes of this class=========="); // Get all properties of this class Field[] field = clz.getDeclaredFields(); for (int i = 0; i < field.length; i++) { // 1. Permission modifier int mo = field[i].getModifiers(); String vis = Modifier.toString(mo); // 2. Attribute type Class<?> type = field[i].getType(); //3, name String name = field[i].getName(); System.out.println(vis + " " + type.getName() + " "+ name + ";"); } System.out.println("=========Exposed properties include interface or parent class properties======"); field = clz.getFields(); for (int i = 0; i < field.length; i++) { System.out.println(field [i]); }
Original, reprint please indicate the source
Java reflection mechanism will continue to be discussed later. Welcome to lebyte