This assignment uses ideas from Sections 7.2 and 7.6. This assignment is based on Exercise 7.9 from the end of Chapter 7 in our textbook. This assignment is due Thursday, December 12.
Download this zip file.
You will write a program that uses Java's Reflection API to interpret simple script files that direct your program to instantiate objects and then call methods on those objects.
In the zip file there are several example script files. These files have a fairly simple syntax. For example, look at the files reflection.1.script
and reflection.2.script
while you read this description of their syntax. The script files are divided up in "Blocks" that are separated by a blank line. The first line of each "Block" is the name of a Java class (which, of course, is also the name of the class's constructors). The next line is the number of arguments to a constructor of that class. The next several lines are the constructor arguments themselves. After the constructor arguments, there is a method name (the method should be from the class that was just instantiated). After the method name, there is a line with the number of arguments to that method. After that line, there are the method arguments themselves. After the method arguments, there can be another group of lines that specify another method and its arguments. A blank line denotes that there are no more method calls to the current object.
There is one more feature to the language in these script files. In the file reflection.2.script
notice that there are lines containing $1
, or $2
, or $3
, etc. These are references to previously instantiated objects. For example, the symbol $1
refers to the first object that was instantiated (i.e., in the first block). And $2
would refer to the second object that was instantiated. These symbols can be used as arguments to constructors or methods. In addition, they can be used instead of the name of a class to let you return to a previously instantiated object and call more methods on it (that is, a block of method calls can use a previously instantiated object instead of having to create a new object; see for example the last block in reflection.2.script
).
In the zip file there is a program RunReflectionScript_Demo.class
that is a demonstration version of the program that you need to write. You can run this program on the included script files to see how your program should work.
This assignment is adapted from Exercise 7.9 from the end of Chapter 7 in our textbook. The author of the textbook has a solution to that problem online, and an updated (for Java 6) copy, HorstmannChapter7Exercise9Solution.java
is also included in the zip file. You can use that file to get an idea of how to do some of the step in this assignment.
In the zip file there is a file RunReflectionScript.java
which is a rough outline of solution to this assignment. You should complete the code in this file.
In the zip file that are files FormLayout.java
, FormLayoutTester.java
, and FormLayoutTester.script
. The purpose of these files is to give you an idea of the usefulness of reflection. The files FormLayout.java
and FormLayoutTester.java
are from Chapter 5. They are an example of the kind of code that creates GUI's. The file FormLayoutTester.script
is a script file that can be interpreted by RunReflectionScript_Demo.java
and it duplicates the GUI created by FormLayoutTester.java
! Notice what this shows. A program like RunReflectionScript_Demo.java
, which doesn't explicitly have anything to do with GUI's, can use reflection to create a GUI from a script file. This is how many modern programs use reflection. They store a description of their GUI in some kind of (usually XML) data file and then at runtime, the program builds its GUI from the description in the data file. This allows the design of the GUI to be changeable long after the code for the program is written and compiled.
Turn in a zip file called CS507Hw3Surname.zip
(where Surname
is your last name) containing your source code file RunReflectionScript.java
.
This assignment is due Thursday, December 12.