1 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 fExceptionStack; 42 43 private static final List EMPTY_CATCH_CLAUSE= new ArrayList (0); 44 45 public FlowContext(int start, int length) { 46 fStart= start; 47 fLength= length; 48 fExceptionStack= new ArrayList (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 119 void pushExcptions(TryStatement node) { 120 List 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 exceptions= fExceptionStack.iterator(); exceptions.hasNext(); ) { 133 for (Iterator catchClauses= ((List )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 |