Java Dynamic Compilation

Source: Internet
Author: User

1. What does reflection mean?

: Gets the basic information of the class dynamically while the program is running. For example: Create an object, invoke the method of the class, and get the basic structure of the class. This gives the program a lot of flexibility,

It can solve a lot of dead things. It can generate different information based on different classes/objects. For example, Hibernate generates SQL statements because it does not know what specific queries the customer is going to perform. Therefore, the corresponding SQL statements can be generated based on the class information and reflection. Very flexible, to solve the dead thing. My personal summary is to generate dynamic responses based on dynamic requirements.

。 The reflection function is very powerful.

Java reflection can provide a large

The advantage of the reflection mechanism is that it can realize dynamic object creation and compilation, which shows great flexibility, especially in the development of Java EE. For example, a large-scale software, it is impossible to design the perfect one at a time, when the program compiled, released, when the discovery needs to update some features, we can not want users to uninstall the previous, and then reinstall the new version, if so, this software is certainly not many people use.       Using static words, the entire program needs to be recompiled once to achieve the function of the update, and the use of reflection mechanism, it can not uninstall, only need to be dynamically created and compiled at run time, you can implement this function. Its disadvantage is that it has an impact on performance. Using reflection is basically an interpretation operation, and we can tell the JVM what we want to do and it satisfies our requirements. This type of operation is always slower than just performing the same operation directly.

, the Java reflection mechanism is a very useful thing, with it can solve a lot of dead things, because the reflection mechanism of flexible line is very large, with him, we do not spend too much time to write the code of the database, but the method more time in the logical function of the project, this can greatly reduce development time, And the code is very readable. Many of the first open source frameworks are just the reflection mechanism, which is just a configuration file and then the rules to invoke his method.
    1. JAVA6.0 introduces a dynamic compilation mechanism.
    2. Application Scenarios for dynamic compilation:
    • Can do a browser-side write Java code, upload the server compiled and run the online evaluation system, need to conduct security checks.
    • The server dynamically loads some class files for compilation
Iii. dynamically run the compiled Class 1. Start a new process run with Runtime.getruntime () run

For example:

[Java]View PlainCopy 
    1. <pre name="code" class="java" > Runtime run = runtime.getruntime ();
    2. Process process = Run.exec ("JAVA-CP e:/workspace/images Testjava");



2. Run the compiled class through reflection
    1. try {
    2. url[] urls = new url[] { new URL ("file:/" + "e:/workspace/images/")};
    3. URLClassLoader loader = new URLClassLoader (URLs);
    4. //Call this class through reflection
    5. Class clazz = Loader.loadclass ("Testjava");
    6. Method m = Clazz.getmethod ("main", string[].   Class);
    7. //M.invoke (null,new string[]{"AA", "BB"});
    8. //Since the variable parameter is jdk5.0, the code above will be compiled into M

Java Dynamic Compilation

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.