Entering the Java Web

Reflection

  Java reflection mechanism of Java program refers to the operating state, for any of the classes, have access to all properties and methods of this class; for a given object can invoke any of its properties and methods. This dynamic access to the content and the dynamic call object class method called reflection .

  The Java reflection mechanism allows programmers in the unknown class, the class information acquired become more diverse and flexible manner, to call the corresponding methods class, Java is a mechanism for increasing the flexibility and dynamics

 

significance

  First, the reflection mechanism greatly improves the flexibility and scalability of the program, reduce the coupling module, to improve their ability to adapt.

    Second, you can let the program create and control any kind of objects through reflection, without prior hard-coded target class.

    Again, the use of reflection can be configured at runtime class of an object, determining a class has member variables and methods, invoke an object method.
    Finally, where the reflex mechanism is the basis for building the technology framework that uses reflection to avoid writing code to die in the frame.
 
  It is more than the reflection characteristics, so it can dynamically compile and create objects, greatly inspired the flexibility of a programming language, and strengthen the characteristics of a multi-state, to further enhance the ability to abstract object-oriented programming, and thus subject to the programming community all ages.
 
principle
  Reflection (the Reflection) is a more advanced features provided by Java, which provides a dynamic feature, and this feature is that reflected by the reflection mechanism related API can obtain any Java class includes properties, methods, constructors , modifiers, and other information. Element need not be determined in the JVM is running, so that they can be reflected dynamically created or invoked at run time. Reflecting a lot about technology in middleware
 
Features
  1, the properties of an object
  Obtained by the getClass () method of an object class, and then instantiate a Field objects, properties receiving class declaration, and finally () method to obtain an instance of the property through get, note that the property must be public here, if you want to access it is necessary to set a mark may ask to access, otherwise it will report illegalAccessException exception

   

   2, to obtain the static properties of a class

  First, according to the class attribute classes get, get with an object as a Field object by instantiating receiving properties of the class declaration, it is different, because the property is static, so directly from the class
 
  3, the method performed on an object.
    Similarly, first to obtain the object class , then the class configuration of the array, and to make it a condition searching method. By getMethod () method, a method to be executed obtained. Invoke execution of the method, a method with parameters args object owner of the process execution. The return value is an object.
 
  4, performing a static method of a class.
  The basic principle and the "method of execution of a class," the same, except that method. invoke (null, args), the first argument here is null, because the call is a static method, so the owner will be able to run without the aid.
 
  5, a new instance of the class. Methods We performed using parameters constructor to create a new instance. If no parameter can be used directly newoneClass. newlnstaneeO to achieve ridicule. Similarly, first class to be constructed to give an example, and the array type, the configuration parameters are configured by getConstructor (argsClass) obtained last used newlnstanee (args) method to create a new instance.
 
Feature
 
  Despite brings great flexibility and convenience reflection, but reflection and disadvantages. Reflection function is very powerful, but not abused. When not in use can complete reflection, try not to use, for the following reasons:
  
  1, performance problems.
 
  Java reflection mechanism contains some dynamic typing, so Java Virtual Machine is not able to optimize the dynamic code. Thus, the reflection efficiency of operation is much lower efficiency than in normal operation. We should avoid using reflection high performance requirements of the program or code to be executed frequently. Also, how to use reflection determines the level of performance. As part of the program if it is running less, the performance will not be a problem.
 
  2, security restrictions.
 
  Use reflective usually need to run the program no security restrictions. If a request for program safety, it is best not to use reflection.

 

    3, program robustness

  反射允许代码执行一些通常不被允许的操作,所以使用反射有可能会导致意想不到的后果。反射代码破坏了Java程序结构的抽象性,所以当程序运行的平台发生变化的时候,由于抽象的逻辑结构不能被识别,代码产生的效果与之前会产生差异。

Guess you like

Origin www.cnblogs.com/ranxiaofan/p/11907549.html