Bug 270462: [toString] toString wizard generates wrong code
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/GenerateToStringTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/GenerateToStringTest.java
index 6abb7f3..be5b621 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/GenerateToStringTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/core/source/GenerateToStringTest.java
@@ -8,6 +8,7 @@
* Contributors:
* Mateusz Matela <mateusz.matela@gmail.com> - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070
* Mateusz Matela <mateusz.matela@gmail.com> - [toString] finish toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=267710
+ * Mateusz Matela <mateusz.matela@gmail.com> - [toString] toString wizard generates wrong code - https://bugs.eclipse.org/bugs/show_bug.cgi?id=270462
*******************************************************************************/
package org.eclipse.jdt.ui.tests.core.source;
@@ -2150,7 +2151,7 @@
+ " Collection<Integer> integerCollection;\r\n"
+ " public String toString() {\r\n"
+ " final int maxLen = 10;\r\n"
- + " return MessageFormat.format(\"A [AArray={1}, aBool={2}, anA={3}, floatArray={4}, hashMap={5}, intArray={6}, integerCollection={7}, list={8}, object={9}, stringArray={10}, wildCollection={11}, charArrayMethod()={12}, floatArrayMethod()={13}]\", new Object[]{AArray, Boolean.valueOf(aBool), anA, floatArray, hashMap != null ? toString(hashMap.entrySet(), maxLen) : null, intArray, integerCollection != null ? toString(integerCollection, maxLen) : null, list != null ? toString(list, maxLen) : null, object, stringArray, wildCollection != null ? toString(wildCollection, maxLen) : null, charArrayMethod(), floatArrayMethod()});\r\n"
+ + " return MessageFormat.format(\"A [AArray={0}, aBool={1}, anA={2}, floatArray={3}, hashMap={4}, intArray={5}, integerCollection={6}, list={7}, object={8}, stringArray={9}, wildCollection={10}, charArrayMethod()={11}, floatArrayMethod()={12}]\", new Object[]{AArray, new Boolean(aBool), anA, floatArray, hashMap != null ? toString(hashMap.entrySet(), maxLen) : null, intArray, integerCollection != null ? toString(integerCollection, maxLen) : null, list != null ? toString(list, maxLen) : null, object, stringArray, wildCollection != null ? toString(wildCollection, maxLen) : null, charArrayMethod(), floatArrayMethod()});\r\n"
+ " }\r\n" + " private String toString(Collection collection, int maxLen) {\r\n" + " StringBuffer buffer = new StringBuffer();\r\n" + " buffer.append(\"[\");\r\n"
+ " int i = 0;\r\n" + " for (Iterator iterator = collection.iterator(); iterator.hasNext() && i < maxLen; i++) {\r\n" + " if (i > 0) {\r\n" + " buffer.append(\", \");\r\n"
+ " }\r\n" + " buffer.append(iterator.next());\r\n" + " }\r\n" + " buffer.append(\"]\");\r\n" + " return buffer.toString();\r\n" + " }\r\n" + " \r\n" + "}\r\n" + "";
diff --git a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringFormatGenerator.java b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringFormatGenerator.java
index 40c7f93..c445842 100644
--- a/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringFormatGenerator.java
+++ b/org.eclipse.jdt.ui/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringFormatGenerator.java
@@ -7,6 +7,7 @@
*
* Contributors:
* Mateusz Matela <mateusz.matela@gmail.com> - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070
+ * Mateusz Matela <mateusz.matela@gmail.com> - [toString] toString wizard generates wrong code - https://bugs.eclipse.org/bugs/show_bug.cgi?id=270462
*******************************************************************************/
package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration;
@@ -17,6 +18,7 @@
import org.eclipse.jdt.core.dom.ArrayCreation;
import org.eclipse.jdt.core.dom.ArrayInitializer;
+import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
@@ -94,7 +96,7 @@
if (getContext().is50orHigher()) {
buffer.append("%s"); //$NON-NLS-1$
} else {
- buffer.append("{" + arguments.size() + "}"); //$NON-NLS-1$ //$NON-NLS-2$
+ buffer.append("{" + (arguments.size() - 1) + "}"); //$NON-NLS-1$ //$NON-NLS-2$
}
}
}
@@ -112,7 +114,10 @@
if (typeName.equals("float"))nonPrimitiveType= "java.lang.Float"; //$NON-NLS-1$ //$NON-NLS-2$
if (typeName.equals("double"))nonPrimitiveType= "java.lang.Double"; //$NON-NLS-1$ //$NON-NLS-2$
if (typeName.equals("boolean"))nonPrimitiveType= "java.lang.Boolean"; //$NON-NLS-1$ //$NON-NLS-2$
- return createMethodInvocation(addImport(nonPrimitiveType), "valueOf", super.createMemberAccessExpression(member, true, true)); //$NON-NLS-1$
+ ClassInstanceCreation classInstance= fAst.newClassInstanceCreation();
+ classInstance.setType(fAst.newSimpleType(fAst.newSimpleName(addImport(nonPrimitiveType))));
+ classInstance.arguments().add(super.createMemberAccessExpression(member, true, true));
+ return classInstance;
}
return super.createMemberAccessExpression(member, ignoreArraysCollections, ignoreNulls);
}