1 24 25 package org.aspectj.compiler.base.ast; 26 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.NoSuchElementException ; 31 32 54 55 public final class ControlContext { 56 57 private List context = new ArrayList (); 58 59 63 public void enter(Stmt s) { 64 context.add(s); 65 } 66 67 69 public void exit() { 70 context.remove(context.size() - 1); 71 } 72 73 79 public Stmt getBreakTarget(String label) { 80 return getTarget(label, true); 81 } 82 83 90 public Stmt getContinueTarget(String label) { 91 return getTarget(label, false); 92 } 93 94 99 public boolean isLabelUsed(String label) { 100 for (int i = context.size() - 1; i >= 0; i--) { 101 Object s = context.get(i); 102 if (s instanceof LabeledStmt && 103 label.equals(((LabeledStmt)s).getLabel())) 104 return true; 105 } 106 return false; 107 } 108 109 private Stmt getTarget(String target, boolean isBreak) { 110 int i = context.size() - 1; 114 if (target == null) { 115 if (isBreak) { 116 for (; i >= 0; i--) { 117 Stmt s = (Stmt) context.get(i); 118 if (s.isBreakable()) return s; 119 } 120 } else { 121 for (; i >= 0; i--) { 122 Stmt s = (Stmt) context.get(i); 123 if (s.isContinuable()) return s; 124 } 125 } 126 } else { 127 for (; i >= 0; i--) { 128 Stmt s = (Stmt) context.get(i); 129 if (s instanceof LabeledStmt 130 && target.equals(((LabeledStmt)s).getLabel())) { 131 if (isBreak) return s; 132 s = ((LabeledStmt) s).getStmt(); 133 if (s.isContinuable()) return s; 137 throw new UnsupportedOperationException (); 138 } 139 } 140 } 141 throw new NoSuchElementException (); 142 } 143 144 154 public Iterator getWindsUntil(final Stmt end) { 155 return new Iterator () { 156 private int i = context.size() - 1; 157 private boolean live = true; 158 private void search() { 159 for (; i >= 0; i--) { 160 Object o = context.get(i); 161 if (o == end) { 162 break; 163 } else if (o instanceof WindingStmt) { 164 return; 165 } 166 } 167 live = false; 168 } 169 { 170 search(); 171 } 172 public boolean hasNext() { return live; } 173 public Object next() { 174 if (! live) throw new NoSuchElementException (); 175 Object o = context.get(i--); 176 search(); 177 return o; 178 } 179 public void remove() { 180 throw new UnsupportedOperationException (); 181 } 182 }; 183 } 184 } 185 | Popular Tags |