Java Copier Frees You from Tedious Coding

ometimes Java classes seem like close relatives who unfortunately don’t have the same parent. Their structures and looks are similar, but not so similar that you can derive one from the other. For example, two Java files might share a large part of their structures but significantly differ in details, because they inevitably have different properties.

So when you implement a class that’s similar to an existing one, you find yourself copying the old class source to the new file, editing the new source, and modifying class and member names as well as member types in several spots.

The somusar/tjpp product offers a better way. A smart, simple Java copier, somusar/tjpp can take the property list for new class Y and make it like class X.

Editor’s Note: The author, Francesco Aliverti-Piuri, is co-founder and manager of SO.MUS.AR, the company that markets somusar/tjpp. We have selected this article for publication because we believe it to have objective technical merit.

Java Copier Case Study: Your HelloWorld Class Delivery Business
Your company delivers countless HelloWorld classes over the Internet. Believe it or not, the business is flourishing and you have to develop HelloWorld classes at an increasingly rapid pace to keep up with customers’ requests.

The following is a typical delivery for your company:

class Hello {   public static void main (String args[]) {      System.out.println("Good morning, Francesco!");   }}

Or like this:

class Hello2 {   public static void main (String args[]) {      System.err.println("Hello, Fap!");   }}

Soon the requests grow so fast that your copy/paste/edit technique is overwhelmed. Hire a new programmer, maybe? Not in this economy, the boss says. So you have to come up with a new way to solve your productivity gap. You remember that you read something about a Java code generator and you rush to your browser to google for it. You find somusar/tjpp and download the free demo.

Twenty minutes later, you have written your new macro, “hello”:

@ INTERFACE(GREETING,WHO,CHANNEL)class __JAVACLASS__ {   public static void main (String args[]) {      CHANNEL.println("GREETING, WHO!");   }}

This snippet instructs tjpp to replace GREETING, WHO, and CHANNEL with the parameters of the macro call. Additionally, the source files that you write thereafter need only contain a call to that macro with the appropriate arguments, like this:

$ hello(Good morning,Francesco,System.out)

Or like this:

$ hello(Hello,Fap,System.err)

Then you can convert them to regular, standard Java files with one simple command:

  prompt> tjpp Hello.tj  prompt> tjpp Hello2.tj

Tjpp generates well over 1,000 lines of code per second on a regular desktop PC. You could verify its performance by writing a more complex macro and using it several times in the same input file. You then could run tjpp on it and measure its performance on your own desktop.

Somusar/tjpp Does Tedious Coding for You
Multi-property Java classes often require developers to write code to perform a similar action for each property. An example is copying data from a database query result set to the corresponding members of the Java class. Another is the set of accessors and mutators, often referred to as getters and setters. A third is the toString() method returning a string containing the current value of each class property, as in the following example:

public String toString() {    String s = "[ propertyA = " + propertyA +                 " propertyB = " + propertyB + " ]";    return s;}

This method can be very useful when debugging, but writing it can be tedious (as is the case for getters and setters). Moreover, the toString method needs maintenance: if you add a new property to the class, you also have to extend it to include the new property in the return string. This has led many Java programmers to discard it from their classes, even though they appreciate its usefulness.

Somusar/tjpp provides a means to let your computer rapidly do this tedious work for you. The following input automatically produces a standard five-properties Java class (see Listing 1).

public class FiveProperties {$   property(int,      a_unique_id,      readonly)$   property(double,   a_double)$   property(String,   a_string)$   property(Object,   an_object)$   property(int [][], an_int_array)$   getters$   setters$   to_string}

The class declaration contains a list of properties, complete with type, identifier, and optional flag for immutable properties (“readonly”). Then three lines instruct tjpp to produce getters, setters, and toString for those properties. You can freely add handwritten Java code wherever you like in this input. Tjpp will copy it to the output Java file, including blank lines, comments, and so on. The only exceptions are tjpp command lines, marked by a dollar sign ($) in the first column, which will not be in the Java file–not even as blank lines.

You also can easily write your own macros, and program tjpp to perform whatever repetitive task you meet in your daily work. And you don’t need to learn a completely new, complex language syntax to do this.

Tjpp Example: to_string Macro
To see the java copier’s functionality in action, rewrite a macro. First, create a file “to_string” in the macro directory of tjpp. You don’t need to compile or configure it, because tjpp uses textual instructions and file names for its macros. Thus, you can create a new tjpp command (“hello_again”) simply by creating a text file called “hello_again” in the macro directory. Then you use it as you do the predefined macros:

$ hello_again$ hello_again(with,some,parameters,if,you,like)Your new "to_string" file contains the following code:    public String toString() {        String s = "[" +$       property_scan(property_string)                   " ]";        return s;    }

Except for the property_scan command, which applies the property_string macro to each property of the class, it’s just Java. So, now write a macro file called “property_string”:

@ INTERFACE(PROPERTY_INDEX,PROPERTY_COUNT,PROPERTY_INFO)@ SET prop_id=$property_id(PROPERTY_INFO)                   " prop_id = '" + prop_id + "'" +

Line 1 tells this macro to expect three arguments that correspond to the arguments that property_scan provides when it calls property_string. In this particular case, you are interested only in the third argument, from which you can retrieve the property identifier using the predefined macro property_id (line 2). Line 3 also is Java-like: tjpp replaces the prop_id placeholder with the current property identifier.

Tjpp produces the following method (FiveProperties.toString) as a result:

public String toString() {    String s = "[" +               " aUniqueId = '" + aUniqueId + "'" +               " aDouble = '" + aDouble + "'" +               " aString = '" + aString + "'" +               " anObject = '" + anObject + "'" +               " anIntArray = '" + anIntArray + "'" +               " ]";    return s;}

More Than Just a Copier
Tjpp provides other facilities to simplify and accelerate development cycles, most notably:

  • Preprocessor-like directives: conditional generation of code, file include, and generation variables (similar to C/C++’s #if…#else…#endif, #include, and #define)
  • A tracing directive that prints on System.err the value of one or more variables with the file name and file line of the trace
  • An assert directive that checks for a boolean expression and prints a message if it evaluates to false

Remember that tjpp is open for you to write new macros, or even rewrite the existing macros (as you did with to_string). So, this product could enable a quantum leap in your productivity.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: