KickJava   Java API By Example, From Geeks To Geeks.

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


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.IFix;
27 import org.eclipse.jdt.internal.corext.fix.UnusedCodeFix;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 public class UnnecessaryCodeCleanUp extends AbstractCleanUp {
32         
33     public UnnecessaryCodeCleanUp(Map JavaDoc options) {
34         super(options);
35     }
36     
37     public UnnecessaryCodeCleanUp() {
38         super();
39     }
40     
41     /**
42      * {@inheritDoc}
43      */

44     public boolean requireAST(ICompilationUnit unit) throws CoreException {
45         return isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS);
46     }
47     
48     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
49         if (compilationUnit == null)
50             return null;
51         
52         return UnusedCodeFix.createCleanUp(compilationUnit,
53                 false,
54                 false,
55                 false,
56                 false,
57                 false,
58                 false,
59                 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS));
60     }
61     
62
63     /**
64      * {@inheritDoc}
65      */

66     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
67         if (compilationUnit == null)
68             return null;
69         
70         return UnusedCodeFix.createCleanUp(compilationUnit, problems,
71                 false,
72                 false,
73                 false,
74                 false,
75                 false,
76                 false,
77                 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS));
78     }
79
80     public Map JavaDoc getRequiredOptions() {
81         Map JavaDoc options= new Hashtable JavaDoc();
82
83         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS))
84             options.put(JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK, JavaCore.WARNING);
85
86         return options;
87     }
88     
89     /**
90      * {@inheritDoc}
91      */

92     public String JavaDoc[] getDescriptions() {
93         List JavaDoc result= new ArrayList JavaDoc();
94         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS))
95             result.add(MultiFixMessages.UnusedCodeCleanUp_RemoveUnusedCasts_description);
96         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
97     }
98     
99     /**
100      * {@inheritDoc}
101      */

102     public String JavaDoc getPreview() {
103         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
104         
105         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) {
106             buf.append("Boolean b= Boolean.TRUE;\n"); //$NON-NLS-1$
107
} else {
108             buf.append("Boolean b= (Boolean) Boolean.TRUE;\n"); //$NON-NLS-1$
109
}
110         
111         return buf.toString();
112     }
113
114     /**
115      * {@inheritDoc}
116      */

117     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
118         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) {
119             IFix fix= UnusedCodeFix.createRemoveUnusedCastFix(compilationUnit, problem);
120             if (fix != null)
121                 return true;
122         }
123         return false;
124     }
125
126     /**
127      * {@inheritDoc}
128      */

129     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
130         int result= 0;
131         IProblem[] problems= compilationUnit.getProblems();
132         if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS))
133             result+= getNumberOfProblems(problems, IProblem.UnnecessaryCast);
134         return result;
135     }
136 }
137
Popular Tags