Loading java class
When you call the java command to run the program, the command to start a java virtual machine process, all the threads will run the program in this process inside the virtual machine. Thread running generated, variables in this process, commonly used memory area of the JVM process.
Class loading procedure
When a program calls a class, the class of class files are read into memory with a storage array, generating a respective class object. At this class is not yet available. Then began to check the class file is correct, then the storage space allocated to the class static variable. The last execution initialization of static objects and the static code block.
If the class is in the parent class. And it has not been loaded, it will be loaded to initialize the parent class. And initialization statements executed first class.
Reflecting
reflection is operating in a class file is loaded into the JVM time. Because the java class is compiled into class files. Therefore, such a reflective acquired class file java classes get all the information.
Namely: java class all the elements, including attributes, methods, will be seen as an object.
How to get the object class of
java is object-oriented language, all things are all objects. That is all the class of an object. Class is the object classes of a package lang: java.lang.Class object.
There are three ways to obtain:
1, to obtain the object by object class
// object is created first class Stu stu = new Stu ();
in the case do not know the name of the class, passing an object, you can use getClass () method to obtain class name
// class = stu.getClass getClass obtain such class ();
2, obtained by the class name
// skip the process object class creates objects getClass = Stu.class class;
. 3, obtained by the class name, but there will be abnormal raised, and the turn require strong type
most commonly
the try {
class getClass = (class) the Class.forName ( "Stu");
} the catch (a ClassNotFoundException E) {
e.printStackTrace ();
}
acquired object class attributes, methods,
this need to call upon the method class :( the following is the content Api)
method provides a single method (and how to access the method) information about the class or interface. The method may reflect a class or instance method (a method including abstract).
Method permits widening conversion real parameter participating in the underlying matching method to be called; if you want to shrink conversion will throw IllegalArgumentException.
First: All the members of this class of methods to get, connected text Class object getClass
getMethods (): class is to get all the public methods.
Method, [] = getclass.getMethods Methods ();
getDeclareMethods (): is obtained as an all methods, regardless of permission. But the parent class inherited methods not included.
Method [] methods = getclass.getDeclareMethods () ;
Inheritance development as little as possible
through the collection: getReturnType (): get the return value type
getName (): get the name of the method
to get the type, method name reacquisition
Class = the returnType Methods [I] .getReturnType ();
String = returnTypeName the returnType. getName ();
Now get the method's return value type, method name, but also the method parameter list, as a method parameter list may have more, so you need to get a set of parameter list, traversing get a single parameter of type
public Class <?> [ ] getParameterTypes ()
parameter type returns an array of Class objects in declaration order, which describes the objects represented by this method object method. If the underlying method takes no parameters, an array of length 0 is returned.
Returns: the parameter type of the method represented by this object (from of Api)
Class[ ] paramTypes = methods[ i ].getParameterTypes();
A: paramTypes [i] .getName (); to give the name of the parameter type.
Parameter class by the parameter name can be provided Java8 parameter name.
Parameter [] parameters = method [i ] .getParameters ();
traversing the name of the parameter set may give a general compiler ---- default parameter names are not compiled, it is possible to obtain the arg0;
// String Type = parameters Parameter Type [I] .getType () toString ();.
// parameter name String name = parameters [i] .getName ();
acquisition member variable
is an object method class member variable is an object of the Field class.
getFields (): get all the public member variables
getDeclaredFields (): get all member variables declared their own, regardless of permission ,, but does not include the parent class inheritance.
Fields [] fields = getclass.getFields () ;
getType (): get the type member variables
getName (): get the member variable name
String name = fields [i] .getType () getName ();.
Gets Constructor
Constructor Constructor object class;
// get a set of constructors supra difference
the Constructor [] = constructors1 class1.getConstructors ();
the Constructor [] = constructors2 class1.getDeclaredConstructors ();
Traverse type obtained:
for (the Constructor constructor: constructors2) {
Class [] = paramTypes Constructor.getParameterTypes ();
for (Class paramType: paramTypes) {
String paramTypeName paramType.getName = ();
}
}
by calling method reflected
acquiring class type, the method then acquires the type of
the try {
Class getClass = (Class) the Class.forName ( "Stu");
} the catch (a ClassNotFoundException E) {
e.printStackTrace ();
}
// the name of the first parameter representation, a method of sequentially later parameter type, not pass without
method, [] = getclass.getMethod methods ( "FunctionName", FunctionType)
methods.function (class object, method parameters)
EG:
class methods
public class Stu {
void the Add public (A int, int B) {return A + B;}
}
get a class object, method of obtaining the calling method
Stu Stu new new STU = ();
the try {
Class getClass = (Class) the Class.forName ( "Stu" );
Method, FUNC = getclass.getMethod ( "the Add", int.class, int.claaa);
func.add (STU, 2,3);
} the catch (a ClassNotFoundException E) {
e.printStackTrace ();
}