KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.JavaCore;
23 import org.eclipse.jdt.core.compiler.IProblem;
24 import org.eclipse.jdt.core.dom.CompilationUnit;
25
26 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
27 import org.eclipse.jdt.internal.corext.fix.IFix;
28 import org.eclipse.jdt.internal.corext.fix.UnusedCodeFix;
29
30 import org.eclipse.jdt.ui.text.java.IProblemLocation;
31
32 /**
33  * Create fixes which can remove unused code
34  * @see org.eclipse.jdt.internal.corext.fix.UnusedCodeFix
35  *
36  */

37 public class UnusedCodeCleanUp extends AbstractCleanUp {
38         
39     public UnusedCodeCleanUp(Map JavaDoc options) {
40         super(options);
41     }
42     
43     public UnusedCodeCleanUp() {
44         super();
45     }
46     
47     /**
48      * {@inheritDoc}
49      */

50     public boolean requireAST(ICompilationUnit unit) throws CoreException {
51         boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
52         
53         return removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
54                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
55                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
56                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) ||
57                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES) ||
58                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS);
59     }
60
61     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
62         if (compilationUnit == null)
63             return null;
64         
65         boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
66         
67         return UnusedCodeFix.createCleanUp(compilationUnit,
68                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS),
69                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS),
70                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS),
71                 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES),
72                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES),
73                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS),
74                 false);
75     }
76     
77
78     /**
79      * {@inheritDoc}
80      */

81     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
82         if (compilationUnit == null)
83             return null;
84         
85         boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
86         return UnusedCodeFix.createCleanUp(compilationUnit, problems,
87                 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS),
88                 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS),
89                 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS),
90                 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES),
91                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES),
92                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS),
93                 false);
94     }
95
96     public Map JavaDoc getRequiredOptions() {
97         Map JavaDoc options= new Hashtable JavaDoc();
98         
99         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS))
100             options.put(JavaCore.COMPILER_PB_UNUSED_IMPORT, JavaCore.WARNING);
101
102         boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS);
103         if (removeMembers && (
104                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
105                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
106                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
107                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)))
108             options.put(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER, JavaCore.WARNING);
109         
110         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
111             options.put(JavaCore.COMPILER_PB_UNUSED_LOCAL, JavaCore.WARNING);
112
113         return options;
114     }
115     
116     /**
117      * {@inheritDoc}
118      */

119     public String JavaDoc[] getDescriptions() {
120         List JavaDoc result= new ArrayList JavaDoc();
121         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS))
122             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedImport_description);
123         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS))
124             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedMethod_description);
125         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS))
126             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedConstructor_description);
127         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES))
128             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedType_description);
129         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS))
130             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedField_description);
131         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
132             result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedVariable_description);
133         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
134     }
135
136     /**
137      * {@inheritDoc}
138      */

139     public String JavaDoc getPreview() {
140         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
141         
142         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) {
143         } else {
144             buf.append("import pack.Bar;\n"); //$NON-NLS-1$
145
}
146         buf.append("class Example {\n"); //$NON-NLS-1$
147
if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)) {
148         } else {
149             buf.append(" private class Sub {}\n"); //$NON-NLS-1$
150
}
151         buf.append(" public Example(boolean b) {}\n"); //$NON-NLS-1$
152
if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS)) {
153         } else {
154             buf.append(" private Example() {}\n"); //$NON-NLS-1$
155
}
156         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS)) {
157         } else {
158             buf.append(" private int fField;\n"); //$NON-NLS-1$
159
}
160         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS)) {
161         } else {
162             buf.append(" private void foo() {}\n"); //$NON-NLS-1$
163
}
164         buf.append(" public void bar() {\n"); //$NON-NLS-1$
165
if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) {
166         } else {
167             buf.append(" int i= 10;\n"); //$NON-NLS-1$
168
}
169         buf.append(" }\n"); //$NON-NLS-1$
170
buf.append("}\n"); //$NON-NLS-1$
171

172         return buf.toString();
173     }
174     
175     /**
176      * {@inheritDoc}
177      */

178     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
179         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) {
180             UnusedCodeFix fix= UnusedCodeFix.createRemoveUnusedImportFix(compilationUnit, problem);
181             if (fix != null)
182                 return true;
183         }
184         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) ||
185                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) ||
186                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) ||
187                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) ||
188                 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
189         {
190             UnusedCodeFix fix= UnusedCodeFix.createUnusedMemberFix(compilationUnit, problem, false);
191             if (fix != null)
192                 return true;
193         }
194         return false;
195     }
196
197     /**
198      * {@inheritDoc}
199      */

200     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
201         int result= 0;
202         IProblem[] problems= compilationUnit.getProblems();
203         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
204             for (int i=0;i<problems.length;i++) {
205                 int id= problems[i].getID();
206                 if (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport ||
207                         id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound)
208                     result++;
209             }
210         }
211         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS))
212             result+= getNumberOfProblems(problems, IProblem.UnusedPrivateMethod);
213         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS))
214             result+= getNumberOfProblems(problems, IProblem.UnusedPrivateConstructor);
215         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES))
216             result+= getNumberOfProblems(problems, IProblem.UnusedPrivateType);
217         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS))
218             result+= getNumberOfProblems(problems, IProblem.UnusedPrivateField);
219         if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES))
220             result+= getNumberOfProblems(problems, IProblem.LocalVariableIsNeverUsed);
221         return result;
222     }
223 }
224
Popular Tags