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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.fix;
13 import java.util.ArrayList;
14 import java.util.Hashtable;
15 import java.util.List;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaProject;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.compiler.IProblem;
27 import org.eclipse.jdt.core.dom.CompilationUnit;
29 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
30 import org.eclipse.jdt.internal.corext.fix.PotentialProgrammingProblemsFix;
32 import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
33 import org.eclipse.jdt.ui.cleanup.ICleanUpFix;
34 import org.eclipse.jdt.ui.text.java.IProblemLocation;
36 public class PotentialProgrammingProblemsCleanUp extends AbstractMultiFix {
38 public PotentialProgrammingProblemsCleanUp(Map<String, String> options) {
42 public PotentialProgrammingProblemsCleanUp() {
50 public CleanUpRequirements getRequirements() {
51 boolean requireAST= requireAST();
52 Map<String, String> requiredOptions= requireAST ? getRequiredOptions() : null;
53 return new CleanUpRequirements(requireAST, false, false, requiredOptions);
56 private boolean requireAST() {
57 boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
61 return isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
62 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT);
69 protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException {
71 boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
75 return PotentialProgrammingProblemsFix.createCleanUp(compilationUnit,
76 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
77 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
84 protected ICleanUpFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
85 if (compilationUnit == null)
88 return PotentialProgrammingProblemsFix.createCleanUp(compilationUnit, problems,
89 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
90 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)));
93 private Map<String, String> getRequiredOptions() {
94 Map<String, String> result= new Hashtable<String, String>();
95 if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
96 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
97 result.put(JavaCore.COMPILER_PB_MISSING_SERIAL_VERSION, JavaCore.WARNING);
105 public String[] getStepDescriptions() {
106 List<String> result= new ArrayList<String>();
108 if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)))
109 result.add(MultiFixMessages.SerialVersionCleanUp_Generated_description);
110 if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
111 result.add(MultiFixMessages.CodeStyleCleanUp_addDefaultSerialVersionId_description);
113 return result.toArray(new String[result.size()]);
120 public String getPreview() {
121 StringBuffer buf= new StringBuffer();
123 buf.append("class E implements java.io.Serializable {\n"); //$NON-NLS-1$
124 if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))) {
125 buf.append(" private static final long serialVersionUID = -391484377137870342L;\n"); //$NON-NLS-1$
126 } else if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT))) {
127 buf.append(" private static final long serialVersionUID = 1L;\n"); //$NON-NLS-1$
129 buf.append("}\n"); //$NON-NLS-1$
131 return buf.toString();
137 public boolean canFix(ICompilationUnit compilationUnit, IProblemLocation problem) {
138 if (problem.getProblemId() == IProblem.MissingSerialVersion)
139 return (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
140 || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
149 public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException {
150 RefactoringStatus superStatus= super.checkPreConditions(project, compilationUnits, monitor);
151 if (superStatus.hasFatalError())
154 return PotentialProgrammingProblemsFix.checkPreConditions(project, compilationUnits, monitor,
155 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED),
156 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT),
164 public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException {
165 return PotentialProgrammingProblemsFix.checkPostConditions(monitor);
172 public int computeNumberOfFixes(CompilationUnit compilationUnit) {
173 if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
174 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
175 return getNumberOfProblems(compilationUnit.getProblems(), IProblem.MissingSerialVersion);