Browse DevX
Sign up for e-mail newsletters from DevX


Build a Reflection-based Interpreter in Java : Page 2

Java's support of reflection enables you to create an interpreter that executes commands interactively. This article demonstrates how to build a reflection-based system that allows simple scripting of Java programs without having to install a special-purpose scripting language. It also provides a downloadable sample program.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Every command specifies a class as the first element of the interpreter. In fact, the first word of every command in the scripting language must be a valid Java class. Additionally, the second word of every method must be a valid static, public method of that class.

These classes and methods are accessed from the CommandLine interpreter class, which is the main class of your scripting language. Figure 2 shows the relationships between the parts of a scripting command and the classes and methods running inside the CommandLine object.

Figure 2: The CommandLine Object

The following is a piece of the Calculator class:

static public double add( double a, double b ) { return a + b; }

This method must be static. As I mentioned previously, the scripting language has no variables. If you create an instance of an object, you have no place to put it and no way to reference it from the interpreter. By simply making sure that your methods are static, you bypass the need for instances.

Note also that your method does have a return value. This return value is printed after the method is executed.

Test: The Sample Program
Test is the name of the sample program provided with this article. Here's an example of a simple session within it:

% java Test $ Calculator add 10.0 20.0 30.0 $ Calculator divide 100.0 4.0 25.0 $ Calculator divide 100.0 0.0 Infinity $ Calculator divide 100.0 hello Error: CommandLineException: Can't find method divide in Calculator $ TinyChatServer startServer 5000 Server started on port 5000 Listening on ServerSocket[addr=,port=0,localport=5000] $ TinyChatServer startServer 5555 Listening on ServerSocket[addr=,port=0,localport=5555] $

Don't let the prompts confuse you. The first prompt, %, is the actual operating system command prompt, at which you type the java command. The second prompt, $, is the interpreter prompt.

This interpreter session does a little arithmetic and then starts up two chat servers (running on different ports). (You can try out the chat server by running the command java TinyChatServer hostname port at another command prompt.) Notice that your interpreter catches errors: when you tried to divide 100.0 by "hello" it reported that it couldn't find a method that matched the arguments. I'll discuss this more later. First, take a look at the code to see how CommandLine works.

CommandLine reads commands from an InputStream and writes its output to an OutputStream. This function allows CommandLine to read commands from files, standard input, or any other data source that can be presented as a string. It also allows CommandLine to redirect the output of a command anywhere.

The Test program creates a CommandLine object that reads from System.in and writes to System.out as follows:

static public void main( String args[] ) throws Exception { new CommandLine( System.in, System.out ); }

CommandLine has a background thread that reads from the input stream and writes to the output stream. Here's an abbreviated version of the interpreter (download the full source for a complete look):

String line = in.readLine(); String result = execute( line ); out.write( result );

Whatever the command returns is printed to the output stream.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date