KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > code > flow > InOutFlowAnalyzer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 package org.eclipse.jdt.internal.corext.refactoring.code.flow;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jdt.core.dom.ASTNode;
17 import org.eclipse.jdt.core.dom.Block;
18 import org.eclipse.jdt.core.dom.CatchClause;
19 import org.eclipse.jdt.core.dom.EnhancedForStatement;
20 import org.eclipse.jdt.core.dom.ForStatement;
21 import org.eclipse.jdt.core.dom.IVariableBinding;
22 import org.eclipse.jdt.core.dom.MethodDeclaration;
23 import org.eclipse.jdt.core.dom.ReturnStatement;
24 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
25 import org.eclipse.jdt.core.dom.VariableDeclarationExpression;
26 import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
27 import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
28
29 public class InOutFlowAnalyzer extends FlowAnalyzer {
30     
31     public InOutFlowAnalyzer(FlowContext context) {
32         super(context);
33     }
34     
35     public FlowInfo perform(ASTNode[] selectedNodes) {
36         FlowContext context= getFlowContext();
37         GenericSequentialFlowInfo result= createSequential();
38         for (int i= 0; i < selectedNodes.length; i++) {
39             ASTNode node= selectedNodes[i];
40             node.accept(this);
41             result.merge(getFlowInfo(node), context);
42         }
43         return result;
44     }
45     
46     protected boolean traverseNode(ASTNode node) {
47         // we are only traversing the selected nodes.
48
return true;
49     }
50     
51     protected boolean createReturnFlowInfo(ReturnStatement node) {
52         // we are only traversing selected nodes.
53
return true;
54     }
55     
56     public void endVisit(Block node) {
57         super.endVisit(node);
58         clearAccessMode(accessFlowInfo(node), node.statements());
59     }
60     
61     public void endVisit(CatchClause node) {
62         super.endVisit(node);
63         clearAccessMode(accessFlowInfo(node), node.getException());
64     }
65     
66     public void endVisit(EnhancedForStatement node) {
67         super.endVisit(node);
68         clearAccessMode(accessFlowInfo(node), node.getParameter());
69     }
70     
71     public void endVisit(ForStatement node) {
72         super.endVisit(node);
73         clearAccessMode(accessFlowInfo(node), node.initializers());
74     }
75     
76     public void endVisit(MethodDeclaration node) {
77         super.endVisit(node);
78         FlowInfo info= accessFlowInfo(node);
79         for (Iterator JavaDoc iter= node.parameters().iterator(); iter.hasNext();) {
80             clearAccessMode(info, (SingleVariableDeclaration)iter.next());
81         }
82     }
83
84     private void clearAccessMode(FlowInfo info, SingleVariableDeclaration decl) {
85         IVariableBinding binding= decl.resolveBinding();
86         if (binding != null && !binding.isField())
87             info.clearAccessMode(binding, fFlowContext);
88     }
89     
90     private void clearAccessMode(FlowInfo info, List JavaDoc nodes) {
91         if (nodes== null || nodes.isEmpty() || info == null)
92             return;
93         for (Iterator JavaDoc iter= nodes.iterator(); iter.hasNext(); ) {
94             Object JavaDoc node= iter.next();
95             Iterator JavaDoc fragments= null;
96             if (node instanceof VariableDeclarationStatement) {
97                 fragments= ((VariableDeclarationStatement)node).fragments().iterator();
98             } else if (node instanceof VariableDeclarationExpression) {
99                 fragments= ((VariableDeclarationExpression)node).fragments().iterator();
100             }
101             if (fragments != null) {
102                 while (fragments.hasNext()) {
103                     clearAccessMode(info, (VariableDeclarationFragment)fragments.next());
104                 }
105             }
106         }
107     }
108     
109     private void clearAccessMode(FlowInfo info, VariableDeclarationFragment fragment) {
110             IVariableBinding binding= fragment.resolveBinding();
111             if (binding != null && !binding.isField())
112                 info.clearAccessMode(binding, fFlowContext);
113     }
114 }
115
116
Popular Tags