Java Generator
Mar 20th, 2007 at 8:13 pm by Jon
A few days ago, a friend from SEMO IMed me, wanting help on a java program. So, I immediately set out on a search of my hard drive and stumbled across the very same program he was working on. Thats the problem with school programming; it’s always the same thing, semester after semester… This program was actually on of the more complicated one’s we did for java, so I wasn’t at all surprised that he was having trouble with it (Graeme is a networking guy, not a developer).
Anyway, here is the description for the project. This is actually this year’s requirements, so my project may not be entirely correct.
Programmes can help us write programmes. “Personal” code generators usually have a simple interface and produce code in the programmer’s (or their boss’s or teacher’s) preferred style. Such generators should be easily changeable and adaptable so they can be easily modified.
Write a Java programme which generates the “routine” code for a class from a specification in a simple (ASCII) text file (described below). Since this programme will be rewritten and adapted it is important to think through how everything will be represented before writing code. (Think classes!)
The basic usage of this programme is:
Each generated method should have a comment indicating where changes are likely. The generated code should contain a testing main method. This method may assume that data for testing is to be read from standard input.
The programme has one or two command line parameters, the first is the name of the specification file, the second (if present) is the name of the file to hold the output - use standard output if there is no second parameter. For now just use standard input and output, redirecting files.
The basic structure of the specification file is:
- the first character on a line determines the kind of line as
follows:
- ! line contains the class name and other information
- % line contains the type and name of a data field.
- $ line contains the return value name and parameters of a method
- The second character determines kind of access
- - private access
- + public access
- # protected access
- * package (friendly) access
Sample specification file contents follow:
!Name
%-String first
%-String last
%-String mid!+ HourlyEmployee extends Employee
%- double hours
%- double payrate
%- Date birthDate
$+ String toString()This is version 0.1 of the programme. The current date, time, and version of the generating programme should be in a comment at the beginning of the generated code. The data fields should be the first items in the class followed by constructors and the accessors and the mutators. Other commonly inherited methods can be “silently” generated. The output file is to be appropriately indented and formatted. The formatting and indentation should be easy to modify.
So, if you’d like to take a peek at this tiny program, just click here. Don’t worry, I’d rather cut off my foot than post a virus on the web. If your really that paranoid (I’m talking about you Matt), then don’t get on the internet!