KickJava   Java API By Example, From Geeks To Geeks.

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


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.Arrays JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.List JavaDoc;
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 JavaDoc FOR_LOOP_ELEMENT_IDENTIFIER= "element"; //$NON-NLS-1$
43

44     protected static final IStatus ERROR_STATUS= new Status(IStatus.ERROR, JavaPlugin.getPluginId(), ""); //$NON-NLS-1$
45

46     private final ForStatement fStatement;
47     private ConvertLoopOperation fOperation;
48     private final String JavaDoc[] fUsedNames;
49     
50     public ConvertLoopOperation(ForStatement statement, String JavaDoc[] usedNames) {
51         fStatement= statement;
52         fUsedNames= usedNames;
53     }
54     
55     public void setBodyConverter(ConvertLoopOperation operation) {
56         fOperation= operation;
57     }
58     
59     public abstract String JavaDoc 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 JavaDoc[] getUsedVariableNames() {
78         final List JavaDoc results= new ArrayList JavaDoc();
79         
80         ForStatement forStatement= getForStatement();
81         CompilationUnit root= (CompilationUnit)forStatement.getRoot();
82         
83         Collection JavaDoc 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 JavaDoc[])results.toArray(new String JavaDoc[results.size()]);
101     }
102     
103 }
Popular Tags