The applicable scene of Java reflection mechanism and its advantages and disadvantages __java

Source: Internet
Author: User
Tags reflection

The application scenario of Java reflection mechanism and its advantages and disadvantages

What is the applicable scene of reflection?

1). The Java reflection mechanism is useful when it comes to doing the basic framework, saying that the reflection mechanism is the cornerstone of many Java frameworks . And the general application level is seldom used, but this kind of thing, now many open source framework basically already to you package good, oneself basic need not write. Typically, in addition to hibernate, Spring also uses a number of reflection mechanisms. The classic is to write the configuration in the XML file or properties, and then parse the contents of XML or properties inside the Java class, get a string, and then use the reflection mechanism to get a class instance of the classes from that string. This allows you to dynamically configure something, do not need every time in the code to go to new or do other things, later to change the words directly to change the configuration file, code maintenance is very convenient, and sometimes to adapt to some of the requirements, Java classes may not be able to directly call another method, This can also be achieved by reflection mechanism.
In general, you write very little, when you want to use that depends on the demand, reflection mechanism is simply based on a string to get the entity object you want, and then call it the original thing. But if you want to write the framework yourself, it will be used more.

2 When you do a software can install Plug-ins, you do not even know the type name of the plug-in, how do you instantiate this object? Because the program is to support the plug-in (third party), in the development of the time did not know. So it can't be new in the code, but reflection can get the correct class instance by reflecting, dynamically loading the assembly, then reading out the class, checking the markup, and then instantiating the object.

3 In the coding phase does not know that class name, to read the class name from the configuration file at runtime, there is no way to Hard-code new ClassName (), but must use reflection to create this object. The purpose of reflection is to extend an unknown application. For example, you write a program that defines some interfaces, as long as the DLLs that implement them can be inserted into the program as Plug-ins. So how to achieve it. can be realized by reflection. is to load the DLL into memory and then invoke the method in the DLL by reflection. Many factory models are used for reflection.

second, programmers in their own business development should try to stay away from reflection

reflection: in popular libraries like spring and hibernate, reflection naturally has its own application. But introspection business code is not a good thing in many cases, for many reasons, I always recommend that you do not use reflection.

The first is code readability and tool support. It's easy to open the familiar IDE and look for the internal dependencies of your Java code. Now, use reflection to replace your code and try again, what's the result? If you modify a encapsulated object state by reflection, the result becomes even more uncontrollable. Take a look at the following sample code:


If you do this, you will not be able to secure a compile-time guarantee. Just like the example above, you will find that if the parameters of the Getdeclaredfield () method call are wrong, they will only be discovered at runtime. You know, finding a run-time bug is far more difficult than a compile-time bug.

Finally, we have to talk about the price issue. The degree of optimization of the JIT to reflect is different, some optimization time will be longer, and some even can not apply optimization. Therefore, sometimes the performance loss of reflection can reach several orders of magnitude difference. But in a typical business application, you may not notice the cost.

To sum up, I think the only reasonable (direct) use of reflection in the business code is through AOP. In addition to this, you'd better stay away from the reflex feature.

third, performance analysis

Reflection mechanism is a kind of ability of program self analysis. Class variable, constructor, method, modifier for getting a class.

Advantages: Runtime type of judgment, dynamic class loading, dynamic proxy using reflection.

Disadvantage: performance is a problem, reflection is equivalent to a series of interpretation operations, to inform the JVM to do things, performance is much slower than direct Java code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.