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.CodeStyleFix; 27 import org.eclipse.jdt.internal.corext.fix.IFix; 28 29 import org.eclipse.jdt.ui.text.java.IProblemLocation; 30 31 35 public class CodeStyleCleanUp extends AbstractCleanUp { 36 37 public CodeStyleCleanUp() { 38 this(null); 39 } 40 41 public CodeStyleCleanUp(Map options) { 42 super(options); 43 } 44 45 48 public boolean requireAST(ICompilationUnit unit) throws CoreException { 49 boolean nonStaticFields= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS); 50 boolean nonStaticMethods= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS); 51 boolean qualifyStatic= isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS); 52 53 return nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS) || 54 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS) || 55 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD) || 56 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS) || 57 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS) || 58 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD) || 59 nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY) || 60 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY); 61 } 62 63 public IFix createFix(CompilationUnit compilationUnit) throws CoreException { 64 if (compilationUnit == null) 65 return null; 66 67 boolean nonStaticFields= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS); 68 boolean nonStaticMethods= isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS); 69 boolean qualifyStatic= isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS); 70 71 return CodeStyleFix.createCleanUp(compilationUnit, 72 nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS), 73 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS), 74 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD), 75 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS), 76 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS), 77 qualifyStatic && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD), 78 nonStaticFields && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY), 79 nonStaticMethods && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY) 80 ); 81 } 82 83 86 public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException { 87 if (compilationUnit == null) 88 return null; 89 90 return CodeStyleFix.createCleanUp(compilationUnit, problems, 91 isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS), 92 isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS), 93 isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)); 94 } 95 96 public Map getRequiredOptions() { 97 Map options= new Hashtable (); 98 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) 99 options.put(JavaCore.COMPILER_PB_STATIC_ACCESS_RECEIVER, JavaCore.WARNING); 100 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) 101 options.put(JavaCore.COMPILER_PB_INDIRECT_STATIC_ACCESS, JavaCore.WARNING); 102 return options; 103 } 104 105 108 public String [] getDescriptions() { 109 List result= new ArrayList (); 110 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) 111 result.add(MultiFixMessages.CodeStyleMultiFix_AddThisQualifier_description); 112 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS)) 113 result.add(MultiFixMessages.CodeStyleCleanUp_QualifyNonStaticMethod_description); 114 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY)) 115 result.add(MultiFixMessages.CodeStyleCleanUp_removeFieldThis_description); 116 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY)) 117 result.add(MultiFixMessages.CodeStyleCleanUp_removeMethodThis_description); 118 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD)) 119 result.add(MultiFixMessages.CodeStyleMultiFix_QualifyAccessToStaticField); 120 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD)) 121 result.add(MultiFixMessages.CodeStyleCleanUp_QualifyStaticMethod_description); 122 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) 123 result.add(MultiFixMessages.CodeStyleMultiFix_ChangeNonStaticAccess_description); 124 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) 125 result.add(MultiFixMessages.CodeStyleMultiFix_ChangeIndirectAccessToStaticToDirect); 126 return (String [])result.toArray(new String [result.size()]); 127 } 128 129 public String getPreview() { 130 StringBuffer buf= new StringBuffer (); 131 132 buf.append("private int value;\n"); buf.append("public int get() {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) { 135 buf.append(" return this.value + this.value;\n"); } else if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_IF_NECESSARY)) { 137 buf.append(" return value + value;\n"); } else { 139 buf.append(" return this.value + value;\n"); } 141 buf.append("}\n"); buf.append("\n"); buf.append("public int getZero() {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_ALWAYS)) { 145 buf.append(" return this.get() - this.get();\n"); } else if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_METHOD_USE_THIS_IF_NECESSARY)) { 147 buf.append(" return get() - get();\n"); } else { 149 buf.append(" return this.get() - get();\n"); } 151 buf.append("}\n"); buf.append("\n"); buf.append("class E {\n"); buf.append(" public static int NUMBER;\n"); buf.append(" public static void set(int i) {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_FIELD)) { 157 buf.append(" E.NUMBER= i;\n"); } else { 159 buf.append(" NUMBER= i;\n"); } 161 buf.append(" }\n"); buf.append("\n"); buf.append(" public void reset() {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_METHOD)) { 165 buf.append(" E.set(0);\n"); } else { 167 buf.append(" set(0);\n"); } 169 buf.append(" }\n"); buf.append("}\n"); buf.append("\n"); buf.append("class ESub extends E {\n"); buf.append(" public void reset() {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) { 175 buf.append(" E.NUMBER= 0;\n"); } else { 177 buf.append(" ESub.NUMBER= 0;\n"); } 179 buf.append(" }\n"); buf.append("}\n"); buf.append("\n"); buf.append("public void dec() {\n"); if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) { 184 buf.append(" E.NUMBER--;\n"); } else { 186 buf.append(" (new E()).NUMBER--;\n"); } 188 buf.append("}\n"); 190 return buf.toString(); 191 } 192 193 196 public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException { 197 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) { 198 CodeStyleFix fix= CodeStyleFix.createAddFieldQualifierFix(compilationUnit, problem); 199 if (fix != null) 200 return true; 201 } 202 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) { 203 CodeStyleFix fix= CodeStyleFix.createIndirectAccessToStaticFix(compilationUnit, problem); 204 if (fix != null) 205 return true; 206 } 207 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) { 208 CodeStyleFix[] fixes= CodeStyleFix.createNonStaticAccessFixes(compilationUnit, problem); 209 if (fixes != null && fixes.length > 0) 210 return true; 211 } 212 return false; 213 } 214 215 218 public int maximalNumberOfFixes(CompilationUnit compilationUnit) { 219 int result= 0; 220 IProblem[] problems= compilationUnit.getProblems(); 221 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_NON_STATIC_FIELD_USE_THIS_ALWAYS)) 222 result+= getNumberOfProblems(problems, IProblem.UnqualifiedFieldAccess); 223 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_SUBTYPE_ACCESS)) { 224 for (int i=0;i<problems.length;i++) { 225 int id= problems[i].getID(); 226 if (id == IProblem.IndirectAccessToStaticField || id == IProblem.IndirectAccessToStaticMethod) 227 result++; 228 } 229 } 230 if (isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS) && isEnabled(CleanUpConstants.MEMBER_ACCESSES_STATIC_QUALIFY_WITH_DECLARING_CLASS_INSTANCE_ACCESS)) { 231 for (int i=0;i<problems.length;i++) { 232 int id= problems[i].getID(); 233 if (id == IProblem.NonStaticAccessToStaticField || id == IProblem.NonStaticAccessToStaticMethod) 234 result++; 235 } 236 } 237 return result; 238 } 239 240 } 241 | Popular Tags |