javap/src/main/java/org/apidesign/javap/TrapDataIterator.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Dec 2012 08:33:40 +0100
branchexceptions
changeset 378 ccb1544a88bc
parent 290 57dff70280c7
child 398 945c799a6812
permissions -rw-r--r--
Fixing license of files that are donated by us
jaroslav@378
     1
/**
jaroslav@378
     2
 * Back 2 Browser Bytecode Translator
jaroslav@378
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@378
     4
 *
jaroslav@378
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@378
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@378
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@378
     8
 *
jaroslav@378
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@378
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@378
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@378
    12
 * GNU General Public License for more details.
jaroslav@378
    13
 *
jaroslav@378
    14
 * You should have received a copy of the GNU General Public License
jaroslav@378
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@378
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@288
    17
 */
jaroslav@288
    18
package org.apidesign.javap;
jaroslav@288
    19
jaroslav@288
    20
/**
jaroslav@288
    21
 *
jaroslav@288
    22
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@288
    23
 */
jaroslav@288
    24
public final class TrapDataIterator {
jaroslav@290
    25
    private final Hashtable exStart = new Hashtable();
jaroslav@290
    26
    private final Hashtable exStop = new Hashtable();
jaroslav@289
    27
    private TrapData[] current = new TrapData[10];
jaroslav@289
    28
    private int currentCount;
jaroslav@288
    29
    
jaroslav@288
    30
    TrapDataIterator(Vector exceptionTable) {
jaroslav@288
    31
        for (int i=0 ; i < exceptionTable.size(); i++) {
jaroslav@288
    32
            final TrapData td = (TrapData)exceptionTable.elementAt(i);
jaroslav@288
    33
            exStart.put(td.start_pc, td);
jaroslav@288
    34
            exStop.put(td.end_pc, td);
jaroslav@288
    35
        }
jaroslav@288
    36
    }
jaroslav@288
    37
jaroslav@288
    38
    public void advanceTo(int i) {
jaroslav@290
    39
        Short s = Short.valueOf((short)i);
jaroslav@290
    40
        TrapData e = (TrapData) exStart.get(s);
jaroslav@288
    41
        if (e != null) {
jaroslav@289
    42
            add(e);
jaroslav@288
    43
        }
jaroslav@290
    44
        e = (TrapData) exStop.get(s);
jaroslav@288
    45
        if (e != null) {
jaroslav@289
    46
            remove(e);
jaroslav@288
    47
        }
jaroslav@288
    48
    }
jaroslav@288
    49
jaroslav@288
    50
    public boolean useTry() {
jaroslav@289
    51
        return currentCount > 0;
jaroslav@288
    52
    }
jaroslav@288
    53
jaroslav@288
    54
    public TrapData[] current() {
jaroslav@289
    55
        return current;
jaroslav@289
    56
    }
jaroslav@289
    57
jaroslav@289
    58
    private void add(TrapData e) {
jaroslav@289
    59
        if (currentCount == current.length) {
jaroslav@289
    60
            TrapData[] data = new TrapData[currentCount * 2];
jaroslav@289
    61
            for (int i = 0; i < currentCount; i++) {
jaroslav@289
    62
                data[i] = current[i];
jaroslav@289
    63
            }
jaroslav@289
    64
            current = data;
jaroslav@289
    65
        }
jaroslav@289
    66
        current[currentCount++] = e;
jaroslav@289
    67
    }
jaroslav@289
    68
jaroslav@289
    69
    private void remove(TrapData e) {
jaroslav@289
    70
        int from = 0;
jaroslav@289
    71
        while (from < currentCount) {
jaroslav@289
    72
            if (e == current[from++]) {
jaroslav@289
    73
                break;
jaroslav@289
    74
            }
jaroslav@289
    75
        }
jaroslav@289
    76
        while (from < currentCount) {
jaroslav@289
    77
            current[from - 1] = current[from];
jaroslav@289
    78
            current[from] = null;
jaroslav@289
    79
            from++;
jaroslav@289
    80
        }
jaroslav@289
    81
        currentCount--;
jaroslav@288
    82
    }
jaroslav@288
    83
}