1 11 package org.eclipse.jdt.internal.ui.fix; 12 13 14 import java.util.ArrayList ; 15 import java.util.Hashtable ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.CoreException; 20 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.JavaCore; 23 import org.eclipse.jdt.core.compiler.IProblem; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 26 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; 27 import org.eclipse.jdt.internal.corext.fix.IFix; 28 import org.eclipse.jdt.internal.corext.fix.UnusedCodeFix; 29 30 import org.eclipse.jdt.ui.text.java.IProblemLocation; 31 32 37 public class UnusedCodeCleanUp extends AbstractCleanUp { 38 39 public UnusedCodeCleanUp(Map options) { 40 super(options); 41 } 42 43 public UnusedCodeCleanUp() { 44 super(); 45 } 46 47 50 public boolean requireAST(ICompilationUnit unit) throws CoreException { 51 boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS); 52 53 return removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) || 54 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) || 55 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) || 56 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) || 57 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES) || 58 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS); 59 } 60 61 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 62 if (compilationUnit == null) 63 return null; 64 65 boolean removeUnuseMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS); 66 67 return UnusedCodeFix.createCleanUp(compilationUnit, 68 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS), 69 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS), 70 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS), 71 removeUnuseMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES), 72 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES), 73 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS), 74 false); 75 } 76 77 78 81 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 82 if (compilationUnit == null) 83 return null; 84 85 boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS); 86 return UnusedCodeFix.createCleanUp(compilationUnit, problems, 87 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS), 88 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS), 89 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS), 90 removeMembers && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES), 91 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES), 92 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS), 93 false); 94 } 95 96 public Map getRequiredOptions() { 97 Map options= new Hashtable (); 98 99 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) 100 options.put(JavaCore.COMPILER_PB_UNUSED_IMPORT, JavaCore.WARNING); 101 102 boolean removeMembers= isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS); 103 if (removeMembers && ( 104 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) || 105 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) || 106 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) || 107 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES))) 108 options.put(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER, JavaCore.WARNING); 109 110 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) 111 options.put(JavaCore.COMPILER_PB_UNUSED_LOCAL, JavaCore.WARNING); 112 113 return options; 114 } 115 116 119 public String [] getDescriptions() { 120 List result= new ArrayList (); 121 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) 122 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedImport_description); 123 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS)) 124 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedMethod_description); 125 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS)) 126 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedConstructor_description); 127 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)) 128 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedType_description); 129 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS)) 130 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedField_description); 131 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) 132 result.add(MultiFixMessages.UnusedCodeMultiFix_RemoveUnusedVariable_description); 133 return (String [])result.toArray(new String [result.size()]); 134 } 135 136 139 public String getPreview() { 140 StringBuffer buf= new StringBuffer (); 141 142 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) { 143 } else { 144 buf.append("import pack.Bar;\n"); } 146 buf.append("class Example {\n"); if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)) { 148 } else { 149 buf.append(" private class Sub {}\n"); } 151 buf.append(" public Example(boolean b) {}\n"); if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS)) { 153 } else { 154 buf.append(" private Example() {}\n"); } 156 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS)) { 157 } else { 158 buf.append(" private int fField;\n"); } 160 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS)) { 161 } else { 162 buf.append(" private void foo() {}\n"); } 164 buf.append(" public void bar() {\n"); if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) { 166 } else { 167 buf.append(" int i= 10;\n"); } 169 buf.append(" }\n"); buf.append("}\n"); 172 return buf.toString(); 173 } 174 175 178 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 179 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS)) { 180 UnusedCodeFix fix= UnusedCodeFix.createRemoveUnusedImportFix(compilationUnit, problem); 181 if (fix != null) 182 return true; 183 } 184 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS) || 185 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS) || 186 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES) || 187 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS) || 188 isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) 189 { 190 UnusedCodeFix fix= UnusedCodeFix.createUnusedMemberFix(compilationUnit, problem, false); 191 if (fix != null) 192 return true; 193 } 194 return false; 195 } 196 197 200 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 201 int result= 0; 202 IProblem[] problems= compilationUnit.getProblems(); 203 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS) && !isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) { 204 for (int i=0;i<problems.length;i++) { 205 int id= problems[i].getID(); 206 if (id == IProblem.UnusedImport || id == IProblem.DuplicateImport || id == IProblem.ConflictingImport || 207 id == IProblem.CannotImportPackage || id == IProblem.ImportNotFound) 208 result++; 209 } 210 } 211 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS)) 212 result+= getNumberOfProblems(problems, IProblem.UnusedPrivateMethod); 213 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS)) 214 result+= getNumberOfProblems(problems, IProblem.UnusedPrivateConstructor); 215 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES)) 216 result+= getNumberOfProblems(problems, IProblem.UnusedPrivateType); 217 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS) && isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS)) 218 result+= getNumberOfProblems(problems, IProblem.UnusedPrivateField); 219 if (isEnabled(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES)) 220 result+= getNumberOfProblems(problems, IProblem.LocalVariableIsNeverUsed); 221 return result; 222 } 223 } 224 | Popular Tags |