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.IFix; 24 import org.eclipse.jdt.internal.corext.fix.VariableDeclarationFix; 25 26 import org.eclipse.jdt.ui.text.java.IProblemLocation; 27 28 public class VariableDeclarationCleanUp extends AbstractCleanUp { 29 30 public VariableDeclarationCleanUp(Map options) { 31 super(options); 32 } 33 34 public VariableDeclarationCleanUp() { 35 super(); 36 } 37 38 41 public boolean requireAST(ICompilationUnit unit) throws CoreException { 42 boolean addFinal= isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL); 43 if (!addFinal) 44 return false; 45 46 return isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS) || 47 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS) || 48 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES); 49 } 50 51 54 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 55 if (compilationUnit == null) 56 return null; 57 58 boolean addFinal= isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL); 59 if (!addFinal) 60 return null; 61 62 return VariableDeclarationFix.createCleanUp(compilationUnit, 63 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS), 64 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS), 65 isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES)); 66 } 67 68 71 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 72 return null; 74 } 75 76 79 public Map getRequiredOptions() { 80 return null; 81 } 82 83 86 public String [] getDescriptions() { 87 List result= new ArrayList (); 88 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS)) 89 result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalField_description); 90 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS)) 91 result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalParameters_description); 92 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES)) 93 result.add(MultiFixMessages.VariableDeclarationCleanUp_AddFinalLocals_description); 94 95 return (String [])result.toArray(new String [result.size()]); 96 } 97 98 public String getPreview() { 99 StringBuffer buf= new StringBuffer (); 100 101 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PRIVATE_FIELDS)) { 102 buf.append("private final int i= 0;\n"); } else { 104 buf.append("private int i= 0;\n"); } 106 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_PARAMETERS)) { 107 buf.append("public void foo(final int j) {\n"); } else { 109 buf.append("public void foo(int j) {\n"); } 111 if (isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL) && isEnabled(CleanUpConstants.VARIABLE_DECLARATIONS_USE_FINAL_LOCAL_VARIABLES)) { 112 buf.append(" final int k;\n"); buf.append(" int h;\n"); buf.append(" h= 0;\n"); } else { 116 buf.append(" int k, h;\n"); buf.append(" h= 0;\n"); } 119 buf.append("}\n"); 121 return buf.toString(); 122 } 123 124 127 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 128 return false; 129 } 130 131 134 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 135 return -1; 136 } 137 } 138 | Popular Tags |