]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.Hashtable; | |
15 | import java.util.List; | |
16 | import java.util.Map; | |
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.Java50Fix; | |
27 | ||
28 | import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; | |
29 | import org.eclipse.jdt.ui.cleanup.ICleanUpFix; | |
30 | import org.eclipse.jdt.ui.text.java.IProblemLocation; | |
31 | ||
32 | ||
33 | /** | |
34 | * Create fixes which can transform pre Java50 code to Java50 code | |
35 | * @see org.eclipse.jdt.internal.corext.fix.Java50Fix | |
36 | * | |
37 | */ | |
38 | public class Java50CleanUp extends AbstractMultiFix { | |
39 | ||
40 | public Java50CleanUp(Map<String, String> options) { | |
41 | super(options); | |
42 | } | |
43 | ||
44 | public Java50CleanUp() { | |
45 | super(); | |
46 | } | |
47 | ||
48 | /** | |
49 | * {@inheritDoc} | |
50 | */ | |
51 | @Override | |
52 | public CleanUpRequirements getRequirements() { | |
53 | boolean requireAST= requireAST(); | |
54 | Map<String, String> requiredOptions= requireAST ? getRequiredOptions() : null; | |
55 | return new CleanUpRequirements(requireAST, false, false, requiredOptions); | |
56 | } | |
57 | ||
58 | private boolean requireAST() { | |
59 | boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); | |
60 | ||
61 | return addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE) || | |
62 | addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED) || | |
63 | isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES); | |
64 | } | |
65 | ||
66 | /** | |
67 | * {@inheritDoc} | |
68 | */ | |
69 | @Override | |
70 | protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException { | |
71 | boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); | |
72 | boolean addOverride= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE); | |
73 | return Java50Fix.createCleanUp(compilationUnit, | |
74 | addAnotations && addOverride, | |
75 | addAnotations && addOverride && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION), | |
76 | addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED), | |
77 | isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)); | |
78 | } | |
79 | ||
80 | /** | |
81 | * {@inheritDoc} | |
82 | */ | |
83 | @Override | |
84 | protected ICleanUpFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { | |
85 | if (compilationUnit == null) | |
86 | return null; | |
87 | ||
88 | boolean addAnnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); | |
89 | boolean addOverride= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE); | |
90 | return Java50Fix.createCleanUp(compilationUnit, problems, | |
91 | addAnnotations && addOverride, | |
92 | addAnnotations && addOverride && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION), | |
93 | addAnnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED), | |
94 | isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)); | |
95 | } | |
96 | ||
97 | private Map<String, String> getRequiredOptions() { | |
98 | Map<String, String> result= new Hashtable<String, String>(); | |
99 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { | |
100 | result.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, JavaCore.WARNING); | |
101 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION)) { | |
102 | result.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION_FOR_INTERFACE_METHOD_IMPLEMENTATION, JavaCore.ENABLED); | |
103 | } | |
104 | } | |
105 | ||
106 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) | |
107 | result.put(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION, JavaCore.WARNING); | |
108 | ||
109 | if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) | |
110 | result.put(JavaCore.COMPILER_PB_RAW_TYPE_REFERENCE, JavaCore.WARNING); | |
111 | ||
112 | return result; | |
113 | } | |
114 | ||
115 | /** | |
116 | * {@inheritDoc} | |
117 | */ | |
118 | @Override | |
119 | public String[] getStepDescriptions() { | |
120 | List<String> result= new ArrayList<String>(); | |
121 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { | |
122 | result.add(MultiFixMessages.Java50MultiFix_AddMissingOverride_description); | |
123 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION)) { | |
124 | result.add(MultiFixMessages.Java50MultiFix_AddMissingOverride_description2); | |
125 | } | |
126 | } | |
127 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) | |
128 | result.add(MultiFixMessages.Java50MultiFix_AddMissingDeprecated_description); | |
129 | if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) | |
130 | result.add(MultiFixMessages.Java50CleanUp_AddTypeParameters_description); | |
131 | return result.toArray(new String[result.size()]); | |
132 | } | |
133 | ||
134 | /** | |
135 | * {@inheritDoc} | |
136 | */ | |
137 | @Override | |
138 | public String getPreview() { | |
139 | StringBuffer buf= new StringBuffer(); | |
140 | ||
141 | buf.append("class E {\n"); //$NON-NLS-1$ | |
142 | buf.append(" /**\n"); //$NON-NLS-1$ | |
143 | buf.append(" * @deprecated\n"); //$NON-NLS-1$ | |
144 | buf.append(" */\n"); //$NON-NLS-1$ | |
145 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) { | |
146 | buf.append(" @Deprecated\n"); //$NON-NLS-1$ | |
147 | } | |
148 | buf.append(" public void foo() {}\n"); //$NON-NLS-1$ | |
149 | buf.append("}\n"); //$NON-NLS-1$ | |
150 | buf.append("class ESub extends E implements Runnable {\n"); //$NON-NLS-1$ | |
151 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { | |
152 | buf.append(" @Override\n"); //$NON-NLS-1$ | |
153 | } | |
154 | buf.append(" public void foo() {}\n"); //$NON-NLS-1$ | |
155 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) | |
156 | && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE) | |
157 | && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION)) { | |
158 | buf.append(" @Override\n"); //$NON-NLS-1$ | |
159 | } | |
160 | buf.append(" public void run() {}\n"); //$NON-NLS-1$ | |
161 | buf.append("}\n"); //$NON-NLS-1$ | |
162 | ||
163 | return buf.toString(); | |
164 | } | |
165 | ||
166 | /** | |
167 | * {@inheritDoc} | |
168 | */ | |
169 | public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) { | |
170 | int id= problem.getProblemId(); | |
171 | ||
172 | if (Java50Fix.isMissingOverrideAnnotationProblem(id)) { | |
173 | if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { | |
174 | return ! Java50Fix.isMissingOverrideAnnotationInterfaceProblem(id) || isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION); | |
175 | } | |
176 | ||
177 | } else if (Java50Fix.isMissingDeprecationProblem(id)) { | |
178 | return isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED); | |
179 | ||
180 | } else if (Java50Fix.isRawTypeReferenceProblem(id)) { | |
181 | return isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES); | |
182 | } | |
183 | ||
184 | return false; | |
185 | } | |
186 | ||
187 | /** | |
188 | * {@inheritDoc} | |
189 | */ | |
190 | @Override | |
191 | public int computeNumberOfFixes(CompilationUnit compilationUnit) { | |
192 | int result= 0; | |
193 | ||
194 | boolean addAnnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); | |
195 | boolean addMissingOverride= addAnnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE); | |
196 | boolean addMissingOverrideInterfaceMethods= addMissingOverride && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE_FOR_INTERFACE_METHOD_IMPLEMENTATION); | |
197 | boolean addMissingDeprecated= addAnnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED); | |
198 | boolean useTypeArgs= isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES); | |
199 | ||
200 | IProblem[] problems= compilationUnit.getProblems(); | |
201 | for (int i= 0; i < problems.length; i++) { | |
202 | int id= problems[i].getID(); | |
203 | if (addMissingOverride && Java50Fix.isMissingOverrideAnnotationProblem(id)) | |
204 | if (! Java50Fix.isMissingOverrideAnnotationInterfaceProblem(id) || addMissingOverrideInterfaceMethods) | |
205 | result++; | |
206 | if (addMissingDeprecated && Java50Fix.isMissingDeprecationProblem(id)) | |
207 | result++; | |
208 | if (useTypeArgs && Java50Fix.isRawTypeReferenceProblem(id)) | |
209 | result++; | |
210 | } | |
211 | return result; | |
212 | } | |
213 | ||
214 | } |