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 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.JavaCore; 22 import org.eclipse.jdt.core.compiler.IProblem; 23 import org.eclipse.jdt.core.dom.CompilationUnit; 24 25 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; 26 import org.eclipse.jdt.internal.corext.fix.IFix; 27 import org.eclipse.jdt.internal.corext.fix.Java50Fix; 28 29 import org.eclipse.jdt.ui.text.java.IProblemLocation; 30 31 36 public class Java50CleanUp extends AbstractCleanUp { 37 38 public Java50CleanUp(Map options) { 39 super(options); 40 } 41 42 public Java50CleanUp() { 43 super(); 44 } 45 46 49 public boolean requireAST(ICompilationUnit unit) throws CoreException { 50 boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); 51 52 return addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE) || 53 addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED) || 54 isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES); 55 } 56 57 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 58 if (compilationUnit == null) 59 return null; 60 61 boolean addAnotations= isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS); 62 return Java50Fix.createCleanUp(compilationUnit, 63 addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE), 64 addAnotations && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED), 65 isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)); 66 } 67 68 71 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 72 if (compilationUnit == null) 73 return null; 74 75 return Java50Fix.createCleanUp(compilationUnit, problems, 76 isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE), 77 isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED), 78 isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)); 79 } 80 81 public Map getRequiredOptions() { 82 Map options= new Hashtable (); 83 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) 84 options.put(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION, JavaCore.WARNING); 85 86 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) 87 options.put(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION, JavaCore.WARNING); 88 89 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) 90 options.put(JavaCore.COMPILER_PB_RAW_TYPE_REFERENCE, JavaCore.WARNING); 91 92 return options; 93 } 94 95 98 public String [] getDescriptions() { 99 List result= new ArrayList (); 100 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) 101 result.add(MultiFixMessages.Java50MultiFix_AddMissingOverride_description); 102 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) 103 result.add(MultiFixMessages.Java50MultiFix_AddMissingDeprecated_description); 104 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) 105 result.add(MultiFixMessages.Java50CleanUp_AddTypeParameters_description); 106 return (String [])result.toArray(new String [result.size()]); 107 } 108 109 112 public String getPreview() { 113 StringBuffer buf= new StringBuffer (); 114 115 buf.append("class E {\n"); buf.append(" /**\n"); buf.append(" * @deprecated\n"); buf.append(" */\n"); if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) { 120 buf.append(" @Deprecated\n"); } 122 buf.append(" public void foo() {}\n"); buf.append("}\n"); buf.append("class ESub extends E {\n"); if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { 126 buf.append(" @Override\n"); } 128 buf.append(" public void foo() {}\n"); buf.append("}\n"); 131 return buf.toString(); 132 } 133 134 137 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 138 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { 139 Java50Fix fix= Java50Fix.createAddOverrideAnnotationFix(compilationUnit, problem); 140 if (fix != null) 141 return true; 142 } 143 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) { 144 Java50Fix fix= Java50Fix.createAddDeprectatedAnnotation(compilationUnit, problem); 145 if (fix != null) 146 return true; 147 } 148 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) { 149 Java50Fix fix= Java50Fix.createRawTypeReferenceFix(compilationUnit, problem); 150 if (fix != null) 151 return true; 152 } 153 return false; 154 } 155 156 159 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 160 int result= 0; 161 IProblem[] problems= compilationUnit.getProblems(); 162 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_OVERRIDE)) { 163 result+= getNumberOfProblems(problems, IProblem.MissingOverrideAnnotation); 164 } 165 if (isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS) && isEnabled(CleanUpConstants.ADD_MISSING_ANNOTATIONS_DEPRECATED)) { 166 for (int i=0;i<problems.length;i++) { 167 int id= problems[i].getID(); 168 if (id == IProblem.FieldMissingDeprecatedAnnotation || id == IProblem.MethodMissingDeprecatedAnnotation || id == IProblem.TypeMissingDeprecatedAnnotation) 169 result++; 170 } 171 } 172 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATION_USE_TYPE_ARGUMENTS_FOR_RAW_TYPE_REFERENCES)) { 173 for (int i=0;i<problems.length;i++) { 174 int id= problems[i].getID(); 175 if (id == IProblem.UnsafeTypeConversion || id == IProblem.RawTypeReference || id == IProblem.UnsafeRawMethodInvocation) 176 result++; 177 } 178 } 179 return result; 180 } 181 182 } 183 | Popular Tags |