KickJava   Java API By Example, From Geeks To Geeks.

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


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.Java50Fix;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 /**
32  * Create fixes which can transform pre Java50 code to Java50 code
33  * @see org.eclipse.jdt.internal.corext.fix.Java50Fix
34  *
35  */

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

49     public boolean requireAST(ICompilationUnit unit) throws CoreException {
50         boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
51         
52         return addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE) ||
53                addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED) ||
54                isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES);
55     }
56
57     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
58         if (compilationUnit == null)
59             return null;
60         
61         boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
62         return Java50Fix.createCleanUp(compilationUnit,
63                 addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE),
64                 addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED),
65                 isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES));
66     }
67     
68     /**
69      * {@inheritDoc}
70      */

71     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
72         if (compilationUnit == null)
73             return null;
74         
75         return Java50Fix.createCleanUp(compilationUnit, problems,
76                 isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE),
77                 isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED),
78                 isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES));
79     }
80
81     public Map JavaDoc getRequiredOptions() {
82         Map JavaDoc options= new Hashtable JavaDoc();
83         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE))
84             options.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, JavaCore.WARNING);
85         
86         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED))
87             options.put(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION, JavaCore.WARNING);
88         
89         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES))
90             options.put(JavaCore.COMPILER_PB_RAW_TYPE_REFERENCE, JavaCore.WARNING);
91                 
92         return options;
93     }
94     
95     /**
96      * {@inheritDoc}
97      */

98     public String JavaDoc[] getDescriptions() {
99         List JavaDoc result= new ArrayList JavaDoc();
100         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE))
101             result.add(MultiFixMessages.Java50MultiFix_AddMissingOverride_description);
102         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED))
103             result.add(MultiFixMessages.Java50MultiFix_AddMissingDeprecated_description);
104         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES))
105             result.add(MultiFixMessages.Java50CleanUp_AddTypeParameters_description);
106         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
107     }
108     
109     /**
110      * {@inheritDoc}
111      */

112     public String JavaDoc getPreview() {
113         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
114         
115         buf.append("class E {\n"); //$NON-NLS-1$
116
buf.append(" /**\n"); //$NON-NLS-1$
117
buf.append(" * @deprecated\n"); //$NON-NLS-1$
118
buf.append(" */\n"); //$NON-NLS-1$
119
if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) {
120             buf.append(" @Deprecated\n"); //$NON-NLS-1$
121
}
122         buf.append(" public void foo() {}\n"); //$NON-NLS-1$
123
buf.append("}\n"); //$NON-NLS-1$
124
buf.append("class ESub extends E {\n"); //$NON-NLS-1$
125
if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) {
126             buf.append(" @Override\n"); //$NON-NLS-1$
127
}
128         buf.append(" public void foo() {}\n"); //$NON-NLS-1$
129
buf.append("}\n"); //$NON-NLS-1$
130

131         return buf.toString();
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
138         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) {
139             Java50Fix fix= Java50Fix.createAddOverrideAnnotationFix(compilationUnit, problem);
140             if (fix != null)
141                 return true;
142         }
143         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) {
144             Java50Fix fix= Java50Fix.createAddDeprectatedAnnotation(compilationUnit, problem);
145             if (fix != null)
146                 return true;
147         }
148         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) {
149             Java50Fix fix= Java50Fix.createRawTypeReferenceFix(compilationUnit, problem);
150             if (fix != null)
151                 return true;
152         }
153         return false;
154     }
155
156     /**
157      * {@inheritDoc}
158      */

159     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
160         int result= 0;
161         IProblem[] problems= compilationUnit.getProblems();
162         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) {
163             result+= getNumberOfProblems(problems, IProblem.MissingOverrideAnnotation);
164         }
165         if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) {
166             for (int i=0;i<problems.length;i++) {
167                 int id= problems[i].getID();
168                 if (id == IProblem.FieldMissingDeprecatedAnnotation || id == IProblem.MethodMissingDeprecatedAnnotation || id == IProblem.TypeMissingDeprecatedAnnotation)
169                     result++;
170             }
171         }
172         if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) {
173             for (int i=0;i<problems.length;i++) {
174                 int id= problems[i].getID();
175                 if (id == IProblem.UnsafeTypeConversion || id == IProblem.RawTypeReference || id == IProblem.UnsafeRawMethodInvocation)
176                     result++;
177             }
178         }
179         return result;
180     }
181     
182 }
183
Popular Tags