Atitit. Improve api compatibility and enhance java c #. Net php js,
Atitit. Improve api compatibility and enhance java c #. Net php js
1. What is reflection 1
1.1. Main functions provided by reflection: 2
1.2. reflection process: 2
2. required classes in Class reflection: there are not many classes required for Class reflection, they are: Field, Constructor, Method, Class, Object, 2
3. Use of reflection 3
3.1. Visualization 3
3.2. system flexibility and scalability 3
3.3. Json xml serialization 3
3.4. function pointer 3
3.5. Rpc3
4. Common Reflection api4
4.1. Apache common-lang. Reflect 5 Class 4
4.2. java. lang. reflect; 20 classes 4
4.3. C #. Net reflection api4
4.4. Php ReflectionClass ReflectionMethod 4
5. C # java difference Implementation 5
6. Refer to 5
1. What is reflection?
In the runtime environment, can I obtain the attributes and methods of a class based on the class name? Can any running object dynamically call the method of this object? The answer is yes. This kind of dynamic mechanism for retrieving class information and dynamically calling an object is called reflection.
What is reflection:
The concept of reflection was first proposed by Smith in 1982. It mainly refers to the ability of a program to access, detect, and modify its own state or behavior. This concept quickly led to research on application reflectivity in the computer science field. It was first adopted by the design field of programming language, and has made achievements in Lisp and object-oriented aspects. LEAD/LEAD ++, OpenC ++, MetaXa, and OpenJava are reflection-based languages. Recently, reflection mechanisms have been applied to Windows, operating systems, and file systems.
1.1. Main functions provided by reflection:
1. Determine the class to which any object belongs during running
2. Construct a class object at runtime
3. Determine the attributes and methods of a class at runtime.
4. Call the method of an object at runtime
1.2. reflection process:
1. Load a class
2. dynamically create an object based on this class
3. Methods for dynamically obtaining this object
4. Call the method of this object
Allow the program to obtain the internal information of any class with a known name during the execution period, including package, type parameters, superclass, implemented interfaces, inner classes, outer class, fields, constructors, methods, and modifiers can generate instances during the execution period, change fields content, or evoke methods.
Author: nickname: old wow's paw (full name: Attilax Akbar Al Rapanui Attila Akba Arla Panui) Name: AI long, EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://www.cnblogs.com/attilax/
2. required classes in Class reflection: there are not many classes required for Class reflection. They are Field, Constructor, Method, Class, Object,
Below I will give a simple description of these classes.
Field Class: provides information about the attributes of a class or interface and its dynamic access permissions. The reflected field may be a class (static) attribute or instance attribute. A simple understanding of it can regard it as a class that encapsulates the attributes of the reflection class.
Constructor class: Provides information about a single Constructor of the class and its access permissions. This class is different from the Field class. The Field class encapsulates the attributes of the reflection class, while the Constructor class encapsulates the Constructor method of the reflection class.
Method class: Provides information about a separate Method on a class or interface. The reflected methods may be class methods or instance methods (including abstract methods ). This class is not hard to understand. It is a class used to encapsulate reflection class methods.
Class: an instance of a Class indicates the classes and interfaces in a running Java application. Enumeration is a type, and annotation is an interface. Each array is a Class mapped to a Class Object. All arrays with the same element type and dimension share the Class object.
Object Class: each class uses an Object as a superclass. All objects (including arrays) implement this class method.
3. Use of reflection
3.1. Visualization
JavaBean is one of the practical applications of reflection. It allows some tools to operate software components visually. These tools dynamically load and obtain the attributes of Java components (classes) through reflection.
3.2. system flexibility and scalability
Most of them use reflection and other methods to load external plug-ins. This decouples the system from the plug-ins and adds functions.
3.3. Json xml serialization 3.4. function pointer 3.5. Rpc
Private String exe (HttpServletRequest req ){
String meth = req. getParameter ("method ");
Handler hd = (Handler) mp. get (meth );
If (hd! = Null ){
Try {
Return (String) hd. handleReq (req );
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Return e. getMessage ();
}
}
If (hd = null ){
String classname = refx. getClassName (meth );
String meth_name = refx. getMethodName (meth );
Object o;
Try {
O = ConstructorUtils. invokeConstructor (
Class. forName (classname), null );
MethodUtils. invokeMethod (o, meth_name, null );
} Catch (Exception e ){
Return core. toJsonStrO88 (e );
}
4. Common Reflection APIs
Obtains an instance, calls a method, and obtains a set of methods and attributes. Get comments get anno
4.1. Apache common-lang. Reflect 5 classes 4.2. java. lang. reflect; 20 classes
O = ConstructorUtils. invokeConstructor (
Class. forName (classname), null );
Object invokeMethod_ret = MethodUtils. invokeMethod (o, meth_name,
Req );
4.3. C #. Net reflection api4.4. Php ReflectionClass ReflectionMethod
$ Class = new ReflectionClass ('person '); // creates a reflection class for the class "Person ".
5. Differences between C # java Implementation
However, in C #, you can load Assembly. LoadFile ("file path");, JAVA
No, because JAVA can create a JAR package at most.
The methods and attributes in the class can be printed using for or foreach.
Summary:
It seems that the two languages are learning from each other and copying each other. Actually a reflection,
The same idea. Only in JAVA, CLASS is used, while C # uses TYPE.
Continue to work hard. Come on !!!
6. Reference
PHP reflection mechanism-guisu, program life. If you do not enter, you will leave. -Blog channel-CSDN.NET.htm
PHP reflection class reflectionclass、reflectionmethoduse pai_php?_script. htm
Comparison between JAVA reflection and C # reflection-Lonely travel-blog channel-CSDN.NET.htm
Java and c# _ .htm
Learning: java principle-reflection mechanism-flying Dutch-boke .htm