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: import org.eclipse.core.runtime.IProgressMonitor;
019:
020: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
021:
022: import org.eclipse.jdt.core.ICompilationUnit;
023: import org.eclipse.jdt.core.IJavaProject;
024: import org.eclipse.jdt.core.JavaCore;
025: import org.eclipse.jdt.core.compiler.IProblem;
026: import org.eclipse.jdt.core.dom.CompilationUnit;
027:
028: import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
029: import org.eclipse.jdt.internal.corext.fix.IFix;
030: import org.eclipse.jdt.internal.corext.fix.PotentialProgrammingProblemsFix;
031:
032: import org.eclipse.jdt.ui.text.java.IProblemLocation;
033:
034: public class PotentialProgrammingProblemsCleanUp extends
035: AbstractMultiFix {
036:
037: public PotentialProgrammingProblemsCleanUp(Map options) {
038: super (options);
039: }
040:
041: public PotentialProgrammingProblemsCleanUp() {
042: super ();
043: }
044:
045: /**A
046: * {@inheritDoc}
047: */
048: public CleanUpRequirements getRequirements() {
049: return new CleanUpRequirements(requireAST(), false,
050: getRequiredOptions());
051: }
052:
053: private boolean requireAST() {
054: boolean addSUID = isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
055: if (!addSUID)
056: return false;
057:
058: return isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)
059: || isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT);
060: }
061:
062: /**
063: * {@inheritDoc}
064: */
065: protected IFix createFix(CompilationUnit compilationUnit)
066: throws CoreException {
067:
068: boolean addSUID = isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
069: if (!addSUID)
070: return null;
071:
072: return PotentialProgrammingProblemsFix
073: .createCleanUp(
074: compilationUnit,
075: isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)
076: || isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
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 PotentialProgrammingProblemsFix
088: .createCleanUp(
089: compilationUnit,
090: problems,
091: (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
092: || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)));
093: }
094:
095: private Map getRequiredOptions() {
096: Map result = new Hashtable();
097: if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
098: || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
099: result.put(JavaCore.COMPILER_PB_MISSING_SERIAL_VERSION,
100: JavaCore.WARNING);
101: return result;
102: }
103:
104: /**
105: * {@inheritDoc}
106: */
107: public String[] getDescriptions() {
108: List result = new ArrayList();
109:
110: if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)))
111: result
112: .add(MultiFixMessages.SerialVersionCleanUp_Generated_description);
113: if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
114: result
115: .add(MultiFixMessages.CodeStyleCleanUp_addDefaultSerialVersionId_description);
116:
117: return (String[]) result.toArray(new String[result.size()]);
118: }
119:
120: /**
121: * {@inheritDoc}
122: */
123: public String getPreview() {
124: StringBuffer buf = new StringBuffer();
125:
126: buf.append("class E implements java.io.Serializable{\n"); //$NON-NLS-1$
127: if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))) {
128: buf
129: .append(" private static final long serialVersionUID = -391484377137870342L;\n"); //$NON-NLS-1$
130: } else if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT))) {
131: buf
132: .append(" private static final long serialVersionUID = 1L;\n"); //$NON-NLS-1$
133: }
134: buf.append("}\n"); //$NON-NLS-1$
135:
136: return buf.toString();
137: }
138:
139: /**
140: * {@inheritDoc}
141: */
142: public boolean canFix(ICompilationUnit compilationUnit,
143: IProblemLocation problem) {
144: if (problem.getProblemId() == IProblem.MissingSerialVersion)
145: return (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
146: || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
147:
148: return false;
149: }
150:
151: /**
152: * {@inheritDoc}
153: */
154: public RefactoringStatus checkPreConditions(IJavaProject project,
155: ICompilationUnit[] compilationUnits,
156: IProgressMonitor monitor) throws CoreException {
157: RefactoringStatus super Status = super .checkPreConditions(
158: project, compilationUnits, monitor);
159: if (super Status.hasFatalError())
160: return super Status;
161:
162: return PotentialProgrammingProblemsFix
163: .checkPreConditions(
164: project,
165: compilationUnits,
166: monitor,
167: isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID)
168: && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED),
169: isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID)
170: && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT),
171: false);
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: public RefactoringStatus checkPostConditions(
178: IProgressMonitor monitor) throws CoreException {
179: return PotentialProgrammingProblemsFix
180: .checkPostConditions(monitor);
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public int computeNumberOfFixes(CompilationUnit compilationUnit) {
187: if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
188: || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
189: return getNumberOfProblems(compilationUnit.getProblems(),
190: IProblem.MissingSerialVersion);
191:
192: return 0;
193: }
194: }
|