1. The concept of the Java reflection mechanism
2. How to instantiate a class object
Class.forName (package name. class name);
Object. GetClass ();
Classes. class;
============================ Code ===================================
Package org.liys.getclass;
Class y{
};
public class getclass02{
public static void Main (String args[]) {
Class<?> C1 = null;
class<?> C2 = null;
Class<?> C3 = null;
An instantiated form commonly used in daily development
try{
C1 = Class.forName ("Org.liys.getclass.y");
}catch (ClassNotFoundException e) {}
C2 = new Y (). GetClass (); Instantiated by methods in the object class
C3 = Y.class;//through class. Class instantiation
System.out.println (C1.getname ());
System.out.println (C2.getname ());
System.out.println (C3.getname ());
}
}
Use of the 3.Class class
Use the class newinstance to replace the general new operation.
However, it is necessary to ensure that, in the declaration of the class, there is a need for a parameterless constructor.
.....
Class<?> c = null;
c = Class.forName (package name. class name);
Instantiated object of class = (Class) c.newinstatnce (); (Object downward transformation)
Object. SetName (), object. Setage ();
......
It is rare to instantiate with a class with a parameter constructor, but if there is one, it can be solved with constructor.
Use GetConstructors to get all constructors for the class cons[] and call cons[0],cons[1] with the subscript.
=========================== Code =============================
Package org.liys.newinstance;
Import Java.lang.reflect.Constructor;
Class person{
private String name;
private int age;
Public person (String Name,int age) {
THIS.name = name;
This.age = age;
}
public void SetName (String name) {
THIS.name = name;
}
public void Setage (int.) {
This.age = age;
}
Public String GetName () {
return this.name;
}
public int getage () {
return this.age;
}
Public String toString () {
Return "Name:" + THIS.name + "Age:" + this.age;
}
}
public class getperson{
public static void Main (String args[]) {
Class<?> c = null;
try{
c = Class.forName ("Org.liys.newinstance.Person");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
person p = null;
constructor<?> cons[] = null;
Cons = C.getconstructors ();
try{
p = (person) cons[0].newinstance ("Ms. Li", 30);
}catch (Exception e) {
E.printstacktrace ();
}
P.setname ("Ms. Li");
P.setage (30);
SYSTEM.OUT.PRINTLN (P);
}
}
4. Get the structure of the class (. Class)
The most important thing is to get the object of the class, class<?> C1 = Class.forName (registration. class name);
A. Getting the parent class of a class gets the parent class from the Java Doc query class
class<?> C2 = C1.getsuperclass (); ..... c2.getname ();
B. Get all the methods in the class, including the parameters of the method, throw exceptions, return values, etc.
All methods, including methods inherited from the parent class, are obtained through the GetMethods () method in class
Or through a method in class getDeclaredMethods()
, just get the method defined in the class.
And then according to the method class to find out how to get the parameters of the function, return value, permission range, throw exception, etc.
Code =========================================================================
Package org.lxh.demo15;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Modifier;
public class Getmethoddemo {
public static void Main (String args[]) {
Class<?> C1 = null;
try{
C1 = Class.forName ("Org.lxh.demo15.Person"); Get Class object
}catch (ClassNotFoundException e) {
E.printstacktrace ();
}
method[] m = C1.getmethods (); Get all the methods in the class
String mod = null;
String parm = "";
String Except = "";
for (int i=0; i<m.length;i++) {
Parm = "";
Except = "";
MoD = modifier.tostring (M[i].getmodifiers ()); Get permission access scope for a method
Class<?> RetType = M[i].getreturntype (); Gets the return value of the method
Class<?>[] p = m[i].getparametertypes (); Get the parameters in a method
for (int j= 0;j<p.length;j++) {
Parm = Parm + p[j].getname () + "arg[" +j+ "]";
if (J < (p.length-1)) {
Parm = Parm + ",";
}
}
class<?>[] Ex = M[i].getexceptiontypes (); Gets the exception that the method throws, one is the class type
if (Ex.length > 0) {
Except = "throws";
for (int j= 0;j<ex.length;j++) {
Except = Except + ex[j].getname ();
if (J < (ex.length-1)) {
Except = Except + ",";
}
}
}
SYSTEM.OUT.PRINTLN (mod + "+rettype+" "+ m[i].getname () +" ("+parm+") "+" "+except";
}
}
}
C. Obtaining an Implemented interface class
........
class<?> c[] = c1.getinterfaces (); Returns all interfaces implemented as an array
for (int i=0;i<c.length;i++) {
SYSTEM.OUT.PRINTLN ("Implemented Interface name:" + c[i].getname ());//Output Interface name
}
........
D. Getting the properties of a class
Field f[] = C1.getdeclaredfields (); //Get the properties in this class
Field f[] = C1.getfields (); ///Get public properties in this class , including those inherited from the parent class or the implemented interface class.
.......
Field f[] = C1.getfields (); Get the public properties in this class
for (int i=0;i<f.length;i++) {
class<?> r = F[i].gettype ();//Get attribute type
int mo = F[i].getmodifiers ();//Get modifier numbers
String priv = modifier.tostring (MO); Restore modifier
System.out.print (Priv + "");
System.out.print (R.getname () + "");//Get attribute type
System.out.print (F[i].getname ());//Output Property name
System.out.println (";");
}
..........
E. Getting all the constructor methods for a class
constructor<?> con[] = c1.getconstructors ();
After the same method, find the parameters of the constructor, access the scope of permissions.
JAVA Learning Note-reflection mechanism