001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.fix;
011:
012: import java.util.ArrayList;
013: import java.util.Hashtable;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.core.runtime.CoreException;
018:
019: import org.eclipse.jdt.core.ICompilationUnit;
020: import org.eclipse.jdt.core.JavaCore;
021: import org.eclipse.jdt.core.compiler.IProblem;
022: import org.eclipse.jdt.core.dom.CompilationUnit;
023:
024: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
025: import org.eclipse.jdt.internal.corext.fix.IFix;
026: import org.eclipse.jdt.internal.corext.fix.Java50Fix;
027:
028: import org.eclipse.jdt.ui.text.java.IProblemLocation;
029:
030: /**
031: * Create fixes which can transform pre Java50 code to Java50 code
032: * @see org.eclipse.jdt.internal.corext.fix.Java50Fix
033: *
034: */
035: public class Java50CleanUp extends AbstractMultiFix {
036:
037: public Java50CleanUp(Map options) {
038: super (options);
039: }
040:
041: public Java50CleanUp() {
042: super ();
043: }
044:
045: /**
046: * {@inheritDoc}
047: */
048: public CleanUpRequirements getRequirements() {
049: return new CleanUpRequirements(requireAST(), false,
050: getRequiredOptions());
051: }
052:
053: private boolean requireAST() {
054: boolean addAnotations = isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
055:
056: return addAnotations
057: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)
058: || addAnotations
059: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)
060: || isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES);
061: }
062:
063: /**
064: * {@inheritDoc}
065: */
066: protected IFix createFix(CompilationUnit compilationUnit)
067: throws CoreException {
068: boolean addAnotations = isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS);
069: return Java50Fix
070: .createCleanUp(
071: compilationUnit,
072: addAnotations
073: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE),
074: addAnotations
075: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED),
076: isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES));
077: }
078:
079: /**
080: * {@inheritDoc}
081: */
082: protected IFix createFix(CompilationUnit compilationUnit,
083: IProblemLocation[] problems) throws CoreException {
084: if (compilationUnit == null)
085: return null;
086:
087: return Java50Fix
088: .createCleanUp(
089: compilationUnit,
090: problems,
091: isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
092: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE),
093: isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
094: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED),
095: isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES));
096: }
097:
098: private Map getRequiredOptions() {
099: Map result = new Hashtable();
100: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
101: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE))
102: result.put(
103: JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION,
104: JavaCore.WARNING);
105:
106: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
107: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED))
108: result.put(
109: JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION,
110: JavaCore.WARNING);
111:
112: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES))
113: result.put(JavaCore.COMPILER_PB_RAW_TYPE_REFERENCE,
114: JavaCore.WARNING);
115:
116: return result;
117: }
118:
119: /**
120: * {@inheritDoc}
121: */
122: public String[] getDescriptions() {
123: List result = new ArrayList();
124: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
125: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE))
126: result
127: .add(MultiFixMessages.Java50MultiFix_AddMissingOverride_description);
128: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
129: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED))
130: result
131: .add(MultiFixMessages.Java50MultiFix_AddMissingDeprecated_description);
132: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES))
133: result
134: .add(MultiFixMessages.Java50CleanUp_AddTypeParameters_description);
135: return (String[]) result.toArray(new String[result.size()]);
136: }
137:
138: /**
139: * {@inheritDoc}
140: */
141: public String getPreview() {
142: StringBuffer buf = new StringBuffer();
143:
144: buf.append("class E {\n"); //$NON-NLS-1$
145: buf.append(" /**\n"); //$NON-NLS-1$
146: buf.append(" * @deprecated\n"); //$NON-NLS-1$
147: buf.append(" */\n"); //$NON-NLS-1$
148: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
149: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) {
150: buf.append(" @Deprecated\n"); //$NON-NLS-1$
151: }
152: buf.append(" public void foo() {}\n"); //$NON-NLS-1$
153: buf.append("}\n"); //$NON-NLS-1$
154: buf.append("class ESub extends E {\n"); //$NON-NLS-1$
155: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
156: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) {
157: buf.append(" @Override\n"); //$NON-NLS-1$
158: }
159: buf.append(" public void foo() {}\n"); //$NON-NLS-1$
160: buf.append("}\n"); //$NON-NLS-1$
161:
162: return buf.toString();
163: }
164:
165: /**
166: * {@inheritDoc}
167: */
168: public boolean canFix(ICompilationUnit compilationUnit,
169: IProblemLocation problem) {
170: if (problem.getProblemId() != IProblem.MissingOverrideAnnotation)
171: return isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
172: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE);
173:
174: if (Java50Fix.isMissingDeprecationProblem(problem))
175: return isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
176: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED);
177:
178: if (Java50Fix.isRawTypeReference(problem))
179: return isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES);
180:
181: return false;
182: }
183:
184: /**
185: * {@inheritDoc}
186: */
187: public int computeNumberOfFixes(CompilationUnit compilationUnit) {
188: int result = 0;
189: IProblem[] problems = compilationUnit.getProblems();
190: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
191: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) {
192: result += getNumberOfProblems(problems,
193: IProblem.MissingOverrideAnnotation);
194: }
195: if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS)
196: && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) {
197: for (int i = 0; i < problems.length; i++) {
198: int id = problems[i].getID();
199: if (id == IProblem.FieldMissingDeprecatedAnnotation
200: || id == IProblem.MethodMissingDeprecatedAnnotation
201: || id == IProblem.TypeMissingDeprecatedAnnotation)
202: result++;
203: }
204: }
205: if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) {
206: for (int i = 0; i < problems.length; i++) {
207: int id = problems[i].getID();
208: if (id == IProblem.UnsafeTypeConversion
209: || id == IProblem.RawTypeReference
210: || id == IProblem.UnsafeRawMethodInvocation)
211: result++;
212: }
213: }
214: return result;
215: }
216:
217: }
|