rt/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 400 javap/src/main/java/org/apidesign/javap/TrapDataIterator.java@5452b9fbd253
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
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@400
    33
            put(exStart, td.start_pc, td);
jaroslav@400
    34
            put(exStop, td.end_pc, td);
jaroslav@288
    35
        }
jaroslav@288
    36
    }
jaroslav@400
    37
    
jaroslav@400
    38
    private static void put(Hashtable h, short key, TrapData td) {
jaroslav@400
    39
        Short s = Short.valueOf((short)key);
jaroslav@400
    40
        Vector v = (Vector) h.get(s);
jaroslav@400
    41
        if (v == null) {
jaroslav@400
    42
            v = new Vector(1);
jaroslav@400
    43
            h.put(s, v);
jaroslav@400
    44
        }
jaroslav@400
    45
        v.add(td);
jaroslav@400
    46
    }
jaroslav@400
    47
    
jaroslav@400
    48
    private boolean processAll(Hashtable h, Short key, boolean add) {
jaroslav@398
    49
        boolean change = false;
jaroslav@400
    50
        Vector v = (Vector)h.get(key);
jaroslav@400
    51
        if (v != null) {
jaroslav@400
    52
            int s = v.size();
jaroslav@400
    53
            for (int i = 0; i < s; i++) {
jaroslav@400
    54
                TrapData td = (TrapData)v.elementAt(i);
jaroslav@400
    55
                if (add) {
jaroslav@400
    56
                    add(td);
jaroslav@400
    57
                    change = true;
jaroslav@400
    58
                } else {
jaroslav@400
    59
                    remove(td);
jaroslav@400
    60
                    change = true;
jaroslav@400
    61
                }
jaroslav@400
    62
            }
jaroslav@288
    63
        }
jaroslav@398
    64
        return change;
jaroslav@288
    65
    }
jaroslav@400
    66
    
jaroslav@400
    67
    public boolean advanceTo(int i) {
jaroslav@400
    68
        Short s = Short.valueOf((short)i);
jaroslav@400
    69
        boolean ch1 = processAll(exStart, s, true);
jaroslav@400
    70
        boolean ch2 = processAll(exStop, s, false);
jaroslav@400
    71
        return ch1 || ch2;
jaroslav@400
    72
    }
jaroslav@288
    73
jaroslav@288
    74
    public boolean useTry() {
jaroslav@289
    75
        return currentCount > 0;
jaroslav@288
    76
    }
jaroslav@288
    77
jaroslav@288
    78
    public TrapData[] current() {
jaroslav@400
    79
        TrapData[] copy = new TrapData[currentCount];
jaroslav@400
    80
        for (int i = 0; i < currentCount; i++) {
jaroslav@400
    81
            copy[i] = current[i];
jaroslav@400
    82
        }
jaroslav@400
    83
        return copy;
jaroslav@289
    84
    }
jaroslav@289
    85
jaroslav@289
    86
    private void add(TrapData e) {
jaroslav@289
    87
        if (currentCount == current.length) {
jaroslav@289
    88
            TrapData[] data = new TrapData[currentCount * 2];
jaroslav@289
    89
            for (int i = 0; i < currentCount; i++) {
jaroslav@289
    90
                data[i] = current[i];
jaroslav@289
    91
            }
jaroslav@289
    92
            current = data;
jaroslav@289
    93
        }
jaroslav@289
    94
        current[currentCount++] = e;
jaroslav@289
    95
    }
jaroslav@289
    96
jaroslav@289
    97
    private void remove(TrapData e) {
jaroslav@400
    98
        if (currentCount == 0) {
jaroslav@400
    99
            return;
jaroslav@400
   100
        }
jaroslav@289
   101
        int from = 0;
jaroslav@289
   102
        while (from < currentCount) {
jaroslav@289
   103
            if (e == current[from++]) {
jaroslav@289
   104
                break;
jaroslav@289
   105
            }
jaroslav@289
   106
        }
jaroslav@289
   107
        while (from < currentCount) {
jaroslav@289
   108
            current[from - 1] = current[from];
jaroslav@289
   109
            current[from] = null;
jaroslav@289
   110
            from++;
jaroslav@289
   111
        }
jaroslav@289
   112
        currentCount--;
jaroslav@288
   113
    }
jaroslav@288
   114
}