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
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;
14 import java.util.HashSet;
16 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
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;
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;
40 import org.eclipse.jdt.ui.CodeGeneration;
43 class ToStringGenerationContext {
45 Object[] fSelectedMembers;
47 private ToStringTemplateParser fParser;
51 ToStringGenerationSettings fSettings;
53 ToStringGenerationSettings.CustomBuilderSettings fCustomBuilderSettings;
55 private CompilationUnitRewrite fRewrite;
57 ToStringGenerationContext(ToStringTemplateParser parser, Object[] selectedMembers, ToStringGenerationSettings settings, ITypeBinding type,
58 CompilationUnitRewrite rewrite) {
60 settings.generated_5495634267311453766(selectedMembers, this, type);
64 public ASTRewrite getASTRewrite() {
65 return fRewrite.getASTRewrite();
69 return fRewrite.getAST();
72 public ICompilationUnit getCompilationUnit() {
73 return fRewrite.getCu();
76 public ImportRewrite getImportRewrite() {
77 return fRewrite.getImportRewrite();
80 public int getLimitItemsValue() {
81 return fSettings.generated_6203512945634259293(this);
84 public Object[] getSelectedMembers() {
85 return fSelectedMembers;
88 public ToStringTemplateParser getTemplateParser() {
92 public ITypeBinding getTypeBinding() {
96 public boolean is50orHigher() {
97 return fSettings.generated_4720457951684026118(this);
100 public boolean is60orHigher() {
101 return fSettings.generated_4408472360174240783(this);
104 public boolean isCreateComments() {
105 return fSettings.generated_3890522325313653657(this);
108 public boolean isCustomArray() {
109 return fSettings.generated_8719427988826962334(this);
112 public boolean isForceBlocks() {
113 return fSettings.generated_9053475721656625563(this);
116 public boolean isKeywordThis() {
117 return fSettings.generated_1061606803051821626(this);
120 public boolean isLimitItems() {
121 return fSettings.generated_1165715848829217649(this);
124 public boolean isOverrideAnnotation() {
125 return fSettings.generated_5588797316267707432(this);
128 public boolean isSkipNulls() {
129 return fSettings.generated_3397837620992553666(this);
132 public String getCustomBuilderClass() {
133 return fCustomBuilderSettings.generated_9161054712216784088(this);
136 public String getCustomBuilderVariableName() {
137 return fCustomBuilderSettings.generated_3844037368995630247(this);
140 public String getCustomBuilderAppendMethod() {
141 return fCustomBuilderSettings.generated_3511844344444339683(this);
144 public String getCustomBuilderResultMethod() {
145 return fCustomBuilderSettings.generated_1592728035830864358(this);
148 public boolean isCustomBuilderChainedCalls() {
149 return fCustomBuilderSettings.generated_1251988224814424550(this);
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);
161 if (isOverrideAnnotation() && is50orHigher())
162 StubUtility2.addOverrideAnnotation(getTypeBinding().getJavaElement().getJavaProject(), getASTRewrite(), abstracttostringgenerator.toStringMethod, objectMethod);
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];
169 if (isLimitItems() && (isCollection || isMap) && !isList) {
170 abstracttostringgenerator.needCollectionToStringMethod= true;
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;
180 return isNonPrimitive;
183 public Object generated_6573513055157508799(AbstractToStringGenerator abstracttostringgenerator, String templateElement, Object result) {
184 if (templateElement == ToStringTemplateParser.OBJECT_NAME_VARIABLE) {
185 result= getTypeBinding().getName();
187 if (templateElement == ToStringTemplateParser.OBJECT_GET_NAME_VARIABLE) {
188 //this.getClass().getName()
189 MethodInvocation getClassInvocation= abstracttostringgenerator.fAst.newMethodInvocation();
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;
198 if (templateElement == ToStringTemplateParser.OBJECT_SUPER_TOSTRING_VARIABLE) {
200 SuperMethodInvocation superToStringInvocation= abstracttostringgenerator.fAst.newSuperMethodInvocation();
201 superToStringInvocation.setName(abstracttostringgenerator.fAst.newSimpleName(AbstractToStringGenerator.METHODNAME_TO_STRING));
202 result= superToStringInvocation;
204 if (templateElement == ToStringTemplateParser.OBJECT_HASHCODE_VARIABLE) {
206 MethodInvocation hashCodeInvocation= abstracttostringgenerator.fAst.newMethodInvocation();
208 hashCodeInvocation.setExpression(abstracttostringgenerator.fAst.newThisExpression());
209 hashCodeInvocation.setName(abstracttostringgenerator.fAst.newSimpleName("hashCode")); //$NON-NLS-1$
210 result= hashCodeInvocation;
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()));
223 return abstracttostringgenerator.fAst.newSimpleName(((IVariableBinding)member).getName());
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()));
231 MethodInvocation invocation= abstracttostringgenerator.fAst.newMethodInvocation();
232 if (isKeywordThis()) {
233 invocation.setExpression(abstracttostringgenerator.fAst.newThisExpression());
235 invocation.setName(abstracttostringgenerator.fAst.newSimpleName(((IMethodBinding)member).getName()));
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());
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());
256 superType= superType.getSuperclass();
258 ITypeBinding[] types= getTypeBinding().getDeclaredTypes();
259 for (int i= 0; i < types.length; i++) {
260 abstracttostringgenerator.excluded.add(types[i].getName());
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());
270 superType= superType.getSuperclass();
273 return StubUtility.getVariableNameSuggestions(variableKind, getCompilationUnit().getJavaProject(), baseName, 0, abstracttostringgenerator.excluded, true)[0];
276 public void generated_7124391292801602460(RefactoringStatus status) {
277 if (isCustomArray() || isLimitItems())
278 status.addWarning(CodeGenerationMessages.GenerateToStringOperation_warning_no_arrays_collections_with_this_style);