]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/ToStringGenerationContext.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core extension / org / eclipse / jdt / internal / corext / codemanipulation / tostringgeneration / ToStringGenerationContext.java
1 /*******************************************************************************
2  * Copyright (c) 2008, 2009 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] finish toString() builder wizard - https://bugs.eclipse.org/bugs/show_bug.cgi?id=267710
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration;
13
14 import java.util.HashSet;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.dom.AST;
22 import org.eclipse.jdt.core.dom.ASTNode;
23 import org.eclipse.jdt.core.dom.Expression;
24 import org.eclipse.jdt.core.dom.FieldAccess;
25 import org.eclipse.jdt.core.dom.IMethodBinding;
26 import org.eclipse.jdt.core.dom.ITypeBinding;
27 import org.eclipse.jdt.core.dom.IVariableBinding;
28 import org.eclipse.jdt.core.dom.Javadoc;
29 import org.eclipse.jdt.core.dom.MethodInvocation;
30 import org.eclipse.jdt.core.dom.Modifier;
31 import org.eclipse.jdt.core.dom.SuperMethodInvocation;
32 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
33 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
34
35 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationMessages;
36 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
37 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2;
38 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
39
40 import org.eclipse.jdt.ui.CodeGeneration;
41
42
43 class ToStringGenerationContext {
44
45         Object[] fSelectedMembers;
46
47         private ToStringTemplateParser fParser;
48
49         ITypeBinding fType;
50
51         ToStringGenerationSettings fSettings;
52         
53         ToStringGenerationSettings.CustomBuilderSettings fCustomBuilderSettings;
54
55         private CompilationUnitRewrite fRewrite;
56
57         ToStringGenerationContext(ToStringTemplateParser parser, Object[] selectedMembers, ToStringGenerationSettings settings, ITypeBinding type,
58                         CompilationUnitRewrite rewrite) {
59                 fParser= parser;
60                 settings.generated_5495634267311453766(selectedMembers, this, type);
61                 fRewrite= rewrite;
62         }
63
64         public ASTRewrite getASTRewrite() {
65                 return fRewrite.getASTRewrite();
66         }
67
68         public AST getAST() {
69                 return fRewrite.getAST();
70         }
71
72         public ICompilationUnit getCompilationUnit() {
73                 return fRewrite.getCu();
74         }
75
76         public ImportRewrite getImportRewrite() {
77                 return fRewrite.getImportRewrite();
78         }
79
80         public int getLimitItemsValue() {
81                 return fSettings.generated_6203512945634259293(this);
82         }
83
84         public Object[] getSelectedMembers() {
85                 return fSelectedMembers;
86         }
87
88         public ToStringTemplateParser getTemplateParser() {
89                 return fParser;
90         }
91
92         public ITypeBinding getTypeBinding() {
93                 return fType;
94         }
95
96         public boolean is50orHigher() {
97                 return fSettings.generated_4720457951684026118(this);
98         }
99
100         public boolean is60orHigher() {
101                 return fSettings.generated_4408472360174240783(this);
102         }
103
104         public boolean isCreateComments() {
105                 return fSettings.generated_3890522325313653657(this);
106         }
107
108         public boolean isCustomArray() {
109                 return fSettings.generated_8719427988826962334(this);
110         }
111
112         public boolean isForceBlocks() {
113                 return fSettings.generated_9053475721656625563(this);
114         }
115
116         public boolean isKeywordThis() {
117                 return fSettings.generated_1061606803051821626(this);
118         }
119
120         public boolean isLimitItems() {
121                 return fSettings.generated_1165715848829217649(this);
122         }
123
124         public boolean isOverrideAnnotation() {
125                 return fSettings.generated_5588797316267707432(this);
126         }
127
128         public boolean isSkipNulls() {
129                 return fSettings.generated_3397837620992553666(this);
130         }
131
132         public String getCustomBuilderClass() {
133                 return fCustomBuilderSettings.generated_9161054712216784088(this);
134         }
135
136         public String getCustomBuilderVariableName() {
137                 return fCustomBuilderSettings.generated_3844037368995630247(this);
138         }
139
140         public String getCustomBuilderAppendMethod() {
141                 return fCustomBuilderSettings.generated_3511844344444339683(this);
142         }
143
144         public String getCustomBuilderResultMethod() {
145                 return fCustomBuilderSettings.generated_1592728035830864358(this);
146         }
147
148         public boolean isCustomBuilderChainedCalls() {
149                 return fCustomBuilderSettings.generated_1251988224814424550(this);
150         }
151
152         public void generated_5960924765249646172(AbstractToStringGenerator abstracttostringgenerator, IMethodBinding objectMethod) throws CoreException {
153                 if (isCreateComments()) {
154                         String docString= CodeGeneration.getMethodComment(getCompilationUnit(), getTypeBinding().getQualifiedName(), abstracttostringgenerator.toStringMethod, objectMethod, StubUtility
155                                         .getLineDelimiterUsed(getCompilationUnit()));
156                         if (docString != null) {
157                                 Javadoc javadoc= (Javadoc)getASTRewrite().createStringPlaceholder(docString, ASTNode.JAVADOC);
158                                 abstracttostringgenerator.toStringMethod.setJavadoc(javadoc);
159                         }
160                 }
161                 if (isOverrideAnnotation() && is50orHigher())
162                         StubUtility2.addOverrideAnnotation(getTypeBinding().getJavaElement().getJavaProject(), getASTRewrite(), abstracttostringgenerator.toStringMethod, objectMethod);
163         }
164
165         public boolean generated_4381819252754149304(AbstractToStringGenerator abstracttostringgenerator, boolean isNonPrimitive, ITypeBinding memberType, boolean[] implementsInterfaces, boolean isCollection) {
166                 boolean isList= implementsInterfaces[1];
167                 boolean isMap= implementsInterfaces[2];
168         
169                 if (isLimitItems() && (isCollection || isMap) && !isList) {
170                         abstracttostringgenerator.needCollectionToStringMethod= true;
171                 }
172                 if (isCustomArray() && memberType.isArray()) {
173                         ITypeBinding componentType= memberType.getComponentType();
174                         if (componentType.isPrimitive() && (!is50orHigher() || (!is60orHigher() && isLimitItems()))) {
175                                 if (!abstracttostringgenerator.typesThatNeedArrayToStringMethod.contains(componentType))
176                                         abstracttostringgenerator.typesThatNeedArrayToStringMethod.add(componentType);
177                         } else if (!componentType.isPrimitive())
178                                 isNonPrimitive= true;
179                 }
180                 return isNonPrimitive;
181         }
182
183         public Object generated_6573513055157508799(AbstractToStringGenerator abstracttostringgenerator, String templateElement, Object result) {
184                 if (templateElement == ToStringTemplateParser.OBJECT_NAME_VARIABLE) {
185                         result= getTypeBinding().getName();
186                 }
187                 if (templateElement == ToStringTemplateParser.OBJECT_GET_NAME_VARIABLE) {
188                         //this.getClass().getName()
189                         MethodInvocation getClassInvocation= abstracttostringgenerator.fAst.newMethodInvocation();
190                         if (isKeywordThis())
191                                 getClassInvocation.setExpression(abstracttostringgenerator.fAst.newThisExpression());
192                         getClassInvocation.setName(abstracttostringgenerator.fAst.newSimpleName("getClass")); //$NON-NLS-1$
193                         MethodInvocation getNameInvocation= abstracttostringgenerator.fAst.newMethodInvocation();
194                         getNameInvocation.setExpression(getClassInvocation);
195                         getNameInvocation.setName(abstracttostringgenerator.fAst.newSimpleName("getName")); //$NON-NLS-1$
196                         result= getNameInvocation;
197                 }
198                 if (templateElement == ToStringTemplateParser.OBJECT_SUPER_TOSTRING_VARIABLE) {
199                         //super.toString()
200                         SuperMethodInvocation superToStringInvocation= abstracttostringgenerator.fAst.newSuperMethodInvocation();
201                         superToStringInvocation.setName(abstracttostringgenerator.fAst.newSimpleName(AbstractToStringGenerator.METHODNAME_TO_STRING));
202                         result= superToStringInvocation;
203                 }
204                 if (templateElement == ToStringTemplateParser.OBJECT_HASHCODE_VARIABLE) {
205                         //this.hashCode()
206                         MethodInvocation hashCodeInvocation= abstracttostringgenerator.fAst.newMethodInvocation();
207                         if (isKeywordThis())
208                                 hashCodeInvocation.setExpression(abstracttostringgenerator.fAst.newThisExpression());
209                         hashCodeInvocation.setName(abstracttostringgenerator.fAst.newSimpleName("hashCode")); //$NON-NLS-1$
210                         result= hashCodeInvocation;
211                 }
212                 return result;
213         }
214
215         public Expression generated_2098369527780276835(AbstractToStringGenerator abstracttostringgenerator, Object member) {
216                 if (member instanceof IVariableBinding) {
217                         if (isKeywordThis()) {
218                                 FieldAccess fa= abstracttostringgenerator.fAst.newFieldAccess();
219                                 fa.setExpression(abstracttostringgenerator.fAst.newThisExpression());
220                                 fa.setName(abstracttostringgenerator.fAst.newSimpleName(((IVariableBinding)member).getName()));
221                                 return fa;
222                         }
223                         return abstracttostringgenerator.fAst.newSimpleName(((IVariableBinding)member).getName());
224                 }
225                 if (member instanceof IMethodBinding) {
226                         if (((IMethodBinding)member).getName().equals(AbstractToStringGenerator.METHODNAME_TO_STRING)) {
227                                 SuperMethodInvocation invocation= abstracttostringgenerator.fAst.newSuperMethodInvocation();
228                                 invocation.setName(abstracttostringgenerator.fAst.newSimpleName(((IMethodBinding)member).getName()));
229                                 return invocation;
230                         }
231                         MethodInvocation invocation= abstracttostringgenerator.fAst.newMethodInvocation();
232                         if (isKeywordThis()) {
233                                 invocation.setExpression(abstracttostringgenerator.fAst.newThisExpression());
234                         }
235                         invocation.setName(abstracttostringgenerator.fAst.newSimpleName(((IMethodBinding)member).getName()));
236                         return invocation;
237                 }
238                 return null;
239         }
240
241         public String generated_907652555048609483(AbstractToStringGenerator abstracttostringgenerator, String baseName, int variableKind) {
242                 if (abstracttostringgenerator.excluded == null) {
243                         abstracttostringgenerator.excluded= new HashSet<String>();
244                         IVariableBinding[] fields= getTypeBinding().getDeclaredFields();
245                         for (int i= 0; i < fields.length; i++) {
246                                 abstracttostringgenerator.excluded.add(fields[i].getName());
247                         }
248                         ITypeBinding superType= getTypeBinding().getSuperclass();
249                         while (superType != null) {
250                                 fields= superType.getDeclaredFields();
251                                 for (int i= 0; i < fields.length; i++) {
252                                         if (!Modifier.isPrivate(fields[i].getModifiers())) {
253                                                 abstracttostringgenerator.excluded.add(fields[i].getName());
254                                         }
255                                 }
256                                 superType= superType.getSuperclass();
257                         }
258                         ITypeBinding[] types= getTypeBinding().getDeclaredTypes();
259                         for (int i= 0; i < types.length; i++) {
260                                 abstracttostringgenerator.excluded.add(types[i].getName());
261                         }
262                         superType= getTypeBinding().getSuperclass();
263                         while (superType != null) {
264                                 types= superType.getDeclaredTypes();
265                                 for (int i= 0; i < types.length; i++) {
266                                         if (!Modifier.isPrivate(types[i].getModifiers())) {
267                                                 abstracttostringgenerator.excluded.add(types[i].getName());
268                                         }
269                                 }
270                                 superType= superType.getSuperclass();
271                         }
272                 }
273                 return StubUtility.getVariableNameSuggestions(variableKind, getCompilationUnit().getJavaProject(), baseName, 0, abstracttostringgenerator.excluded, true)[0];
274         }
275
276         public void generated_7124391292801602460(RefactoringStatus status) {
277                 if (isCustomArray() || isLimitItems())
278                         status.addWarning(CodeGenerationMessages.GenerateToStringOperation_warning_no_arrays_collections_with_this_style);
279         }
280
281 }