Java reflection mechanism and application scenarios

What is Java reflection?

JAVA reflection mechanism is in the operating state, for any class, they are made known to all properties and methods of this class; for any object, call it can be any of a method; method of obtaining such a dynamic object and the dynamic invocation the feature is called Java's reflection mechanism.

What applies reflected scene?

  1. When you make a plug-in software can be installed feature, you type the name of the plug even do not know how you instantiate this object? Because the program is to support plug-ins (third-party), when the development is not known. New it can not be in the code, but can be reflected by a reflection, dynamic loader assembly and then read out the class, then a check mark and then instantiate objects, you can get the correct class instance.
  2. In the coding phase does not know the name of the class, to run from the configuration file to read the class name, this time there is no way hardcoded new ClassName (), and must use reflection to create the object. The purpose is to expand the reflection of the unknown Applications. For example, you write a program that defines a number of interfaces, which implements these dll interface can be inserted into a plug-in to this program. So how to achieve it? It can be achieved by reflection. The dll is loaded into memory, then call a method of the dll by way of reflection. Many factory model is the use of reflection. 

Programmers to develop their own business should as far as possible away from the reflection

Reflection: in the popular libraries such as Spring and Hibernate, reflecting its place naturally. But introspection business code in many cases not a good thing, for many reasons, in general, I always suggest that you do not use reflection.

The first is the code readability and tool support. Open familiar IDE, internal dependencies to find your Java code, it is easy. Now, using reflection to replace in your code and then try again, the result? If modified by being reflected encapsulated object state, then the result will become uncontrollable. Please look at the sample code is as follows:

1 public class Secret {
2     private String secrecy;
3     public Secret(String secrecy) {
4         this.secrecy = secrecy;
5     }
6     public String getSecrecy() {
7         return null;
8     }
9 }
 1 import java.lang.reflect.Field;
 2 
 3 public class TetsSecrecy {
 4     public static void main(String[] args) throws Exception {
 5         Secret s = new Secret("TOP SECRET");
 6         Field f = Secret.class.getDeclaredField("secrecy");
 7         f.setAccessible(true);
 8         System.out.println(f.get(s));
 9     }
10 }

If you do so you can not get security guarantees compile-time. Like the example above, you will find that if parameter input getDeclaredField () method call is wrong, only to discover at runtime. To know is difficult to find runtime Bug Bug is much more than compile period.

Finally, to talk about the price issue. JIT to optimize the degree of reflection is different, and some optimization time will be longer, and some even can not be applied optimization. Therefore, the performance loss can reach the reflection difference sometimes several orders of magnitude. However, in typical business applications, you probably will not notice the price.

To sum up, I think the only rational business code (direct) reflection scene through the use of AOP. In addition, you better stay away from this reflection characteristics.

Performance Analysis

Reflex mechanism is the ability to program self-analysis. For obtaining a class variable class, constructors, methods, modifier.

Advantages: determination of the type of operation, dynamic class loading, dynamic proxy using reflection.

Disadvantages: performance is an issue, reflecting the equivalent of a series of operations explained notify jvm things to do than direct performance of java code much slower.

Guess you like

Origin www.cnblogs.com/carry6/p/11521578.html