toString() Generator: Format Templates
Format templates are used by a simple mechanism that allows you to change format of generated method's output string: beginning, ending, separator, and so on. They look similar to JDT code templates but they don't affect the generated code directly (
Code Styles are used for that). Here's a list of available template variables:
${object.className}
|
inserts the class name as a simple String |
${object.getClassName}
|
inserts a call to this.getClass.getName()
|
${object.superToString}
|
inserts a call to super.toString()
|
${object.hashCode}
|
inserts a call to this.hashCode()
|
${object.identityHashCode}
|
inserts a call to System.identityHashCode(this)
|
${member.name}
|
inserts the first member's name |
${member.name()}
|
inserts the first member's name followed by parenthesis in case of methods |
${member.value}
|
inserts the first member's value |
${otherMembers}
|
inserts the remaining members. For each member, the template fragment between the first and the last ${member.*} variable is evaluated and appended to the result. The characters between the last ${member.*} and ${otherMembers} define the separator that is inserted between members (${otherMembers} must stand after the last ${member.*} variable). |
For the template to work properly, the ${otherMembers} variable must be used exactly once in a template and cannot be followed by any ${member.*} variable. ${object.*} variables can be placed anywhere in the template, although if one is placed in a member related fragment (that is between the first ${member.*} variable and ${otherMembers}), it will be repeated for every member, which is probably not a sensible solution.
The description above might seem complicated, but the template format itself is very easy to use. It should all become clear after seeing some working examples.
Template examples
-
The default template is a good example:
${class.name} [${member.name()}=${member.value}, ${otherMembers}]
The output string for this template looks like this:
FooClass[aFloat=1.0, aString=hello, anInt=10, anObject=null, aCharMethod()=a]
-
Multiple line output is also available:
${object.getClassName} {
${member.name}: ${member.value}
${otherMembers}
}
Example result:
FooClass {
aFloat: 1.0
aString: hello
anInt: 10
anObject: null
aCharMethod: a
}
-
If you enclose a member in braces, don't forget to do the same with ${otherMembers} variable:
{${member.name}=${member.value}},
{${otherMembers}}
Here's the effect:
{aFloat=1.0},
{aString=hello},
{anInt=10},
{anObject=null},
{aCharMethod=a}
-
${object.*} variables can be used at the beginning and at the end of the template:
${object.getClassName} (hashcode:${object.hashCode})
members: ${member.name} = ${member.value}; ${otherMembers}
[super: ${object.superToString}]
This template would result in an output similar to this:
FooClass (hashCode:232198409832)
members: aFloat = 1.0; aString = hello; anInt = 10; anObject = null; aCharMethod = a
[super: SuperFooClass[aField=null]]
Generate toString() dialog
toString() Generator: Code Styles
toString() Generator: Content Listing