Using dynamic compilation to implement Eval in Java

Source: Internet
Author: User
Tags eval implement return
compiling | Dynamic We know that there are eval numbers in many scripting languages, which converts strings to statements and executes them. As in JavaScript





var str = aid.value + ". Style.top = 10;"





the value of an ID for "aid" to be taken out to combine into a string, if the value of aid is "Axman", then


str = "Axman.style.top = 10"


now we want to move the control Axman to the top 10 position:





eval (str);





so this string becomes an expression or the statement begins execution. Such a function is very important for dynamic construction of variables


meaning.





So in Java, what if this functionality is implemented? In fact, we can use dynamic compilation to achieve:





assume that there is a set of methods to implement different functions, and now to invoke the corresponding method based on the method name passed in, if there is no eval function, we


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 100 kinds of situations?





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





String str = ......


eval ("MC" +str+ "();");





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





we construct a complete class of strings to be converted: If the method has a return value. Then:





Public Object eval (String str) {


//Generate 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 ("No source file compiled successfully!");


return null;


}


//Call Temp RT Method Return 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]});


//If the method does not return, call the
directly

}





we can write more than one overloaded eval, with return values and no return values. And the parameters that can be passed.





so we can execute it with a string conversion to a Java statement.





This article is just an example, illustrate a dynamic compilation of the idea of a better implementation please friends themselves to complete.











Related Article

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.