1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java Tue Feb 26 16:54:16 2013 +0100
1.3 @@ -0,0 +1,114 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +package org.apidesign.javap;
1.22 +
1.23 +/**
1.24 + *
1.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.26 + */
1.27 +public final class TrapDataIterator {
1.28 + private final Hashtable exStart = new Hashtable();
1.29 + private final Hashtable exStop = new Hashtable();
1.30 + private TrapData[] current = new TrapData[10];
1.31 + private int currentCount;
1.32 +
1.33 + TrapDataIterator(Vector exceptionTable) {
1.34 + for (int i=0 ; i < exceptionTable.size(); i++) {
1.35 + final TrapData td = (TrapData)exceptionTable.elementAt(i);
1.36 + put(exStart, td.start_pc, td);
1.37 + put(exStop, td.end_pc, td);
1.38 + }
1.39 + }
1.40 +
1.41 + private static void put(Hashtable h, short key, TrapData td) {
1.42 + Short s = Short.valueOf((short)key);
1.43 + Vector v = (Vector) h.get(s);
1.44 + if (v == null) {
1.45 + v = new Vector(1);
1.46 + h.put(s, v);
1.47 + }
1.48 + v.add(td);
1.49 + }
1.50 +
1.51 + private boolean processAll(Hashtable h, Short key, boolean add) {
1.52 + boolean change = false;
1.53 + Vector v = (Vector)h.get(key);
1.54 + if (v != null) {
1.55 + int s = v.size();
1.56 + for (int i = 0; i < s; i++) {
1.57 + TrapData td = (TrapData)v.elementAt(i);
1.58 + if (add) {
1.59 + add(td);
1.60 + change = true;
1.61 + } else {
1.62 + remove(td);
1.63 + change = true;
1.64 + }
1.65 + }
1.66 + }
1.67 + return change;
1.68 + }
1.69 +
1.70 + public boolean advanceTo(int i) {
1.71 + Short s = Short.valueOf((short)i);
1.72 + boolean ch1 = processAll(exStart, s, true);
1.73 + boolean ch2 = processAll(exStop, s, false);
1.74 + return ch1 || ch2;
1.75 + }
1.76 +
1.77 + public boolean useTry() {
1.78 + return currentCount > 0;
1.79 + }
1.80 +
1.81 + public TrapData[] current() {
1.82 + TrapData[] copy = new TrapData[currentCount];
1.83 + for (int i = 0; i < currentCount; i++) {
1.84 + copy[i] = current[i];
1.85 + }
1.86 + return copy;
1.87 + }
1.88 +
1.89 + private void add(TrapData e) {
1.90 + if (currentCount == current.length) {
1.91 + TrapData[] data = new TrapData[currentCount * 2];
1.92 + for (int i = 0; i < currentCount; i++) {
1.93 + data[i] = current[i];
1.94 + }
1.95 + current = data;
1.96 + }
1.97 + current[currentCount++] = e;
1.98 + }
1.99 +
1.100 + private void remove(TrapData e) {
1.101 + if (currentCount == 0) {
1.102 + return;
1.103 + }
1.104 + int from = 0;
1.105 + while (from < currentCount) {
1.106 + if (e == current[from++]) {
1.107 + break;
1.108 + }
1.109 + }
1.110 + while (from < currentCount) {
1.111 + current[from - 1] = current[from];
1.112 + current[from] = null;
1.113 + from++;
1.114 + }
1.115 + currentCount--;
1.116 + }
1.117 +}