1 11 package org.eclipse.jdt.internal.ui.fix; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import org.eclipse.core.runtime.CoreException; 18 19 import org.eclipse.jdt.core.ICompilationUnit; 20 import org.eclipse.jdt.core.dom.ASTNode; 21 import org.eclipse.jdt.core.dom.CompilationUnit; 22 23 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; 24 import org.eclipse.jdt.internal.corext.fix.ExpressionsFix; 25 import org.eclipse.jdt.internal.corext.fix.IFix; 26 27 import org.eclipse.jdt.ui.text.java.IProblemLocation; 28 29 public class ExpressionsCleanUp extends AbstractCleanUp { 30 31 public ExpressionsCleanUp(Map options) { 32 super(options); 33 } 34 35 public ExpressionsCleanUp() { 36 super(); 37 } 38 39 42 public boolean requireAST(ICompilationUnit unit) throws CoreException { 43 boolean usePrentheses= isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES); 44 if (!usePrentheses) 45 return false; 46 47 return isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS) || 48 isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER); 49 } 50 51 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 52 if (compilationUnit == null) 53 return null; 54 55 boolean usePrentheses= isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES); 56 if (!usePrentheses) 57 return null; 58 59 return ExpressionsFix.createCleanUp(compilationUnit, 60 isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS), 61 isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)); 62 } 63 64 67 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 68 return createFix(compilationUnit); 69 } 70 71 public Map getRequiredOptions() { 72 return null; 73 } 74 75 78 public String [] getDescriptions() { 79 List result= new ArrayList (); 80 if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS)) 81 result.add(MultiFixMessages.ExpressionsCleanUp_addParanoiac_description); 82 83 if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)) 84 result.add(MultiFixMessages.ExpressionsCleanUp_removeUnnecessary_description); 85 86 return (String [])result.toArray(new String [result.size()]); 87 } 88 89 public String getPreview() { 90 StringBuffer buf= new StringBuffer (); 91 92 if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS)) { 93 buf.append("boolean b= (((i > 0) && (i < 10)) || (i == 50));\n"); } else if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)) { 95 buf.append("boolean b= i > 0 && i < 10 || i == 50;\n"); } else { 97 buf.append("boolean b= (i > 0 && i < 10 || i == 50);\n"); } 99 100 return buf.toString(); 101 } 102 103 106 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 107 if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS)) { 108 IFix fix= ExpressionsFix.createAddParanoidalParenthesisFix(compilationUnit, new ASTNode[] {problem.getCoveredNode(compilationUnit)}); 109 if (fix != null) 110 return true; 111 } 112 if (isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES) && isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER)) { 113 IFix fix= ExpressionsFix.createRemoveUnnecessaryParenthesisFix(compilationUnit, new ASTNode[] {problem.getCoveredNode(compilationUnit)}); 114 if (fix != null) 115 return true; 116 } 117 return false; 118 } 119 120 123 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 124 return -1; 125 } 126 } 127 | Popular Tags |