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.CompilationUnit; 21 22 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; 23 import org.eclipse.jdt.internal.corext.fix.ControlStatementsFix; 24 import org.eclipse.jdt.internal.corext.fix.IFix; 25 26 import org.eclipse.jdt.ui.text.java.IProblemLocation; 27 28 public class ControlStatementsCleanUp extends AbstractCleanUp { 29 30 public ControlStatementsCleanUp(Map options) { 31 super(options); 32 } 33 34 public ControlStatementsCleanUp() { 35 super(); 36 } 37 38 41 public boolean requireAST(ICompilationUnit unit) throws CoreException { 42 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS); 43 44 if (!useBlocks) 45 return false; 46 47 return isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS) || 48 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER) || 49 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW); 50 } 51 52 55 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 56 if (compilationUnit == null) 57 return null; 58 59 boolean useBlocks= isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS); 60 if (!useBlocks) 61 return null; 62 63 return ControlStatementsFix.createCleanUp(compilationUnit, 64 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS), 65 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER), 66 isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW)); 67 } 68 69 72 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 73 return null; 75 } 76 77 80 public Map getRequiredOptions() { 81 return null; 82 } 83 84 87 public String [] getDescriptions() { 88 List result= new ArrayList (); 89 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS)) 90 result.add(MultiFixMessages.CodeStyleMultiFix_ConvertSingleStatementInControlBodeyToBlock_description); 91 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER)) 92 result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocks_description); 93 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW)) 94 result.add(MultiFixMessages.ControlStatementsCleanUp_RemoveUnnecessaryBlocksWithReturnOrThrow_description); 95 96 return (String [])result.toArray(new String [result.size()]); 97 } 98 99 public String getPreview() { 100 StringBuffer buf= new StringBuffer (); 101 102 if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS)) { 103 buf.append("if (obj == null) {\n"); buf.append(" throw new IllegalArgumentException();\n"); buf.append("}\n"); 107 buf.append("if (ids.length > 0) {\n"); buf.append(" System.out.println(ids[0]);\n"); buf.append("} else {\n"); buf.append(" return;\n"); buf.append("}\n"); } else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER)){ 113 buf.append("if (obj == null)\n"); buf.append(" throw new IllegalArgumentException();\n"); buf.append("\n"); 117 buf.append("if (ids.length > 0)\n"); buf.append(" System.out.println(ids[0]);\n"); buf.append("else\n"); buf.append(" return;\n"); buf.append("\n"); } else if (isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS) && isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW)) { 123 buf.append("if (obj == null)\n"); buf.append(" throw new IllegalArgumentException();\n"); buf.append("\n"); 127 buf.append("if (ids.length > 0) {\n"); buf.append(" System.out.println(ids[0]);\n"); buf.append("} else \n"); buf.append(" return;\n"); buf.append("\n"); } else { 133 buf.append("if (obj == null) {\n"); buf.append(" throw new IllegalArgumentException();\n"); buf.append("}\n"); 137 buf.append("if (ids.length > 0) {\n"); buf.append(" System.out.println(ids[0]);\n"); buf.append("} else \n"); buf.append(" return;\n"); buf.append("\n"); } 143 144 return buf.toString(); 145 } 146 147 150 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 151 return false; 152 } 153 154 157 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 158 return -1; 159 } 160 } 161 | Popular Tags |