]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.Iterator; | |
15 | import java.util.List; | |
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 MERGE= new Enum(); | |
32 | public static final Enum ARGUMENTS= new Enum(); | |
33 | public static final Enum RETURN_VALUES= new Enum(); | |
34 | ||
35 | private int fStart; | |
36 | private int fLength; | |
37 | private boolean fConsiderAccessMode; | |
38 | private boolean fLoopReentranceMode; | |
39 | private Enum fComputeMode; | |
40 | private IVariableBinding[] fLocals; | |
41 | private List<List<CatchClause>> fExceptionStack; | |
42 | ||
43 | private static final List<CatchClause> EMPTY_CATCH_CLAUSE= new ArrayList<CatchClause>(0); | |
44 | ||
45 | public FlowContext(int start, int length) { | |
46 | fStart= start; | |
47 | fLength= length; | |
48 | fExceptionStack= new ArrayList<List<CatchClause>>(3); | |
49 | } | |
50 | ||
51 | public void setConsiderAccessMode(boolean b) { | |
52 | fConsiderAccessMode= b; | |
53 | } | |
54 | ||
55 | public void setComputeMode(Enum 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<CatchClause> 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<List<CatchClause>> exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) { | |
133 | for (Iterator<CatchClause> catchClauses= exceptions.next().iterator(); catchClauses.hasNext(); ) { | |
134 | SingleVariableDeclaration caughtException= catchClauses.next().getException(); | |
135 | IVariableBinding binding= caughtException.resolveBinding(); | |
136 | if (binding == null) | |
137 | continue; | |
138 | ITypeBinding caughtype= binding.getType(); | |
139 | while (caughtype != null) { | |
140 | if (caughtype == excpetionType) | |
141 | return true; | |
142 | caughtype= caughtype.getSuperclass(); | |
143 | } | |
144 | } | |
145 | } | |
146 | return false; | |
147 | } | |
148 | } |