jaroslav@378: /** jaroslav@378: * Back 2 Browser Bytecode Translator jaroslav@378: * Copyright (C) 2012 Jaroslav Tulach jaroslav@378: * jaroslav@378: * This program is free software: you can redistribute it and/or modify jaroslav@378: * it under the terms of the GNU General Public License as published by jaroslav@378: * the Free Software Foundation, version 2 of the License. jaroslav@378: * jaroslav@378: * This program is distributed in the hope that it will be useful, jaroslav@378: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@378: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@378: * GNU General Public License for more details. jaroslav@378: * jaroslav@378: * You should have received a copy of the GNU General Public License jaroslav@378: * along with this program. Look for COPYING file in the top folder. jaroslav@378: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@288: */ jaroslav@288: package org.apidesign.javap; jaroslav@288: jaroslav@288: /** jaroslav@288: * jaroslav@288: * @author Jaroslav Tulach jaroslav@288: */ jaroslav@288: public final class TrapDataIterator { jaroslav@290: private final Hashtable exStart = new Hashtable(); jaroslav@290: private final Hashtable exStop = new Hashtable(); jaroslav@289: private TrapData[] current = new TrapData[10]; jaroslav@289: private int currentCount; jaroslav@288: jaroslav@288: TrapDataIterator(Vector exceptionTable) { jaroslav@288: for (int i=0 ; i < exceptionTable.size(); i++) { jaroslav@288: final TrapData td = (TrapData)exceptionTable.elementAt(i); jaroslav@400: put(exStart, td.start_pc, td); jaroslav@400: put(exStop, td.end_pc, td); jaroslav@288: } jaroslav@288: } jaroslav@400: jaroslav@400: private static void put(Hashtable h, short key, TrapData td) { jaroslav@400: Short s = Short.valueOf((short)key); jaroslav@400: Vector v = (Vector) h.get(s); jaroslav@400: if (v == null) { jaroslav@400: v = new Vector(1); jaroslav@400: h.put(s, v); jaroslav@400: } jaroslav@400: v.add(td); jaroslav@400: } jaroslav@400: jaroslav@400: private boolean processAll(Hashtable h, Short key, boolean add) { jaroslav@398: boolean change = false; jaroslav@400: Vector v = (Vector)h.get(key); jaroslav@400: if (v != null) { jaroslav@400: int s = v.size(); jaroslav@400: for (int i = 0; i < s; i++) { jaroslav@400: TrapData td = (TrapData)v.elementAt(i); jaroslav@400: if (add) { jaroslav@400: add(td); jaroslav@400: change = true; jaroslav@400: } else { jaroslav@400: remove(td); jaroslav@400: change = true; jaroslav@400: } jaroslav@400: } jaroslav@288: } jaroslav@398: return change; jaroslav@288: } jaroslav@400: jaroslav@400: public boolean advanceTo(int i) { jaroslav@400: Short s = Short.valueOf((short)i); jaroslav@400: boolean ch1 = processAll(exStart, s, true); jaroslav@400: boolean ch2 = processAll(exStop, s, false); jaroslav@400: return ch1 || ch2; jaroslav@400: } jaroslav@288: jaroslav@288: public boolean useTry() { jaroslav@289: return currentCount > 0; jaroslav@288: } jaroslav@288: jaroslav@288: public TrapData[] current() { jaroslav@400: TrapData[] copy = new TrapData[currentCount]; jaroslav@400: for (int i = 0; i < currentCount; i++) { jaroslav@400: copy[i] = current[i]; jaroslav@400: } jaroslav@400: return copy; jaroslav@289: } jaroslav@289: jaroslav@289: private void add(TrapData e) { jaroslav@289: if (currentCount == current.length) { jaroslav@289: TrapData[] data = new TrapData[currentCount * 2]; jaroslav@289: for (int i = 0; i < currentCount; i++) { jaroslav@289: data[i] = current[i]; jaroslav@289: } jaroslav@289: current = data; jaroslav@289: } jaroslav@289: current[currentCount++] = e; jaroslav@289: } jaroslav@289: jaroslav@289: private void remove(TrapData e) { jaroslav@400: if (currentCount == 0) { jaroslav@400: return; jaroslav@400: } jaroslav@289: int from = 0; jaroslav@289: while (from < currentCount) { jaroslav@289: if (e == current[from++]) { jaroslav@289: break; jaroslav@289: } jaroslav@289: } jaroslav@289: while (from < currentCount) { jaroslav@289: current[from - 1] = current[from]; jaroslav@289: current[from] = null; jaroslav@289: from++; jaroslav@289: } jaroslav@289: currentCount--; jaroslav@288: } jaroslav@288: }