KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > ConvertLoopFix


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /**
12  *
13  **/

14 package org.eclipse.jdt.internal.corext.fix;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.List JavaDoc;
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 JavaDoc/*<IFixRewriteOperation>*/fResult;
34         private final Hashtable JavaDoc 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 JavaDoc resultingCollection) {
40             fFindForLoopsToConvert= findForLoopsToConvert;
41             fConvertIterableForLoops= convertIterableForLoops;
42             fMakeFinal= makeFinal;
43             fResult= resultingCollection;
44             fUsedNames= new Hashtable JavaDoc();
45         }
46         
47         /* (non-Javadoc)
48          * @see org.eclipse.jdt.internal.corext.dom.GenericVisitor#visit(org.eclipse.jdt.core.dom.ForStatement)
49          */

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 JavaDoc usedNamesCollection= fUsedNames.values();
80             String JavaDoc[] usedNames= (String JavaDoc[])usedNamesCollection.toArray(new String JavaDoc[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         /* (non-Javadoc)
99          * @see org.eclipse.jdt.internal.corext.dom.GenericVisitor#endVisit(org.eclipse.jdt.core.dom.ForStatement)
100          */

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 JavaDoc operations= new ArrayList JavaDoc();
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 JavaDoc name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewriteOperations) {
148         super(name, compilationUnit, fixRewriteOperations);
149     }
150     
151 }
152
Popular Tags