]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/fix/PotentialProgrammingProblemsCleanUp.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / fix / PotentialProgrammingProblemsCleanUp.java
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 import org.eclipse.core.runtime.IProgressMonitor;
20
21 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
22
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;
28
29 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
30 import org.eclipse.jdt.internal.corext.fix.PotentialProgrammingProblemsFix;
31
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;
35
36 public class PotentialProgrammingProblemsCleanUp extends AbstractMultiFix {
37
38         public PotentialProgrammingProblemsCleanUp(Map<String, String> options) {
39                 super(options);
40         }
41
42         public PotentialProgrammingProblemsCleanUp() {
43                 super();
44         }
45
46         /**A
47          * {@inheritDoc}
48          */
49         @Override
50         public CleanUpRequirements getRequirements() {
51                 boolean requireAST= requireAST();
52                 Map<String, String> requiredOptions= requireAST ? getRequiredOptions() : null;
53                 return new CleanUpRequirements(requireAST, false, false, requiredOptions);
54         }
55
56         private boolean requireAST() {
57                 boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
58                 if (!addSUID)
59                         return false;
60
61                 return isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
62                        isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT);
63         }
64
65         /**
66          * {@inheritDoc}
67          */
68         @Override
69         protected ICleanUpFix createFix(CompilationUnit compilationUnit) throws CoreException {
70
71                 boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
72                 if (!addSUID)
73                         return null;
74
75                 return PotentialProgrammingProblemsFix.createCleanUp(compilationUnit,
76                                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
77                                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
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                 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)));
91         }
92
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);
98                 return result;
99         }
100
101         /**
102          * {@inheritDoc}
103          */
104         @Override
105         public String[] getStepDescriptions() {
106                 List<String> result= new ArrayList<String>();
107
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);
112
113                 return result.toArray(new String[result.size()]);
114         }
115
116         /**
117          * {@inheritDoc}
118          */
119         @Override
120         public String getPreview() {
121                 StringBuffer buf= new StringBuffer();
122
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$
128                 }
129                 buf.append("}\n"); //$NON-NLS-1$
130
131                 return buf.toString();
132         }
133
134         /**
135          * {@inheritDoc}
136          */
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));
141
142                 return false;
143         }
144
145         /**
146          * {@inheritDoc}
147          */
148         @Override
149         public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException {
150                 RefactoringStatus superStatus= super.checkPreConditions(project, compilationUnits, monitor);
151                 if (superStatus.hasFatalError())
152                         return superStatus;
153
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),
157                                 false);
158         }
159
160         /**
161          * {@inheritDoc}
162          */
163         @Override
164         public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException {
165                 return PotentialProgrammingProblemsFix.checkPostConditions(monitor);
166         }
167
168         /**
169          * {@inheritDoc}
170          */
171         @Override
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);
176
177                 return 0;
178         }
179 }