KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > PotentialProgrammingProblemsCleanUp


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
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.IFix;
31 import org.eclipse.jdt.internal.corext.fix.PotentialProgrammingProblemsFix;
32
33 import org.eclipse.jdt.ui.text.java.IProblemLocation;
34
35 public class PotentialProgrammingProblemsCleanUp extends AbstractCleanUp {
36     
37     public PotentialProgrammingProblemsCleanUp(Map JavaDoc options) {
38         super(options);
39     }
40     
41     public PotentialProgrammingProblemsCleanUp() {
42         super();
43     }
44     
45     /**
46      * {@inheritDoc}
47      */

48     public boolean requireAST(ICompilationUnit unit) throws CoreException {
49         boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
50         if (!addSUID)
51             return false;
52         
53         return isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
54                isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT);
55     }
56     
57     /**
58      * {@inheritDoc}
59      */

60     public IFix createFix(CompilationUnit compilationUnit) throws CoreException {
61         if (compilationUnit == null)
62             return null;
63         
64         boolean addSUID= isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID);
65         if (!addSUID)
66             return null;
67         
68         return PotentialProgrammingProblemsFix.createCleanUp(compilationUnit,
69                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED) ||
70                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT));
71     }
72
73     /**
74      * {@inheritDoc}
75      */

76     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
77         if (compilationUnit == null)
78             return null;
79         
80         return PotentialProgrammingProblemsFix.createCleanUp(compilationUnit, problems,
81                 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
82                 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)));
83     }
84
85     /**
86      * {@inheritDoc}
87      */

88     public Map JavaDoc getRequiredOptions() {
89         Map JavaDoc options= new Hashtable JavaDoc();
90         if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
91                 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
92             options.put(JavaCore.COMPILER_PB_MISSING_SERIAL_VERSION, JavaCore.WARNING);
93         return options;
94     }
95
96     /**
97      * {@inheritDoc}
98      */

99     public String JavaDoc[] getDescriptions() {
100         List JavaDoc result= new ArrayList JavaDoc();
101         
102         if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)))
103             result.add(MultiFixMessages.SerialVersionCleanUp_Generated_description);
104         if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
105             result.add(MultiFixMessages.CodeStyleCleanUp_addDefaultSerialVersionId_description);
106         
107         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
108     }
109     
110     /**
111      * {@inheritDoc}
112      */

113     public String JavaDoc getPreview() {
114         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
115         
116         buf.append("class E implements java.io.Serializable{\n"); //$NON-NLS-1$
117
if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))) {
118             buf.append(" private static final long serialVersionUID = -391484377137870342L;\n"); //$NON-NLS-1$
119
} else if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT))) {
120             buf.append(" private static final long serialVersionUID = 1L;\n"); //$NON-NLS-1$
121
}
122         buf.append("}\n"); //$NON-NLS-1$
123

124         return buf.toString();
125     }
126
127     /**
128      * {@inheritDoc}
129      */

130     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
131         if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED))
132                 || (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT))) {
133             IFix[] fix= PotentialProgrammingProblemsFix.createMissingSerialVersionFixes(compilationUnit, problem);
134             if (fix != null)
135                 return true;
136         }
137         return false;
138     }
139     
140     /**
141      * {@inheritDoc}
142      */

143     public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException {
144         super.checkPreConditions(project, compilationUnits, null);
145         return PotentialProgrammingProblemsFix.checkPreConditions(project, compilationUnits, monitor,
146                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED),
147                 isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT),
148                 false);
149     }
150
151     /**
152      * {@inheritDoc}
153      */

154     public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException {
155         return PotentialProgrammingProblemsFix.checkPostConditions(monitor);
156     }
157
158     /**
159      * {@inheritDoc}
160      */

161     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
162         if ((isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_GENERATED)) ||
163                 (isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID) && isEnabled(CleanUpConstants.ADD_MISSING_SERIAL_VERSION_ID_DEFAULT)))
164             return getNumberOfProblems(compilationUnit.getProblems(), IProblem.MissingSerialVersion);
165         
166         return 0;
167     }
168 }
169
Popular Tags