ADF-DRY #1: JDeveloper code templates

DRY (Don’t repeat yourself) – is a one of the main principles of software development. It’s usually associated with avoiding of code repetition, but you can use it to optimise your workspace. How? Let’s start from code templates.

Code templates may helps you to unify an application source code and probably save a lot of your time. It helps you to focus on solving the main problem instead of wasting time on tasks which can be (and should) be automatized. One of that tasks is ADFLogger declaration which looks almost the same in every class.

The Problem

In this example we will create code template definition with custom shortcode(let’s say “xlog“). The template will be used to generate the following code:

The Solution

To create new code template open Tools -> Preferences and find code templates tab.

Click new button, and set:

  • Shortcut: xlog
  • Context: Java Type Members
  • Description: ADFLogger declaration

OK, now it’s time to fill the “Code” field with:

As you probably notified I used $class$ variable which we have to define. Go to the Variables tab and add new variable with following values:

  • Name: class
  • Type: Class Name
  • Editable: true

After that go to the Import tab and put the following package:

Click OK to save changes;

Test

Now it’s time to test prepared shortcut. Open any *.java file. Type xlog in the proper place and press Ctrl+Enter. The behaviour should be like on the images below.

 

Please notify, that import(1) section and the class name(2) were automatically inserted.

Homework

  • Create code template to generate following log line: LOGGER.fine("XXX {0}, YYY: {1}", XXX, YYY);
  • If you want, you can investigate some default templates like i.e. prm.

 

More about

 

I’m sure, this post will save your time!

Facebooktwittergoogle_plusredditlinkedinmail

Leave a Reply

Feel free to ask me about anything!

Notify of
avatar
wpDiscuz