Our Blog > Blog Detail

Web Content Viewer- Asponte Custom Skin

Actions

Quick Development Tip: RAD Code Templates

  • Created By: John Ewers
  • Updated: February 16, 2016

Code templates in Rational Application Developer (RAD)/Eclipse are a great way to keep Java code blocks consistent. To access the code templates navigate to Preferences -> Java -> Code Style -> Code Templates. Here you can specify the code that is automatically generated by RAD. An important note is these are only added when RAD creates the file/methods. This means that when you create new file in rad these comments and code will be set.

 

 

 

Here are a couple of my code templates:

New Java Files:

/**

* Asponte

* ${user}

*/

 

${package_declaration}

import java.util.logging.Level;

import java.util.logging.Logger;

 

${type_declaration}

Class Body:

/** Logger class name */

private static String CLASS_NAME = ${type_name}.class.getName();

/** Logger object */

private static Logger s_log = Logger.getLogger(CLASS_NAME);


 

You will notice I have several ${variables}. These variables will automatically populate in the context of your new file. There are two types of variables: variables defined by RAD and user defined variables. Sometimes as in the case of ${type_declaration} in "New Java Files" the variables could be required.


 

The next area we will look at is Preferences -> Java -> Editor -> Templates. These templates can be inserted anytime, not just at RAD creation. This makes these templates more versatile than the Code Templates we looked at before. Here are three that I use all time:

addEntering:

final String METHOD_NAME = "${enclosing_method}";

boolean isTraceEnabled = ${log:var(java.util.logging.Logger)}.isLoggable(Level.FINER);

boolean isDebugEnabled = ${log:var(java.util.logging.Logger)}.isLoggable(Level.FINEST);

boolean isErrorEnabled = ${log:var(java.util.logging.Logger)}.isLoggable(Level.SEVERE);

if(isTraceEnabled) {

   ${log:var(java.util.logging.Logger)}.entering(CLASS_NAME, METHOD_NAME,new Object[]{${enclosing_method_arguments}});

}

${cursor}

addExiting:

if(isTraceEnabled) {

   ${log:var(java.util.logging.Logger)}.exiting(CLASS_NAME, METHOD_NAME);

}

addDebug:

if(isDebugEnabled) {

   final String MESSAGE = ": ${cursor}";

    StringBuilder debugBuilder = new StringBuilder();

    debugBuilder.append(METHOD_NAME);

   debugBuilder.append(MESSAGE);

   ${log:var(java.util.logging.Logger)}.finest(debugBuilder.toString());

}

 

When I create a new method I type "addEn" and the press Ctrl+Space and "addEntering" will be automatically added defining method name and my logging status and then place the cursor at the end of the method block.

 

 

These tips can help to speed up your development time and keep some consistency throughout your code. A third advantage for me is everytime I create a method and see my 3 logging levels I am reminded to think about what I should log while coding and not after when something has broken.

Contact Form- Asponte Custom Skin

Actions

Contact Us

*
*


Submit
Call Us 888-926-9434
Complementary Content
${loading}