KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.jdt.core.dom.CatchClause;
20 import org.eclipse.jdt.core.dom.ITypeBinding;
21 import org.eclipse.jdt.core.dom.IVariableBinding;
22 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
23 import org.eclipse.jdt.core.dom.TryStatement;
24
25
26 public class FlowContext {
27
28     private static class Enum {
29     }
30     
31     public static final Enum JavaDoc MERGE= new Enum JavaDoc();
32     public static final Enum JavaDoc ARGUMENTS= new Enum JavaDoc();
33     public static final Enum JavaDoc RETURN_VALUES= new Enum JavaDoc();
34     
35     private int fStart;
36     private int fLength;
37     private boolean fConsiderAccessMode;
38     private boolean fLoopReentranceMode;
39     private Enum JavaDoc fComputeMode;
40     private IVariableBinding[] fLocals;
41     private List JavaDoc fExceptionStack;
42     
43     private static final List JavaDoc EMPTY_CATCH_CLAUSE= new ArrayList JavaDoc(0);
44     
45     public FlowContext(int start, int length) {
46         fStart= start;
47         fLength= length;
48         fExceptionStack= new ArrayList JavaDoc(3);
49     }
50     
51     public void setConsiderAccessMode(boolean b) {
52         fConsiderAccessMode= b;
53     }
54     
55     public void setComputeMode(Enum JavaDoc mode) {
56         fComputeMode= mode;
57     }
58     
59     void setLoopReentranceMode(boolean b) {
60         fLoopReentranceMode= b;
61     }
62     
63     int getArrayLength() {
64         return fLength;
65     }
66     
67     int getStartingIndex() {
68         return fStart;
69     }
70     
71     boolean considerAccessMode() {
72         return fConsiderAccessMode;
73     }
74     
75     boolean isLoopReentranceMode() {
76         return fLoopReentranceMode;
77     }
78     
79     boolean computeMerge() {
80         return fComputeMode == MERGE;
81     }
82     
83     boolean computeArguments() {
84         return fComputeMode == ARGUMENTS;
85     }
86     
87     boolean computeReturnValues() {
88         return fComputeMode == RETURN_VALUES;
89     }
90     
91     public IVariableBinding getLocalFromId(int id) {
92         return getLocalFromIndex(id - fStart);
93     }
94     
95     public IVariableBinding getLocalFromIndex(int index) {
96         if (fLocals == null || index > fLocals.length)
97             return null;
98         return fLocals[index];
99     }
100     
101     public int getIndexFromLocal(IVariableBinding local) {
102         if (fLocals == null)
103             return -1;
104         for (int i= 0; i < fLocals.length; i++) {
105             if (fLocals[i] == local)
106                 return i;
107         }
108         return -1;
109     }
110     
111     void manageLocal(IVariableBinding local) {
112         if (fLocals == null)
113             fLocals= new IVariableBinding[fLength];
114         fLocals[local.getVariableId() - fStart]= local;
115     }
116     
117     //---- Exception handling --------------------------------------------------------
118

119     void pushExcptions(TryStatement node) {
120         List JavaDoc catchClauses= node.catchClauses();
121         if (catchClauses == null)
122             catchClauses= EMPTY_CATCH_CLAUSE;
123         fExceptionStack.add(catchClauses);
124     }
125     
126     void popExceptions() {
127         Assert.isTrue(fExceptionStack.size() > 0);
128         fExceptionStack.remove(fExceptionStack.size() - 1);
129     }
130     
131     boolean isExceptionCaught(ITypeBinding excpetionType) {
132         for (Iterator JavaDoc exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) {
133             for (Iterator JavaDoc catchClauses= ((List JavaDoc)exceptions.next()).iterator(); catchClauses.hasNext(); ) {
134                 SingleVariableDeclaration catchedException= ((CatchClause)catchClauses.next()).getException();
135                 IVariableBinding binding= catchedException.resolveBinding();
136                 if (binding == null)
137                     continue;
138                 ITypeBinding catchedType= binding.getType();
139                 while (catchedType != null) {
140                     if (catchedType == excpetionType)
141                         return true;
142                     catchedType= catchedType.getSuperclass();
143                 }
144             }
145         }
146         return false;
147     }
148 }
149
Popular Tags