Use dynamic compilation in Java to implement eval

Source: Internet
Author: User
As we know, in many scripting languages, Eval Han numbers can be used to convert strings into statements and execute them. For example, in Javascript

VaR STR = aid. Value + ". style. Top = 10 ;"

Extract the value controlled by ID "aid" and combine it into a string. If the aid value is "axman ",
STR = "axman. style. Top = 10"
Now let's move the control axman to the top position of 10:

Eval (STR );

In this way, the string becomes an expression or statement to start execution. This function is very important for dynamic variable construction.
Meaning.

What if this function is implemented in Java? In fact, we can use dynamic compilation to achieve:

Suppose there is a set of methods to implement different functions. Now we need to call the corresponding method based on the passed method name. If there is no eval function, we
You can only do this:

Myclass MC = new myclass ();
If (Str. Equals ("M1 "))
MC. M1 ();
Else if (Str. Equals ("M1 "))
MC. m2 ();
Else if (Str. Equals ("m3 "))
MC. m3 ();
Else if (.........)
.........();

If there are one hundred cases?

If we use the eval method, we can directly do this:

String STR = ...........;
Eval ("MC" + STR + "();");

Is it very convenient? The key is how to implement eval ()?

We construct a complete class for the string to be converted: if the method returns a value, then:

Public object eval (string Str ){
// Generate a Java File
String S = "class temp {";
S + = "Object RT (){"
S + = "myclass MC = new myclass ();"
S + = "Return MC." + STR + "();";
S + = "}"
S + = "}";
File F = new file ("Temp. Java ");
Printwriter PW = new printwriter (New filewriter (f ));
PW. println (s );
PW. Close ();
// Dynamic compilation
Com. Sun. Tools. javac. Main javac = new COM. Sun. Tools. javac. Main ();
String [] cpargs = new string [] {"-d", "directory", "Temp. Java "};
Int status = javac. Compile (cpargs );
If (status! = 0 ){
System. Out. println ("the source file was not compiled successfully! ");
Return NULL;
}
// Call the RT method of temp to return the result:
Myclassloader MC = new myclassloader ();
Class clasz = mc. loadclass ("test. Class", true );
Method RT = clasz. getmethod ("RT", new class [] {string []. Class });
Return Rt. Invoke (null, new object [] {New String [0]});
// Call the API directly if the method is not returned.
}

We can first write a number of overloaded eval values with the returned value and no returned value, as well as parameters that can be passed.

In this way, we can use a string to convert to a Java statement for execution.

This article is just an example to illustrate the idea of dynamic compilation. for better implementation, please complete it yourself.

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.