Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "ToString() generation"

(String concatenation allows the compiler to generate optimized code. Also it's more readable than StringBuilder)
(Added suggestion for a template)
Line 47: Line 47:
 
* Nice to have: Accessible through quickfix/content assistant
 
* Nice to have: Accessible through quickfix/content assistant
 
* Nice to have: Support options for include/exclude hashcode, super.toString(), class name, use System.identityHashCode() or Object's hashCode()
 
* Nice to have: Support options for include/exclude hashcode, super.toString(), class name, use System.identityHashCode() or Object's hashCode()
 +
 +
==== Template suggestion 1 ====
 +
 +
An example for an easily editable format:
 +
 +
<source lang="java">
 +
public String toString()
 +
{
 +
    return super.toString ()
 +
        + " (field=" + field
 +
        + ", field2=" + field2
 +
        + ")"
 +
    ;
 +
}
 +
</source>
 +
 +
This allows to reorder lines by moving (and replacing " (" with ", " in one-of-many cases). Faster than using StringBuilder() or any other way. Fields can be added and removed with least amount of fuzz. Code and result is readable for humans. No ambiguity of result (only "null" and null can't be told apart). Also, the format is easily parsable if you want to offer a "do it again" dialog.
 +
 +
Things to keep in mind:
 +
 +
* The current formatter (where to put blanks and the curly braces)
 +
* Use the field directly or call the getter. Direct access is faster but breaks when a getter is overloaded in a subclass.
 +
 +
--[[User:Digulla.hepe.com|Digulla.hepe.com]] 07:23, 15 May 2008 (EDT)
  
 
[[Category:SOC]]
 
[[Category:SOC]]

Revision as of 07:23, 15 May 2008

About

The toString() method is widely used to represent objects as human-readable text and practically every class should implement it. Usually it is very simple: generated string contains the object type and lists values of the most important fields. That's why process of writing such methods can easilly be automated. Eclipse, as the best java IDE in the world, should include functionality of automatic toString() method generation to make its users lives even simpler. Implementing it is the aim of this project.

There were many bugs posted regarding this problem, see bug 26070 for details.

This project is part of 2008 Google Summer of Code.

Participants:

  • Student: Mateusz Matela (IRC: mmati)
  • Mentor: Markus Alexander Kuppe (IRC: lemmy)

Planned features

Must have

  • Integration with 'Generate hashCode() and equals()' dialog
  • Changing the string format with templates
  • Changing the order of the fields
  • Turning generators on/off separately

Should have

  • Option to use getters instead of accessing the fields directly
  • Option to skip fields with null value
  • Option to ignore default toString() of arrays and list elements instead
  • Option to set the maximum number of elements to list from arrays/Collections

Nice to have

  • Ability to add different implementations of the toString generator (e.g. extension points)
  • Change hashCode and equals generators to optionally use getters

Community proposals

Feel free to add your ideas

  • Should: Support multiple styles
    • Apache Commons-Lang ToStringBuilder
    • Spring Framework's ToStringCreator
    • String concatenation
    • StringBuilder/StringBuffer
    • String.format()
    • Single return value (e.g. if only one field is chosen, for existing primary keys or Objects with 'names')
  • Nice to have: Accessible through quickfix/content assistant
  • Nice to have: Support options for include/exclude hashcode, super.toString(), class name, use System.identityHashCode() or Object's hashCode()

Template suggestion 1

An example for an easily editable format:

public String toString()
{
    return super.toString ()
        + " (field=" + field
        + ", field2=" + field2
        + ")"
    ;
}

This allows to reorder lines by moving (and replacing " (" with ", " in one-of-many cases). Faster than using StringBuilder() or any other way. Fields can be added and removed with least amount of fuzz. Code and result is readable for humans. No ambiguity of result (only "null" and null can't be told apart). Also, the format is easily parsable if you want to offer a "do it again" dialog.

Things to keep in mind:

  • The current formatter (where to put blanks and the curly braces)
  • Use the field directly or call the getter. Direct access is faster but breaks when a getter is overloaded in a subclass.

--Digulla.hepe.com 07:23, 15 May 2008 (EDT)

Back to the top