Jump to: navigation, search

Difference between revisions of "ToString() generation"

(Nice to have)
(Added Code Template Suggestion 2, Mentor info and Glossary)
Line 11: Line 11:
 
*Student: Mateusz Matela (IRC: mmati)
 
*Student: Mateusz Matela (IRC: mmati)
  
*Mentor: Markus Alexander Kuppe (IRC: lemmy)
+
*Mentor: Markus Alexander Kuppe (IRC: lemmy), Mike Haller (IRC: mhaller)
 +
 
 +
*CVS: dev.eclipse.org/cvsroot/technology/org.eclipse.soc/.../ (TBD)
  
 
== Planned features ==
 
== Planned features ==
Line 18: Line 20:
  
 
* Integration with 'Generate hashCode() and equals()' dialog
 
* Integration with 'Generate hashCode() and equals()' dialog
* Changing the string format with templates
 
* Changing the order of the fields
 
 
* Turning generators on/off separately
 
* Turning generators on/off separately
 +
* Changing the output string format with templates
 +
* Changing the order of the fields
  
 
=== Should have ===
 
=== Should have ===
Line 29: Line 31:
 
* Option to ignore default toString() of arrays and list elements instead
 
* Option to ignore default toString() of arrays and list elements instead
 
* Option to set the maximum number of elements to list from arrays/Collections
 
* Option to set the maximum number of elements to list from arrays/Collections
* Refactorings if e.g. a field is renamed/removed/added (consider save actions. mark fields for permanent exclusion by)
+
* Option to chose the insertion point of the method (as this is already there for equals() and hashCode())
* NLS support (if desirable in toString)
+
  
 
=== Nice to have ===
 
=== Nice to have ===
  
* Ability to add different implementations of the toString generator (e.g. extension points)
+
* Ability to add different implementations of the toString generator or use multiple code templates (e.g. extension points)
 
* Change hashCode and equals generators to optionally use getters
 
* Change hashCode and equals generators to optionally use getters
 
* Annotations (would require Java5 though)
 
* Annotations (would require Java5 though)
 +
* Refactorings if e.g. a field is renamed/removed/added (consider save actions. mark fields for permanent exclusion by)
 +
* NLS support (if desirable in toString)
  
=== Community proposals ===
+
=== Glossary ===
 +
 
 +
* Code Template: The template used to generate the method signature and body of the toString() method
 +
* Output String Format: The format of the String generated by the implementation of the toString() method
 +
 
 +
== Community proposals ==
  
 
Feel free to add your ideas
 
Feel free to add your ideas
Line 53: Line 61:
 
* Nice to have: Integration with [http://www.javalobby.org/java/forums/t16350.html Detail Formatters] from the Debugger
 
* Nice to have: Integration with [http://www.javalobby.org/java/forums/t16350.html Detail Formatters] from the Debugger
  
==== Template suggestion 1 ====
+
==== Code Template suggestion 1 ====
  
 
An example for an easily editable format:
 
An example for an easily editable format:
Line 76: Line 84:
  
 
--[[User:Digulla.hepe.com|Digulla.hepe.com]] 07:23, 15 May 2008 (EDT)
 
--[[User:Digulla.hepe.com|Digulla.hepe.com]] 07:23, 15 May 2008 (EDT)
 +
 +
==== Code Template suggestion 2 ====
 +
 +
<source lang="java">
 +
public String toString()
 +
{
 +
    return String.format("%s[field=%s, field2=%s]", super.toString(), field, field2);
 +
}
 +
</source>
 +
 +
* Reordering of fields is done in the dialog and the toString() method is regenerated
 +
* No String concatenation required
 +
* Still easy to add new fields manually if desired
 +
* Bad to see which fields are associated with which variables, as they are far apart
  
 
[[Category:SOC]]
 
[[Category:SOC]]

Revision as of 10:07, 24 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), Mike Haller (IRC: mhaller)
  • CVS: dev.eclipse.org/cvsroot/technology/org.eclipse.soc/.../ (TBD)

Planned features

Must have

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

Should have

  • Option to use getters instead of accessing the fields directly
    • Not just fields/getters but all methods
  • 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
  • Option to chose the insertion point of the method (as this is already there for equals() and hashCode())

Nice to have

  • Ability to add different implementations of the toString generator or use multiple code templates (e.g. extension points)
  • Change hashCode and equals generators to optionally use getters
  • Annotations (would require Java5 though)
  • Refactorings if e.g. a field is renamed/removed/added (consider save actions. mark fields for permanent exclusion by)
  • NLS support (if desirable in toString)

Glossary

  • Code Template: The template used to generate the method signature and body of the toString() method
  • Output String Format: The format of the String generated by the implementation of the toString() method

Community proposals

Feel free to add your ideas

  • Should: Support multiple styles (per project option)
    • 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()
  • Nice to have: Integration with Detail Formatters from the Debugger

Code 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)

Code Template suggestion 2

public String toString()
{
    return String.format("%s[field=%s, field2=%s]", super.toString(), field, field2);
}
  • Reordering of fields is done in the dialog and the toString() method is regenerated
  • No String concatenation required
  • Still easy to add new fields manually if desired
  • Bad to see which fields are associated with which variables, as they are far apart