Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 526798

Summary: Improve code generated by "Source/Generate toString()" when using "Limit number of items in arrays..."
Product: [Eclipse Project] JDT Reporter: Martin Rust <martin.rust>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: martin.rust
Version: 4.8   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard: stalebug

Description Martin Rust CLA 2017-11-03 05:48:44 EDT
The toString() output with the option "Limit number of items in arrays/collections/maps()" does not give a clue to the reader if the limit was exactly reached or if it was overstepped.
E. g. with a limit of 10, the output of a list containing consecutive Integer from 1 to 20 would be indistinguishable from a list containing only those from 1 to 10.

To generate an output of 
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...]",
I suggest making this simple change to the generated helper method:

private static String toString(Collection<?> collection, int maxLen) {
	StringBuilder builder = new StringBuilder();
	builder.append("[");
	int i = 0;
	Iterator<?> iterator = collection.iterator();
	while (iterator.hasNext() && i < maxLen) {
		if (i > 0) {
			builder.append(", ");
		}
		builder.append(iterator.next());
		i++;
	}
	if (iterator.hasNext()) {
		builder.append(", ...");
	}
	builder.append("]");
	return builder.toString();
}

Note that IMHO the helper method also should be made static.
Comment 1 Eclipse Genie CLA 2020-05-14 03:37:22 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.