1 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.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 options) { 38 super(options); 39 } 40 41 public PotentialProgrammingProblemsCleanUp() { 42 super(); 43 } 44 45 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 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 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 88 public Map getRequiredOptions() { 89 Map options= new Hashtable (); 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 99 public String [] getDescriptions() { 100 List result= new ArrayList (); 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 [])result.toArray(new String [result.size()]); 108 } 109 110 113 public String getPreview() { 114 StringBuffer buf= new StringBuffer (); 115 116 buf.append("class E implements java.io.Serializable{\n"); 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"); } 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"); } 122 buf.append("}\n"); 124 return buf.toString(); 125 } 126 127 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 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 154 public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException { 155 return PotentialProgrammingProblemsFix.checkPostConditions(monitor); 156 } 157 158 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 |