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.UnusedCodeFix; 28 29 import org.eclipse.jdt.ui.text.java.IProblemLocation; 30 31 public class UnnecessaryCodeCleanUp extends AbstractCleanUp { 32 33 public UnnecessaryCodeCleanUp(Map options) { 34 super(options); 35 } 36 37 public UnnecessaryCodeCleanUp() { 38 super(); 39 } 40 41 44 public boolean requireAST(ICompilationUnit unit) throws CoreException { 45 return isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS); 46 } 47 48 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 49 if (compilationUnit == null) 50 return null; 51 52 return UnusedCodeFix.createCleanUp(compilationUnit, 53 false, 54 false, 55 false, 56 false, 57 false, 58 false, 59 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)); 60 } 61 62 63 66 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 67 if (compilationUnit == null) 68 return null; 69 70 return UnusedCodeFix.createCleanUp(compilationUnit, problems, 71 false, 72 false, 73 false, 74 false, 75 false, 76 false, 77 isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)); 78 } 79 80 public Map getRequiredOptions() { 81 Map options= new Hashtable (); 82 83 if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) 84 options.put(JavaCore.COMPILER_PB_UNNECESSARY_TYPE_CHECK, JavaCore.WARNING); 85 86 return options; 87 } 88 89 92 public String [] getDescriptions() { 93 List result= new ArrayList (); 94 if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) 95 result.add(MultiFixMessages.UnusedCodeCleanUp_RemoveUnusedCasts_description); 96 return (String [])result.toArray(new String [result.size()]); 97 } 98 99 102 public String getPreview() { 103 StringBuffer buf= new StringBuffer (); 104 105 if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) { 106 buf.append("Boolean b= Boolean.TRUE;\n"); } else { 108 buf.append("Boolean b= (Boolean) Boolean.TRUE;\n"); } 110 111 return buf.toString(); 112 } 113 114 117 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 118 if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) { 119 IFix fix= UnusedCodeFix.createRemoveUnusedCastFix(compilationUnit, problem); 120 if (fix != null) 121 return true; 122 } 123 return false; 124 } 125 126 129 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 130 int result= 0; 131 IProblem[] problems= compilationUnit.getProblems(); 132 if (isEnabled(CleanUpConstants.REMOVE_UNNECESSARY_CASTS)) 133 result+= getNumberOfProblems(problems, IProblem.UnnecessaryCast); 134 return result; 135 } 136 } 137 | Popular Tags |