KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > CodeStyleCleanUp


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation 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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.CoreException;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.core.compiler.IProblem;
23 import org.eclipse.jdt.core.dom.CompilationUnit;
24
25 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
26 import org.eclipse.jdt.internal.corext.fix.CodeStyleFix;
27 import org.eclipse.jdt.internal.corext.fix.IFix;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 /**
32  * Creates fixes which can resolve code style issues
33  * @see org.eclipse.jdt.internal.corext.fix.CodeStyleFix
34  */

35 public class CodeStyleCleanUp extends AbstractCleanUp {
36
37     public CodeStyleCleanUp() {
38         this(null);
39     }
40     
41     public CodeStyleCleanUp(Map JavaDoc options) {
42         super(options);
43     }
44     
45     /**
46      * {@inheritDoc}
47      */

48     public boolean requireAST(ICompilationUnit unit) throws CoreException {
49         boolean nonStaticFields= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS);
50         boolean nonStaticMethods= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS);
51         boolean qualifyStatic= isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS);
52         
53         return nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS) ||
54                qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS) ||
55                qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD) ||
56                qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS) ||
57                nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS) ||
58                qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD) ||
59                nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY) ||
60                nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY);
61     }
62     
63     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
64         if (compilationUnit == null)
65             return null;
66         
67         boolean nonStaticFields= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS);
68         boolean nonStaticMethods= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS);
69         boolean qualifyStatic= isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS);
70         
71         return CodeStyleFix.createCleanUp(compilationUnit,
72                 nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS),
73                 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS),
74                 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD),
75                 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS),
76                 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS),
77                 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD),
78                 nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY),
79                 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY)
80                 );
81     }
82     
83     /**
84      * {@inheritDoc}
85      */

86     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
87         if (compilationUnit == null)
88             return null;
89         
90         return CodeStyleFix.createCleanUp(compilationUnit, problems,
91                 isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS),
92                 isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS),
93                 isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS));
94     }
95
96     public Map JavaDoc getRequiredOptions() {
97         Map JavaDoc options= new Hashtable JavaDoc();
98         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS))
99             options.put(JavaCore.COMPILER_PB_STATIC_ACCESS_RECEIVER, JavaCore.WARNING);
100         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS))
101             options.put(JavaCore.COMPILER_PB_INDIRECT_STATIC_ACCESS, JavaCore.WARNING);
102         return options;
103     }
104
105     /**
106      * {@inheritDoc}
107      */

108     public String JavaDoc[] getDescriptions() {
109         List JavaDoc result= new ArrayList JavaDoc();
110         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS))
111             result.add(MultiFixMessages.CodeStyleMultiFix_AddThisQualifier_description);
112         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS))
113             result.add(MultiFixMessages.CodeStyleCleanUp_QualifyNonStaticMethod_description);
114         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY))
115             result.add(MultiFixMessages.CodeStyleCleanUp_removeFieldThis_description);
116         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY))
117             result.add(MultiFixMessages.CodeStyleCleanUp_removeMethodThis_description);
118         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD))
119             result.add(MultiFixMessages.CodeStyleMultiFix_QualifyAccessToStaticField);
120         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD))
121             result.add(MultiFixMessages.CodeStyleCleanUp_QualifyStaticMethod_description);
122         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS))
123             result.add(MultiFixMessages.CodeStyleMultiFix_ChangeNonStaticAccess_description);
124         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS))
125             result.add(MultiFixMessages.CodeStyleMultiFix_ChangeIndirectAccessToStaticToDirect);
126         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
127     }
128     
129     public String JavaDoc getPreview() {
130         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
131         
132         buf.append("private int value;\n"); //$NON-NLS-1$
133
buf.append("public int get() {\n"); //$NON-NLS-1$
134
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) {
135             buf.append(" return this.value + this.value;\n"); //$NON-NLS-1$
136
} else if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY)) {
137             buf.append(" return value + value;\n"); //$NON-NLS-1$
138
} else {
139             buf.append(" return this.value + value;\n"); //$NON-NLS-1$
140
}
141         buf.append("}\n"); //$NON-NLS-1$
142
buf.append("\n"); //$NON-NLS-1$
143
buf.append("public int getZero() {\n"); //$NON-NLS-1$
144
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS)) {
145             buf.append(" return this.get() - this.get();\n"); //$NON-NLS-1$
146
} else if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY)) {
147             buf.append(" return get() - get();\n"); //$NON-NLS-1$
148
} else {
149             buf.append(" return this.get() - get();\n"); //$NON-NLS-1$
150
}
151         buf.append("}\n"); //$NON-NLS-1$
152
buf.append("\n"); //$NON-NLS-1$
153
buf.append("class E {\n"); //$NON-NLS-1$
154
buf.append(" public static int NUMBER;\n"); //$NON-NLS-1$
155
buf.append(" public static void set(int i) {\n"); //$NON-NLS-1$
156
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD)) {
157             buf.append(" E.NUMBER= i;\n"); //$NON-NLS-1$
158
} else {
159             buf.append(" NUMBER= i;\n"); //$NON-NLS-1$
160
}
161         buf.append(" }\n"); //$NON-NLS-1$
162
buf.append("\n"); //$NON-NLS-1$
163
buf.append(" public void reset() {\n"); //$NON-NLS-1$
164
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD)) {
165             buf.append(" E.set(0);\n"); //$NON-NLS-1$
166
} else {
167             buf.append(" set(0);\n"); //$NON-NLS-1$
168
}
169         buf.append(" }\n"); //$NON-NLS-1$
170
buf.append("}\n"); //$NON-NLS-1$
171
buf.append("\n"); //$NON-NLS-1$
172
buf.append("class ESub extends E {\n"); //$NON-NLS-1$
173
buf.append(" public void reset() {\n"); //$NON-NLS-1$
174
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) {
175             buf.append(" E.NUMBER= 0;\n"); //$NON-NLS-1$
176
} else {
177             buf.append(" ESub.NUMBER= 0;\n"); //$NON-NLS-1$
178
}
179         buf.append(" }\n"); //$NON-NLS-1$
180
buf.append("}\n"); //$NON-NLS-1$
181
buf.append("\n"); //$NON-NLS-1$
182
buf.append("public void dec() {\n"); //$NON-NLS-1$
183
if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) {
184             buf.append(" E.NUMBER--;\n"); //$NON-NLS-1$
185
} else {
186             buf.append(" (new E()).NUMBER--;\n"); //$NON-NLS-1$
187
}
188         buf.append("}\n"); //$NON-NLS-1$
189

190         return buf.toString();
191     }
192
193     /**
194      * {@inheritDoc}
195      */

196     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
197         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) {
198             CodeStyleFix fix= CodeStyleFix.createAddFieldQualifierFix(compilationUnit, problem);
199             if (fix != null)
200                 return true;
201         }
202         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) {
203             CodeStyleFix fix= CodeStyleFix.createIndirectAccessToStaticFix(compilationUnit, problem);
204             if (fix != null)
205                 return true;
206         }
207         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) {
208             CodeStyleFix[] fixes= CodeStyleFix.createNonStaticAccessFixes(compilationUnit, problem);
209             if (fixes != null && fixes.length > 0)
210                 return true;
211         }
212         return false;
213     }
214
215     /**
216      * {@inheritDoc}
217      */

218     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
219         int result= 0;
220         IProblem[] problems= compilationUnit.getProblems();
221         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS))
222             result+= getNumberOfProblems(problems, IProblem.UnqualifiedFieldAccess);
223         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) {
224             for (int i=0;i<problems.length;i++) {
225                 int id= problems[i].getID();
226                 if (id == IProblem.IndirectAccessToStaticField || id == IProblem.IndirectAccessToStaticMethod)
227                     result++;
228             }
229         }
230         if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) {
231             for (int i=0;i<problems.length;i++) {
232                 int id= problems[i].getID();
233                 if (id == IProblem.NonStaticAccessToStaticField || id == IProblem.NonStaticAccessToStaticMethod)
234                     result++;
235             }
236         }
237         return result;
238     }
239
240 }
241
Popular Tags