Browse DevX
Sign up for e-mail newsletters from DevX


Java Copier Frees You from Tedious Coding : Page 4

Transfer the repetitive copy/paste/edit part of your Java programming from your hands to your computer. With the somusar/tjpp Java copier, you can set yourself free of this tedious work.




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

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.

Francesco Aliverti-Piuri has more than 20 years of varied software experience on several platforms, technologies, and architectures. His focus is on code and documentation generation. .
Thanks for your registration, follow us on our social networks to keep up-to-date