TOP
You can generate code as shown
below in an instant for
hundreds of Java classes, all at once! Generating toString() implementation
or Copy Constructor had never been simpler.
Depending on your needs, toString() or Copy Constructor can be generated in
one of the following ways for your Java Classes:
-
Single Public Class: Open Package Explorer in Java Perspective,
right click on the Java File which contains this class and select either
JUtils >> Generate toString() or JUtils >> Generate Copy Constructor
from the context menu, depending on what you need.
-
Single Public/Non-Public/Nested/Inner Class: Open the Java File containing
the Class in the Java Editor and right click after placing the caret (keyboard cursor or 'I' cursor)
within the Class. You should be seeing an entry by name JUtils at the end of the context.
Click JUtils >> Generate toString() or JUtils >> Generate Copy Constructor from
this context menu, depending on your need.
-
Multiple Public Classes Under A Single Package: Open Package Explorer in Java
Perspective. Select all the Classes (under any one package at a time) for which toString()
or Copy Constructor is desired. Right click, select either JUtils >> Generate toString()
or JUtils >> Generate Copy Constructor from the context menu to do a batch code
generation.
- hide
/**
* Constructs a <code>String</code>with all attributes
* in name = value format.
*
* @return a <code>String</code> representation
* of this object.
*/
public String toString()
{
final String TAB = " ";
String retValue = "";
retValue = "Person ( "
+ super.toString() + TAB
+ "name = " + this.name + TAB
+ "age = " + this.age + TAB
+ "height = " + this.height + TAB
+ "weight = " + this.weight + TAB
+ "salary = " + this.salary + TAB
+ "address = " + this.address + TAB
+ " )";
return retValue;
}
Alternately, if you don't want so much of string concatenation happening
in your code, you can always switch to either
StringBuffer
or
StringBuilder
mode.
This can be configured using the plugin's
preference page.
The same code generated using
StringBuffer
would look like
this.
- hide
/**
* Constructs a <code>String</code> with all attributes
* in name = value format.
*
* @return a <code>String</code> representation
* of this object.
*/
public String toString()
{
final String TAB = " ";
StringBuffer retValue = new StringBuffer();
retValue.append("Person ( ")
.append(super.toString()).append(TAB)
.append("name = ").append(this.name).append(TAB)
.append("age = ").append(this.age).append(TAB)
.append("height = ").append(this.height).append(TAB)
.append("weight = ").append(this.weight).append(TAB)
.append("salary = ").append(this.salary).append(TAB)
.append("address = ").append(this.address).append(TAB)
.append(" )");
return retValue.toString();
}