javap/src/main/java/org/apidesign/javap/TrapDataIterator.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 29 Dec 2012 20:10:10 +0100
changeset 398 945c799a6812
parent 378 ccb1544a88bc
child 400 5452b9fbd253
permissions -rw-r--r--
Use only single try/catch on when there is no branching point
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@398
    38
    public boolean advanceTo(int i) {
jaroslav@398
    39
        boolean change = false;
jaroslav@290
    40
        Short s = Short.valueOf((short)i);
jaroslav@290
    41
        TrapData e = (TrapData) exStart.get(s);
jaroslav@288
    42
        if (e != null) {
jaroslav@289
    43
            add(e);
jaroslav@398
    44
            change = true;
jaroslav@288
    45
        }
jaroslav@290
    46
        e = (TrapData) exStop.get(s);
jaroslav@288
    47
        if (e != null) {
jaroslav@289
    48
            remove(e);
jaroslav@398
    49
            change = true;
jaroslav@288
    50
        }
jaroslav@398
    51
        return change;
jaroslav@288
    52
    }
jaroslav@288
    53
jaroslav@288
    54
    public boolean useTry() {
jaroslav@289
    55
        return currentCount > 0;
jaroslav@288
    56
    }
jaroslav@288
    57
jaroslav@288
    58
    public TrapData[] current() {
jaroslav@289
    59
        return current;
jaroslav@289
    60
    }
jaroslav@289
    61
jaroslav@289
    62
    private void add(TrapData e) {
jaroslav@289
    63
        if (currentCount == current.length) {
jaroslav@289
    64
            TrapData[] data = new TrapData[currentCount * 2];
jaroslav@289
    65
            for (int i = 0; i < currentCount; i++) {
jaroslav@289
    66
                data[i] = current[i];
jaroslav@289
    67
            }
jaroslav@289
    68
            current = data;
jaroslav@289
    69
        }
jaroslav@289
    70
        current[currentCount++] = e;
jaroslav@289
    71
    }
jaroslav@289
    72
jaroslav@289
    73
    private void remove(TrapData e) {
jaroslav@289
    74
        int from = 0;
jaroslav@289
    75
        while (from < currentCount) {
jaroslav@289
    76
            if (e == current[from++]) {
jaroslav@289
    77
                break;
jaroslav@289
    78
            }
jaroslav@289
    79
        }
jaroslav@289
    80
        while (from < currentCount) {
jaroslav@289
    81
            current[from - 1] = current[from];
jaroslav@289
    82
            current[from] = null;
jaroslav@289
    83
            from++;
jaroslav@289
    84
        }
jaroslav@289
    85
        currentCount--;
jaroslav@288
    86
    }
jaroslav@288
    87
}