1 11 14 package org.eclipse.jdt.internal.corext.fix; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Hashtable ; 19 import java.util.List ; 20 21 import org.eclipse.core.runtime.IStatus; 22 23 import org.eclipse.jdt.core.dom.CompilationUnit; 24 import org.eclipse.jdt.core.dom.ForStatement; 25 26 import org.eclipse.jdt.internal.corext.dom.GenericVisitor; 27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 28 29 public class ConvertLoopFix extends LinkedFix { 30 31 private final static class ControlStatementFinder extends GenericVisitor { 32 33 private final List fResult; 34 private final Hashtable fUsedNames; 35 private final boolean fFindForLoopsToConvert; 36 private final boolean fConvertIterableForLoops; 37 private final boolean fMakeFinal; 38 39 public ControlStatementFinder(boolean findForLoopsToConvert, boolean convertIterableForLoops, boolean makeFinal, List resultingCollection) { 40 fFindForLoopsToConvert= findForLoopsToConvert; 41 fConvertIterableForLoops= convertIterableForLoops; 42 fMakeFinal= makeFinal; 43 fResult= resultingCollection; 44 fUsedNames= new Hashtable (); 45 } 46 47 50 public boolean visit(ForStatement node) { 51 if (fFindForLoopsToConvert || fConvertIterableForLoops) { 52 ForStatement current= node; 53 ConvertLoopOperation operation= getConvertOperation(current); 54 ConvertLoopOperation oldOperation= null; 55 while (operation != null) { 56 if (oldOperation == null) { 57 fResult.add(operation); 58 } else { 59 oldOperation.setBodyConverter(operation); 60 } 61 62 if (current.getBody() instanceof ForStatement) { 63 current= (ForStatement)current.getBody(); 64 oldOperation= operation; 65 operation= getConvertOperation(current); 66 } else { 67 operation= null; 68 } 69 } 70 current.getBody().accept(this); 71 return false; 72 } 73 74 return super.visit(node); 75 } 76 77 private ConvertLoopOperation getConvertOperation(ForStatement node) { 78 79 Collection usedNamesCollection= fUsedNames.values(); 80 String [] usedNames= (String [])usedNamesCollection.toArray(new String [usedNamesCollection.size()]); 81 ConvertLoopOperation convertForLoopOperation= new ConvertForLoopOperation(node, usedNames, fMakeFinal); 82 if (convertForLoopOperation.satisfiesPreconditions().isOK()) { 83 if (fFindForLoopsToConvert) { 84 fUsedNames.put(node, convertForLoopOperation.getIntroducedVariableName()); 85 return convertForLoopOperation; 86 } 87 } else if (fConvertIterableForLoops) { 88 ConvertLoopOperation iterableConverter= new ConvertIterableLoopOperation(node, usedNames, fMakeFinal); 89 if (iterableConverter.satisfiesPreconditions().isOK()) { 90 fUsedNames.put(node, iterableConverter.getIntroducedVariableName()); 91 return iterableConverter; 92 } 93 } 94 95 return null; 96 } 97 98 101 public void endVisit(ForStatement node) { 102 if (fFindForLoopsToConvert || fConvertIterableForLoops) { 103 fUsedNames.remove(node); 104 } 105 super.endVisit(node); 106 } 107 108 } 109 110 public static IFix createCleanUp(CompilationUnit compilationUnit, boolean convertForLoops, boolean convertIterableForLoops, boolean makeFinal) { 111 if (!JavaModelUtil.is50OrHigher(compilationUnit.getJavaElement().getJavaProject())) 112 return null; 113 114 if (!convertForLoops && !convertIterableForLoops) 115 return null; 116 117 List operations= new ArrayList (); 118 ControlStatementFinder finder= new ControlStatementFinder(convertForLoops, convertIterableForLoops, makeFinal, operations); 119 compilationUnit.accept(finder); 120 121 if (operations.isEmpty()) 122 return null; 123 124 IFixRewriteOperation[] ops= (IFixRewriteOperation[])operations.toArray(new IFixRewriteOperation[operations.size()]); 125 return new ConvertLoopFix(FixMessages.ControlStatementsFix_change_name, compilationUnit, ops); 126 } 127 128 public static IFix createConvertForLoopToEnhancedFix(CompilationUnit compilationUnit, ForStatement loop) { 129 ConvertLoopOperation convertForLoopOperation= new ConvertForLoopOperation(loop); 130 if (!convertForLoopOperation.satisfiesPreconditions().isOK()) 131 return null; 132 133 return new ConvertLoopFix(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, compilationUnit, new ILinkedFixRewriteOperation[] {convertForLoopOperation}); 134 } 135 136 public static IFix createConvertIterableLoopToEnhancedFix(CompilationUnit compilationUnit, ForStatement loop) { 137 ConvertIterableLoopOperation loopConverter= new ConvertIterableLoopOperation(loop); 138 IStatus status= loopConverter.satisfiesPreconditions(); 139 if (status.getSeverity() == IStatus.ERROR) 140 return null; 141 142 ConvertLoopFix result= new ConvertLoopFix(FixMessages.Java50Fix_ConvertToEnhancedForLoop_description, compilationUnit, new ILinkedFixRewriteOperation[] {loopConverter}); 143 result.setStatus(status); 144 return result; 145 } 146 147 protected ConvertLoopFix(String name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewriteOperations) { 148 super(name, compilationUnit, fixRewriteOperations); 149 } 150 151 } 152 | Popular Tags |