IntelliJ IDEA toString templates for JSON output

Last Updated on by

Post summary: Templates to be applied to IntelliJ IDEA for automatic generation of toString() method that outputs the object in JSON format.

In some situations like APIs testing, it is good to be able to output requests and responses into log files for easy debugging. In some cases you might work with some framework that receives an object and sends it to the wire, receives the response and give you back object without having the possibility to output the data being sent or received. What you can do is print your request/response objects to logs.

Override toString() method

In order to print essential information from your object, it should properly override toString() method. Default Object.toString() prints full class name with memory address. This gives no information whatsoever. Overriding toString() by hand is not a really good idea as in case of API you have lots of objects.

IntelliJ IDEA toString() automatic generation

IntelliJ IDEA provides a way to automatically generate toString() method. It can be done by Generate toString() window shown below. Select Template and fields that have to be included then click OK. If there is already toString() method defined there is confirmation if you want to replace existing one.

IntelliJ-toString-generate

Generate toString() window is opened either by right click on the object you want to generate toString() for and selecting Generate from the menu, or by keyboard combination Alt + Insert. More about keyboard combinations can be found in IntelliJ IDEA keyboard combinations post.

Custom toString() template

IntelliJ IDEA comes with several predefined templates but none of them can print to JSON. You can customize and build your own template by selecting Settings button from Generate toString() window and then go to Templates tab.

IntelliJ-toString-template

Checkstyle

Checkstyle does static code analysis to verify Java code conforms to specific rules. It has OperatorWrapCheck which requires all operators to be at the beginning of the line. Templates below are created according to this rule. In case plus sign is required to be at the end of the line this can be very easily changed in templates.

JSON toString() template

Below is a toString() template that print object to a valid JSON. It doesn’t include super.toString() method if current class has superclass.

public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return "{\"$classname\":{"
#foreach( $member in $members )
#if ( $i == 0 )
+ "##
#else
+ ", ##
#end
#if ( $member.array )
\"$member.name\":" + java.util.Arrays.toString($member.accessor)
#elseif ( $member.string || $member.primitive || $member.numeric || $member.boolean || $member.enum )
\"$member.name\":\"" + $member.accessor + "\""
#else
\"$member.name\":" + $member.accessor
#end
#set ( $i = $i + 1 )
#end
+ "}}";
#else
return "{$classname}";
#end
}

JSON toString() template with super.toString() included

Below is a toString() template that print object to a valid JSON. It includes super.toString() method only if current class has super class.

public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
#if ( $class.hasSuper )
#set ( $i = $i + 1 )
return "{\"$classname\":"
+ super.toString()
#else
return "{\"$classname\":{"
#end
#foreach( $member in $members )
#if ( $i == 0 )
+ "##
#else
+ ", ##
#end
#if ( $member.array )
\"$member.name\":" + java.util.Arrays.toString($member.accessor)
#elseif ( $member.string || $member.primitive || $member.numeric || $member.boolean || $member.enum )
\"$member.name\":\"" + $member.accessor + "\""
#else
\"$member.name\":" + $member.accessor
#end
#set ( $i = $i + 1 )
#end
#if ( $class.hasSuper )
+ "}";
#else
+ "}}";
#end
#else
return "{$classname}";
#end
}

Template notation

Template engine uses Apache Velocity. There are a lot of variables that you can use. List of these is shown on Generate toString Setting Dialog page.

Conclusion

This is a handy feature that IntelliJ IDEA provides. Hope you can make use of it.

Related Posts

Read more...

IntelliJ IDEA keyboard combinations

Last Updated on by

Post summary: Some very useful keyboard combinations when working with IntelliJ IDEA.

IntelliJ IDEA is pretty good Java IDE. They provide community edition which is free and can suit perfectly your needs as an automation QA. This post described important keyboard combinations that can make you more productive. This post is for IntelliJ IDEA v14 Community Edition.

Original video

This post is inspired by video IntelliJ IDEA Tips and Tricks from JavaZone. I watched the video and realized how much you can achieve only with keyboard combinations.

I suggest you watch the video and take notes. I have taken important things from the presentation and put them into current post. It is much easier to read rather than scrolling the video back and forth.

Presentation Assistant plugin

This plugin outputs the keyboard combination of action you perform. You can select something from the menu and it shows the keyboard shortcut for this action. Very handy tool if your purpose is to learn how to do things only with the keyboard. How to install it is shown on 02:50 minute from the video.

Search

  • Ctrl + N – search and open types, search by camel case letters is possible, wildcards supported, :40 leads to line 40.
  • Ctrl + Shift + N – search and open files, search for a folder is possible with a slash (/) in front, wildcards supported, :40 leads to line 40.
  • Ctrl + Shift + Alt + N – search for symbol, filter by namespace is possible with dot.
  • Shift, Shift – search everywhere, TAB changes the resulting cluster, left arrow key gives history, right arrow key moves forward, by default recent files are shown.
  • Ctrl + Shift + A – looks up an action.

Navigation

  • Ctrl + E – recent files, search is possible.
  • Ctrl + Shift + E – recent edited files, search is possible.
  • Alt + 1 – open/close projects window.
  • Ctrl + Shift + F12 – hide/show all windows.
  • Ctrl + H – type hierarchy.
  • Ctrl + Alt + H – method hierarchy.
  • Ctrl + F12 – show class structure with methods.
  • Alt + 7 – open/close structure window.
  • Alt + F12 – shows terminal (cmd)
  • Alt + Home – jump to Navigation Bar (it should be enabled from View -> Navigation Bar). From Navigation Bar you can use keyboard arrows to navigate through packages.
  • Ctrl + Alt + S – opens settings.
  • Click on a module + F4 – opens module settings.
  • Ctrl + ` – shows a quick menu to customize the layout.

Code browsing

  • Alt + F7 – find usages.
  • Ctrl + B – go to declaration.
  • Ctrl + Alt + B – go to implementation.

Edit

  • Shift + Ctrl + L – auto format code.
  • Ctrl + W – select word, then next, then line, then method, then class, then whole file.
  • Shift + Alt + Up/Down – move selected code or line on which cursor is up and down.
  • Ctrl + Y – delete line. This is really confusing for many users. Many applications, such as Microsoft products, office applications, etc., use Ctrl + Y to redo actions, here it deletes line, and when you then do Ctrl + Z – undo you get your self into a real movie.
  • Ctrl + D – duplicate line.
  • Ctrl + C – copy.
  • Ctrl + V – paste.
  • Ctrl + Z – undo.
  • Ctrl + Shift + Z – redo.
  • Shift + F6 – rename class, method, field, variable, etc.
  • Ctrl + Shift + Alt + T – show complete re-factoring menu.
  • Alt + Insert – automatically generate getters, setters, toString, etc.
  • Alt + Enter – shows available actions that can be performed on given piece of code.
  • Alt + Enter -> Inject language or reference -> show nice editor for JSON for e.g.
  • Ctrl + P – get parameter info for a given method.
  • Ctrl + Shift + Space – smart completion, it can be invoked second time over the first completion.
  • Shift + Ctrl + Enter – complete current statement, insert semicolon.
  • Ctrl + Alt + V – extract variable from piece of code cursor is placed in.
  • Ctrl + Alt + T – opens predefined templates.

Debbuging

  • Ctrl + Shift + F8 – show all breakpoint, same is done with right click on breakpoint.
  • F10 – select a configuration to run.
  • Shift + F10 – run currently selected configuration.
  • F9 – select a configuration to debug.
  • Shift + F9 – debug selected configuration.
  • Ctrl + F2 – stop running application.
  • F7 – step into a method in case of debugging.
  • F8 – step over the method in case of debugging.
  • F9 – resume program in case of debugging.
  • Ctrl + F9 – build code.
  • Alt + F8 – show evaluate window during debugging.

Version Control

IntelliJ IDEA has very good Git plugin. You can do almost everything without installing additional visual Git client or without Git Bash.

  • Ctrl + K – commit changes.
  • Ctrl + T – pull changes.

Help

You can find complete keyboard references by IntelliJ IDEA Menu: Help -> Default Keymap References. This opens following PDF file IntelliJIDEA_ReferenceCard.pdf

Another source of information is: Help -> Productivity Guide

Conclusion

Being able to perform most of daily activities actions just with the keyboard will increase your productivity so it is worth spending some time to teach yourself how to work with IntelliJ IDEA keyboard combinations.

Related Posts

Read more...