1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.text.edits.TextEditGroup; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IStatus; 21 22 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup; 23 import org.eclipse.ltk.core.refactoring.GroupCategory; 24 import org.eclipse.ltk.core.refactoring.GroupCategorySet; 25 import org.eclipse.ltk.core.refactoring.TextChange; 26 27 import org.eclipse.jdt.core.ICompilationUnit; 28 import org.eclipse.jdt.core.dom.ASTNode; 29 import org.eclipse.jdt.core.dom.CompilationUnit; 30 import org.eclipse.jdt.core.dom.ITypeBinding; 31 import org.eclipse.jdt.core.dom.Type; 32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 33 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; 34 35 import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; 36 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange; 37 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 38 39 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 40 41 42 public abstract class AbstractFix implements IFix { 43 44 public static abstract class AbstractFixRewriteOperation implements IFixRewriteOperation { 45 46 protected Type importType(final ITypeBinding toImport, final ASTNode accessor, ImportRewrite imports, final CompilationUnit compilationUnit) { 47 ImportRewriteContext importContext= new ContextSensitiveImportRewriteContext(compilationUnit, accessor.getStartPosition(), imports); 48 return imports.addImport(toImport, compilationUnit.getAST(), importContext); 49 } 50 51 protected TextEditGroup createTextEditGroup(String label) { 52 if (label.length() > 0){ 53 return new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label))); 54 } else { 55 return new TextEditGroup(label); 56 } 57 } 58 59 public String getAdditionalInfo() { 60 return null; 61 } 62 } 63 64 private final String fName; 65 private final ICompilationUnit fCompilationUnit; 66 private final IFixRewriteOperation[] fFixRewrites; 67 private final CompilationUnit fUnit; 68 private IStatus fStatus; 69 70 protected AbstractFix(String name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewriteOperations) { 71 fName= name; 72 fCompilationUnit= (ICompilationUnit)compilationUnit.getJavaElement(); 73 fFixRewrites= fixRewriteOperations; 74 fUnit= compilationUnit; 75 fStatus= StatusInfo.OK_STATUS; 76 } 77 78 81 public String getDescription() { 82 return fName; 83 } 84 85 88 public ICompilationUnit getCompilationUnit() { 89 return fCompilationUnit; 90 } 91 92 95 public TextChange createChange() throws CoreException { 96 if (fFixRewrites == null || fFixRewrites.length == 0) 97 return null; 98 99 CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(getCompilationUnit(), fUnit); 100 101 List groups= new ArrayList (); 102 103 for (int i= 0; i < fFixRewrites.length; i++) { 104 fFixRewrites[i].rewriteAST(cuRewrite, groups); 105 } 106 107 CompilationUnitChange result= cuRewrite.createChange(getDescription(), true, null); 108 if (result == null) 109 return null; 110 111 for (Iterator iter= groups.iterator(); iter.hasNext();) { 112 TextEditGroup group= (TextEditGroup)iter.next(); 113 result.addTextEditGroup(group); 114 } 115 return result; 116 } 117 118 public String getAdditionalInfo(){ 119 StringBuffer sb= new StringBuffer (); 120 for (int i = 0; i < fFixRewrites.length; i++) { 121 IFixRewriteOperation frw = fFixRewrites[i]; 122 if (frw instanceof AbstractFixRewriteOperation) { 123 AbstractFixRewriteOperation fro= (AbstractFixRewriteOperation) frw; 124 String info= fro.getAdditionalInfo(); 125 if (info != null) 126 sb.append(info); 127 } 128 } 129 if (sb.length() > 0) { 130 return sb.toString(); 131 } 132 return null; 133 } 134 135 138 public IStatus getStatus() { 139 return fStatus; 140 } 141 142 public void setStatus(IStatus status) { 143 fStatus= status; 144 } 145 } 146 | Popular Tags |