CSC/ECE 517 Summer 2008/wiki1 7 n1: Difference between revisions
No edit summary |
No edit summary |
||
Line 10: | Line 10: | ||
eval("puts \"Hello World\"") #--> Hello World | eval("puts \"Hello World\"") #--> Hello World | ||
===Kernel.eval with | ===Kernel.eval with binding=== | ||
You can optionally specify a binding with the eval method. If a binding is given, the evaluation will be performed in the context of the binding. For example: | |||
def get_binding | def get_binding | ||
Line 18: | Line 19: | ||
a = 2 | a = 2 | ||
the_binding = get_binding | the_binding = get_binding | ||
eval("puts a", the_binding) #--> 1 | eval("puts a", the_binding) #--> 1 | ||
eval("puts a") | eval("puts a") #--> 2 | ||
===Object.instance_eval=== | ===Object.instance_eval=== | ||
Line 32: | Line 33: | ||
paginator = Paginator.new | paginator = Paginator.new | ||
paginator.next | paginator.next | ||
paginator.instance_eval("puts @page_index") #=> 1 | paginator.instance_eval("puts @page_index") #=> 1 | ||
===Module.class_eval=== | ===Module.class_eval=== | ||
klass = Class.new | |||
klass.class_eval("def hello() puts \"Hello World\" end") | |||
klass.new.hello #==> Hello World | |||
==Benefits of Runtime Evaluation== | ==Benefits of Runtime Evaluation== |
Revision as of 22:27, 6 June 2008
Ruby's eval can parse and execute an arbitrary string of Ruby code. Does Java have a similar facility? Provide an illustration of what this facility is good for. Compare the ease of writing such code in Ruby vs. writing equivalent code in Java.
Evaluation Options in Ruby
The eval method is one of the most powerful features of Ruby. The Kernel.eval will parse and execute an arbitrary string of legal Ruby source code. To put it plainly, if your Ruby program can generate a string of valid Ruby code, the Kernel.eval method can evaluate that code. This facility gives developers the ability to modify the runtime behavior of application.
Using eval method is straightforward in Ruby.
Kernel.eval
eval("puts \"Hello World\"") #--> Hello World
Kernel.eval with binding
You can optionally specify a binding with the eval method. If a binding is given, the evaluation will be performed in the context of the binding. For example:
def get_binding a = 1 binding end a = 2 the_binding = get_binding eval("puts a", the_binding) #--> 1 eval("puts a") #--> 2
Object.instance_eval
class Paginator def initialize @page_index = 0 end def next @page_index += 1 end end paginator = Paginator.new paginator.next paginator.instance_eval("puts @page_index") #=> 1
Module.class_eval
klass = Class.new klass.class_eval("def hello() puts \"Hello World\" end") klass.new.hello #==> Hello World
Benefits of Runtime Evaluation
There are many scenarios where the eval method can be utilized.
Calling methods dynamically:
Evaluating input from user:
Add method to a class at run time
Evaluation Options in Java
Java does not have eval method in its standard library due its nature of being static typing and a compiled language, not an interpreted language like Ruby. However it is entirely possible to build an interpreter in Java that would provide similar facility. Below are just few some approaches:
- For simple application, the use of Hashtable with String lookup key would be enough to execute some prewritten piece of code. This is hardly runtime evaluation.
- Java Reflection API providers developers the ability to examine or modify the runtime behavior of applications. Developers can dynamically load Classes, examine private members, and invoke methods. However it cannot be used to parse and evaluate any arbitrary string of Java code. One workaround is to generate code on the fly and write it out to disc, use sun.tools.javac.Main to invoke javac.exe and compile the file into class file, finally using Class.forName of Reflection API to invoke the new code.
- Write a parser using JavaCC. A parser generator is a tool that reads a grammar specification and converts it to a Java program that can recognize matches to the grammar. In addition to the parser generator itself, JavaCC provides other standard capabilities related to parser generation such as tree building (via a tool called JJTree included with JavaCC), actions, debugging, etc.
- Using third party scripting framework that runs in JVM, such as BeanShell, and Jpython.
- Using Java 1.6 Scripting framework. It allows Java applications to host script engines. Third party script engines are supported by dropping any JSR-223 compliant script engine jar in the CLASSPATH and access the same from your Java applications. This mechanism is similar to the way JDBC drivers, JNDI implementations are accessed.
import javax.script.*; public class EvalScript { public static void main(String[] args) throws Exception { // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create a JavaScript engine ScriptEngine engine = factory.getEngineByName("JavaScript"); // evaluate JavaScript code from String engine.eval("print('Hello, World')"); } }
Conclusion
Getting the benefit of runtime evaluation in Java is certainly not impossible, but it isn't easy. Generally Java developers need to determine which of the evaluation options above best suits the application. Using third party scripting engine, the developers need to determine if third-party libraries are compatible with the Java version the project will be using. They need to learn the third party library's API, and syntax for scripting engine. The learning curve is much more steeper for those choose to writer a parser using JavaCC. In addition to learning JavaCC syntax, developers must have knowledge in compiler design and have thorough understanding of Java language and grammar. Java 1.6 introduces Scripting for Java Platform, a powerful scripting framework, but this does not eliminate the fact developers must learn another scripting language to use the framework. Consider Java 1.6 scripting framework the simplest solution, a simple printing "Hello World" application requires 8 lines of code as opposite to 1 line of code in Ruby. Fortunately with Apache Bean Framework and JRuby to bridge Java and Ruby, developers won't have to choose or another.