Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Eclipse Java Development User Guide
Previous Page Home Next Page

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)
${} inserts the first member's 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

  1. The default template is a good example:

    ${} [${}=${member.value}, ${otherMembers}]

    The output string for this template looks like this:

    FooClass[aFloat=1.0, aString=hello, anInt=10, anObject=null, aCharMethod()=a]
  2. Multiple line output is also available:

    ${object.getClassName} {
    	${}: ${member.value}

    Example result:

    FooClass {
    	aFloat: 1.0
    	aString: hello
    	anInt: 10
    	anObject: null
    	aCharMethod: a
  3. If you enclose a member in braces, don't forget to do the same with ${otherMembers} variable:


    Here's the effect:

  4. ${object.*} variables can be used at the beginning and at the end of the template:

    ${object.getClassName} (hashcode:${object.hashCode})
    	members: ${} = ${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]]

Related reference

Generate toString() dialog
toString() Generator: Code Styles
toString() Generator: Content Listing

  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire