]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2008, 2011 Mateusz Matela and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * Mateusz Matela <mateusz.matela@gmail.com> - [code manipulation] [dcr] toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=26070 | |
10 | * Mateusz Matela <mateusz.matela@gmail.com> - [toString] Wrong code generated with String concatenation - https://bugs.eclipse.org/bugs/show_bug.cgi?id=275360 | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration; | |
13 | ||
14 | import org.eclipse.core.runtime.CoreException; | |
15 | ||
16 | import org.eclipse.jdt.core.dom.ConditionalExpression; | |
17 | import org.eclipse.jdt.core.dom.Expression; | |
18 | import org.eclipse.jdt.core.dom.InfixExpression; | |
19 | import org.eclipse.jdt.core.dom.ParenthesizedExpression; | |
20 | import org.eclipse.jdt.core.dom.ReturnStatement; | |
21 | import org.eclipse.jdt.core.dom.StringLiteral; | |
22 | import org.eclipse.jdt.core.dom.InfixExpression.Operator; | |
23 | ||
24 | ||
25 | /** | |
26 | * <p> | |
27 | * Implementation of <code>AbstractToStringGenerator</code> that creates <code>toString()</code> | |
28 | * method by concatenating <code>String</code>s | |
29 | * </p> | |
30 | * <p> | |
31 | * Generated methods look like this: | |
32 | * | |
33 | * <pre> | |
34 | * public String toString() { | |
35 | * return "FooClass( field1=" + field1 + ", field2=" + field2 + ", method1()=" + method1 + ")"; | |
36 | * } | |
37 | * </pre> | |
38 | * | |
39 | * </p> | |
40 | * | |
41 | * @since 3.5 | |
42 | */ | |
43 | public class StringConcatenationGenerator extends AbstractToStringGenerator { | |
44 | ||
45 | private class SumExpressionBuilder { | |
46 | private Expression expression; | |
47 | ||
48 | private StringBuffer buffer; | |
49 | ||
50 | public SumExpressionBuilder(Expression expression) { | |
51 | this.expression= expression; | |
52 | buffer= new StringBuffer(); | |
53 | } | |
54 | ||
55 | public Expression getExpression() { | |
56 | flushBuffer(); | |
57 | return expression; | |
58 | } | |
59 | ||
60 | private void flushBuffer() { | |
61 | if (buffer.length() > 0) { | |
62 | StringLiteral bufferedStringLiteral= fAst.newStringLiteral(); | |
63 | bufferedStringLiteral.setLiteralValue(buffer.toString()); | |
64 | buffer.setLength(0); | |
65 | expression= createSumExpression(expression, bufferedStringLiteral); | |
66 | } | |
67 | } | |
68 | ||
69 | public void addString(String string) { | |
70 | buffer.append(string); | |
71 | } | |
72 | ||
73 | public void addExpression(Expression exp) { | |
74 | flushBuffer(); | |
75 | expression= createSumExpression(expression, exp); | |
76 | } | |
77 | } | |
78 | ||
79 | private SumExpressionBuilder toStringExpressionBuilder; | |
80 | ||
81 | @Override | |
82 | protected void initialize() { | |
83 | super.initialize(); | |
84 | toStringExpressionBuilder= new SumExpressionBuilder(null); | |
85 | } | |
86 | ||
87 | @Override | |
88 | protected void complete() throws CoreException { | |
89 | super.complete(); | |
90 | ReturnStatement returnStatement= fAst.newReturnStatement(); | |
91 | returnStatement.setExpression(toStringExpressionBuilder.getExpression()); | |
92 | toStringMethod.getBody().statements().add(returnStatement); | |
93 | } | |
94 | ||
95 | @Override | |
96 | protected void addElement(Object element) { | |
97 | addElement(element, toStringExpressionBuilder); | |
98 | } | |
99 | ||
100 | private void addElement(Object element, SumExpressionBuilder builder) { | |
101 | if (element instanceof String) { | |
102 | builder.addString((String)element); | |
103 | } | |
104 | if (element instanceof Expression) { | |
105 | Expression expr= (Expression)element; | |
106 | if (expr instanceof ConditionalExpression) { | |
107 | ParenthesizedExpression expr2= fAst.newParenthesizedExpression(); | |
108 | expr2.setExpression(expr); | |
109 | expr= expr2; | |
110 | } | |
111 | builder.addExpression(expr); | |
112 | } | |
113 | } | |
114 | ||
115 | @Override | |
116 | protected void addMember(Object member, boolean addSeparator) { | |
117 | boolean[] interfaces= implementsInterfaces(getMemberType(member).getErasure(), new String[] { "java.util.Collection", "java.util.Map" }); //$NON-NLS-1$ //$NON-NLS-2$ | |
118 | if (getContext().isLimitItems() && getContext().isSkipNulls() && (interfaces[0] || interfaces[1] || getMemberType(member).isArray())) { | |
119 | addMemberCheckNull(member, addSeparator); | |
120 | } else { | |
121 | super.addMember(member, addSeparator); | |
122 | } | |
123 | } | |
124 | ||
125 | @Override | |
126 | protected void addMemberCheckNull(Object member, boolean addSeparator) { | |
127 | ConditionalExpression cExpression= fAst.newConditionalExpression(); | |
128 | ||
129 | // member != null ? | |
130 | InfixExpression infExpression= fAst.newInfixExpression(); | |
131 | infExpression.setLeftOperand(createMemberAccessExpression(member, true, true)); | |
132 | infExpression.setRightOperand(fAst.newNullLiteral()); | |
133 | infExpression.setOperator(Operator.NOT_EQUALS); | |
134 | cExpression.setExpression(infExpression); | |
135 | ||
136 | SumExpressionBuilder builder= new SumExpressionBuilder(null); | |
137 | String[] arrayString= getContext().getTemplateParser().getBody(); | |
138 | for (int i= 0; i < arrayString.length; i++) { | |
139 | addElement(processElement(arrayString[i], member), builder); | |
140 | } | |
141 | if (addSeparator) | |
142 | addElement(getContext().getTemplateParser().getSeparator(), builder); | |
143 | cExpression.setThenExpression(builder.getExpression()); | |
144 | ||
145 | StringLiteral literal= fAst.newStringLiteral(); | |
146 | literal.setLiteralValue(getContext().isSkipNulls() ? "" : "null"); //$NON-NLS-1$ //$NON-NLS-2$ | |
147 | cExpression.setElseExpression(literal); | |
148 | ||
149 | ParenthesizedExpression pExpression= fAst.newParenthesizedExpression(); | |
150 | pExpression.setExpression(cExpression); | |
151 | toStringExpressionBuilder.addExpression(pExpression); | |
152 | } | |
153 | ||
154 | private Expression createSumExpression(Expression left, Expression right) { | |
155 | if (right == null) | |
156 | return left; | |
157 | if (left == null) | |
158 | return right; | |
159 | return createInfixExpression(left, Operator.PLUS, right); | |
160 | } | |
161 | ||
162 | ||
163 | } |