]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringBuilderChainGenerator.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / codemanipulation / tostringgeneration / StringBuilderChainGenerator.java
CommitLineData
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 *******************************************************************************/
11package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration;
12
13import org.eclipse.core.runtime.CoreException;
14
15import org.eclipse.jdt.core.dom.ASTNode;
16import org.eclipse.jdt.core.dom.Block;
17import org.eclipse.jdt.core.dom.Expression;
18import org.eclipse.jdt.core.dom.IfStatement;
19import org.eclipse.jdt.core.dom.MethodInvocation;
20import org.eclipse.jdt.core.dom.Statement;
21import org.eclipse.jdt.core.dom.StringLiteral;
22import org.eclipse.jdt.core.dom.InfixExpression.Operator;
23
24/**
25 * <p>
26 * Implementation of <code>AbstractToStringGenerator</code> that creates <code>toString()</code>
27 * method using <code>StringBuilder</code> (or <code>StringBuffer</code> for old versions of JDK)
28 * with chained calls to the <code>append</code> method.
29 * </p>
30 * <p>
31 * Generated methods look like this:
32 *
33 * <pre>
34 * public String toString() {
35 * StringBuilder builder= new StringBuilder();
36 * builder.append(&quot;FooClass( field1=&quot;).append(field1).append(&quot;, field2=&quot;).append(field2).append(&quot; )&quot;);
37 * return builder.toString();
38 * }
39 * </pre>
40 *
41 * </p>
42 *
43 * @since 3.5
44 */
45public class StringBuilderChainGenerator extends StringBuilderGenerator {
46 protected Block temporaryBlock= null;
47
48 protected Expression temporaryExpression= null;
49
50 protected void appendExpression(Expression expression) {
51 MethodInvocation appendInvocation= fAst.newMethodInvocation();
52 if (temporaryExpression != null)
53 appendInvocation.setExpression(temporaryExpression);
54 else
55 appendInvocation.setExpression(fAst.newSimpleName(fBuilderVariableName));
56 appendInvocation.setName(fAst.newSimpleName(APPEND_METHOD_NAME));
57 appendInvocation.arguments().add(expression);
58 temporaryExpression= appendInvocation;
59 }
60
61 protected void flushBuffer() {
62 if (fBuffer.length() > 0) {
63 if (temporaryBlock == null)
64 temporaryBlock= toStringMethod.getBody();
65 StringLiteral literal= fAst.newStringLiteral();
66 literal.setLiteralValue(fBuffer.toString());
67 appendExpression(literal);
68 fBuffer.setLength(0);
69 }
70 }
71
72 protected void flushTemporaryExpression() {
73 flushBuffer();
74 if (temporaryBlock != null && temporaryExpression != null) {
75 temporaryBlock.statements().add(fAst.newExpressionStatement(temporaryExpression));
76 temporaryExpression= null;
77 }
78 }
79
80 @Override
81 protected void addElement(Object element, Block block) {
82 if (block != temporaryBlock) {
83 flushTemporaryExpression();
84 temporaryBlock= block;
85 }
86 if (element instanceof String)
87 fBuffer.append((String)element);
88 if (element instanceof Expression) {
89 flushBuffer();
90 appendExpression((Expression)element);
91 }
92 }
93
94 @Override
95 protected void addMemberCheckNull(Object member, boolean addSeparator) {
96 IfStatement ifStatement= fAst.newIfStatement();
97 ifStatement.setExpression(createInfixExpression(createMemberAccessExpression(member, true, true), Operator.NOT_EQUALS, fAst.newNullLiteral()));
98 Block thenBlock= fAst.newBlock();
99 flushTemporaryExpression();
100 String[] arrayString= getContext().getTemplateParser().getBody();
101 for (int i= 0; i < arrayString.length; i++) {
102 addElement(processElement(arrayString[i], member), thenBlock);
103 }
104 if (addSeparator)
105 addElement(getContext().getTemplateParser().getSeparator(), thenBlock);
106 flushTemporaryExpression();
107
108 if (thenBlock.statements().size() == 1 && !getContext().isForceBlocks()) {
109 ifStatement.setThenStatement((Statement)ASTNode.copySubtree(fAst, (ASTNode)thenBlock.statements().get(0)));
110 } else {
111 ifStatement.setThenStatement(thenBlock);
112 }
113 toStringMethod.getBody().statements().add(ifStatement);
114 }
115
116 @Override
117 protected void complete() throws CoreException {
118 flushTemporaryExpression();
119 super.complete();
120 }
121
122}