1 11 14 package org.eclipse.jdt.internal.corext.fix; 15 16 import java.util.ArrayList ; 17 import java.util.Arrays ; 18 import java.util.Collection ; 19 import java.util.List ; 20 21 import org.eclipse.text.edits.TextEditGroup; 22 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.core.runtime.Status; 26 27 import org.eclipse.jdt.core.dom.CompilationUnit; 28 import org.eclipse.jdt.core.dom.ForStatement; 29 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 30 import org.eclipse.jdt.core.dom.Statement; 31 import org.eclipse.jdt.core.dom.VariableDeclarationFragment; 32 33 import org.eclipse.jdt.internal.corext.dom.GenericVisitor; 34 import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer; 35 import org.eclipse.jdt.internal.corext.fix.LinkedFix.AbstractLinkedFixRewriteOperation; 36 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 40 public abstract class ConvertLoopOperation extends AbstractLinkedFixRewriteOperation { 41 42 protected static final String FOR_LOOP_ELEMENT_IDENTIFIER= "element"; 44 protected static final IStatus ERROR_STATUS= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), ""); 46 private final ForStatement fStatement; 47 private ConvertLoopOperation fOperation; 48 private final String [] fUsedNames; 49 50 public ConvertLoopOperation(ForStatement statement, String [] usedNames) { 51 fStatement= statement; 52 fUsedNames= usedNames; 53 } 54 55 public void setBodyConverter(ConvertLoopOperation operation) { 56 fOperation= operation; 57 } 58 59 public abstract String getIntroducedVariableName(); 60 61 public abstract IStatus satisfiesPreconditions(); 62 63 protected abstract Statement convert(CompilationUnitRewrite cuRewrite, TextEditGroup group, LinkedProposalModel positionGroups) throws CoreException; 64 65 protected ForStatement getForStatement() { 66 return fStatement; 67 } 68 69 protected Statement getBody(CompilationUnitRewrite cuRewrite, TextEditGroup group, LinkedProposalModel positionGroups) throws CoreException { 70 if (fOperation != null) { 71 return fOperation.convert(cuRewrite, group, positionGroups); 72 } else { 73 return (Statement)cuRewrite.getASTRewrite().createMoveTarget(getForStatement().getBody()); 74 } 75 } 76 77 protected String [] getUsedVariableNames() { 78 final List results= new ArrayList (); 79 80 ForStatement forStatement= getForStatement(); 81 CompilationUnit root= (CompilationUnit)forStatement.getRoot(); 82 83 Collection variableNames= new ScopeAnalyzer(root).getUsedVariableNames(forStatement.getStartPosition(), forStatement.getLength()); 84 results.addAll(variableNames); 85 86 forStatement.accept(new GenericVisitor() { 87 public boolean visit(SingleVariableDeclaration node) { 88 results.add(node.getName().getIdentifier()); 89 return super.visit(node); 90 } 91 92 public boolean visit(VariableDeclarationFragment fragment) { 93 results.add(fragment.getName().getIdentifier()); 94 return super.visit(fragment); 95 } 96 }); 97 98 results.addAll(Arrays.asList(fUsedNames)); 99 100 return (String [])results.toArray(new String [results.size()]); 101 } 102 103 } | Popular Tags |