JDeveloper code templates

0

When you write a lot of code, no matter what sophisticated framework you use, there are pieces of code that you end up writing over and over again (especially when using Java). Writing logging code in Java is a good example. A common pattern is to initialize a static logger variable in every class you want to log something. Fortunately, JDeveloper has a feature called “Code Templates” that can make your life a lot easier! I have seen several blogs that explain how to use Code Templates for logging code (I saw one from as early as 2006; the feature itself has actually been available since 2004!). But still a lot of people are not aware of this feature (or perhaps simply forget to use it).

That is why I created the simplest logging code templates ever. Download the Code Templates XML file and import them into JDeveloper, see screenshot below (note that you cannot import the file from a location with a space in the path, a known JDeveloper issue).

image

 

Now simply enter “log” followed by Ctrl-Enter at the start of the class to create a static logger variable.

image

image

 

After that, simply enter “log” followed by Ctrl-Enter anywhere you want to log something.

image

image

 

That is easy, right? A slightly more complicated example is the template: “lvar”. With this template you can easily log the name and value of a variable. Simply enter “lvar” followed by Ctrl-Enter and then you can choose the variable to log (currently does not work for primitive types).

image

 

Remember that code templates are not limited to logging code (or to Java code)! Whenever you find that you have to type the same thing (or similar things) over and over again you could probably create a code template that does it for you.

Share.

About Author

Leave a Reply