Rather than exposing Vector, providing abstraction to encapsulate manipulation with TrapData via 'iterator' exceptions
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 09 Dec 2012 16:16:19 +0100
branchexceptions
changeset 2886d1e8eccdc98
parent 287 6f696a0ef12f
child 289 a2ca83ddc521
Rather than exposing Vector, providing abstraction to encapsulate manipulation with TrapData via 'iterator'
javap/src/main/java/org/apidesign/javap/MethodData.java
javap/src/main/java/org/apidesign/javap/TrapData.java
javap/src/main/java/org/apidesign/javap/TrapDataIterator.java
javap/src/main/java/org/apidesign/javap/Vector.java
vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
     1.1 --- a/javap/src/main/java/org/apidesign/javap/MethodData.java	Sat Dec 08 10:32:04 2012 +0100
     1.2 +++ b/javap/src/main/java/org/apidesign/javap/MethodData.java	Sun Dec 09 16:16:19 2012 +0100
     1.3 @@ -353,10 +353,10 @@
     1.4      /**
     1.5       * Return exception table in code attributre.
     1.6       */
     1.7 -    public Vector getexception_table(){
     1.8 -        return exception_table;
     1.9 +    public TrapDataIterator getTrapDataIterator(){
    1.10 +        return new TrapDataIterator(exception_table);
    1.11      }
    1.12 -
    1.13 +    
    1.14  
    1.15      /**
    1.16       * Return method attributes.
     2.1 --- a/javap/src/main/java/org/apidesign/javap/TrapData.java	Sat Dec 08 10:32:04 2012 +0100
     2.2 +++ b/javap/src/main/java/org/apidesign/javap/TrapData.java	Sun Dec 09 16:16:19 2012 +0100
     2.3 @@ -26,7 +26,6 @@
     2.4  
     2.5  package org.apidesign.javap;
     2.6  
     2.7 -import java.util.*;
     2.8  import java.io.*;
     2.9  
    2.10  /**
    2.11 @@ -34,7 +33,7 @@
    2.12   *
    2.13   * @author  Sucheta Dambalkar (Adopted code from jdis)
    2.14   */
    2.15 -public class TrapData {
    2.16 +public final class TrapData {
    2.17    public final short start_pc;
    2.18    public final short end_pc;
    2.19    public final short handler_pc;
    2.20 @@ -45,7 +44,7 @@
    2.21      /**
    2.22       * Read and store exception table data in code attribute.
    2.23       */
    2.24 -    public TrapData(DataInputStream in, int num) throws IOException {
    2.25 +    TrapData(DataInputStream in, int num) throws IOException {
    2.26          this.num=num;
    2.27          start_pc = in.readShort();
    2.28          end_pc=in.readShort();
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/javap/src/main/java/org/apidesign/javap/TrapDataIterator.java	Sun Dec 09 16:16:19 2012 +0100
     3.3 @@ -0,0 +1,42 @@
     3.4 +/*
     3.5 + * To change this template, choose Tools | Templates
     3.6 + * and open the template in the editor.
     3.7 + */
     3.8 +package org.apidesign.javap;
     3.9 +
    3.10 +/**
    3.11 + *
    3.12 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.13 + */
    3.14 +public final class TrapDataIterator {
    3.15 +    private final java.util.Map<Short, TrapData> exStart = new java.util.HashMap<Short, TrapData>();
    3.16 +    private final java.util.Map<Short, TrapData> exStop = new java.util.HashMap<Short, TrapData>();
    3.17 +    private final java.util.Deque<TrapData> current = new java.util.ArrayDeque<TrapData>();
    3.18 +    
    3.19 +    TrapDataIterator(Vector exceptionTable) {
    3.20 +        for (int i=0 ; i < exceptionTable.size(); i++) {
    3.21 +            final TrapData td = (TrapData)exceptionTable.elementAt(i);
    3.22 +            exStart.put(td.start_pc, td);
    3.23 +            exStop.put(td.end_pc, td);
    3.24 +        }
    3.25 +    }
    3.26 +
    3.27 +    public void advanceTo(int i) {
    3.28 +        TrapData e = exStart.get((short) i);
    3.29 +        if (e != null) {
    3.30 +            current.addFirst(e);
    3.31 +        }
    3.32 +        e = exStop.get((short) i);
    3.33 +        if (e != null) {
    3.34 +            current.remove(e);
    3.35 +        }
    3.36 +    }
    3.37 +
    3.38 +    public boolean useTry() {
    3.39 +        return !current.isEmpty();
    3.40 +    }
    3.41 +
    3.42 +    public TrapData[] current() {
    3.43 +        return current.toArray(new TrapData[0]);
    3.44 +    }
    3.45 +}
     4.1 --- a/javap/src/main/java/org/apidesign/javap/Vector.java	Sat Dec 08 10:32:04 2012 +0100
     4.2 +++ b/javap/src/main/java/org/apidesign/javap/Vector.java	Sun Dec 09 16:16:19 2012 +0100
     4.3 @@ -8,7 +8,7 @@
     4.4   *
     4.5   * @author Jaroslav Tulach <jtulach@netbeans.org>
     4.6   */
     4.7 -public final class Vector {
     4.8 +final class Vector {
     4.9      private Object[] arr;
    4.10      
    4.11      Vector() {
    4.12 @@ -27,7 +27,7 @@
    4.13          setElementAt(obj, s);
    4.14      }
    4.15  
    4.16 -    public int size() {
    4.17 +    int size() {
    4.18          return arr == null ? 0 : arr.length;
    4.19      }
    4.20  
    4.21 @@ -41,7 +41,7 @@
    4.22          }
    4.23      }
    4.24  
    4.25 -    public Object elementAt(int index) {
    4.26 +    Object elementAt(int index) {
    4.27          return arr[index];
    4.28      }
    4.29  
     5.1 --- a/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sat Dec 08 10:32:04 2012 +0100
     5.2 +++ b/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Sun Dec 09 16:16:19 2012 +0100
     5.3 @@ -25,7 +25,7 @@
     5.4  import org.apidesign.javap.MethodData;
     5.5  import static org.apidesign.javap.RuntimeConstants.*;
     5.6  import org.apidesign.javap.TrapData;
     5.7 -import org.apidesign.javap.Vector;
     5.8 +import org.apidesign.javap.TrapDataIterator;
     5.9  
    5.10  /** Translator of the code inside class files to JavaScript.
    5.11   *
    5.12 @@ -217,7 +217,7 @@
    5.13                  out.append("arg").append(String.valueOf(i)).append(";\n");
    5.14              }
    5.15              out.append("  var s = new Array();\n");
    5.16 -            produceCode(code, m.getexception_table());
    5.17 +            produceCode(m);
    5.18          } else {
    5.19              out.append("  throw 'no code found for ").append(m.getInternalSig()).append("';\n");
    5.20          }
    5.21 @@ -253,7 +253,7 @@
    5.22                  out.append("arg").append(String.valueOf(i + 1)).append(";\n");
    5.23              }
    5.24              out.append(";\n  var s = new Array();\n");
    5.25 -            produceCode(code, m.getexception_table());
    5.26 +            produceCode(m);
    5.27          } else {
    5.28              out.append("  throw 'no code found for ").append(m.getInternalSig()).append("';\n");
    5.29          }
    5.30 @@ -261,33 +261,18 @@
    5.31          return mn;
    5.32      }
    5.33  
    5.34 -    private void produceCode(byte[] byteCodes, Vector exceptionTable) throws IOException {
    5.35 -
    5.36 -        final java.util.Map<Short, TrapData> exStart = new java.util.HashMap<Short, TrapData>();
    5.37 -        final java.util.Map<Short, TrapData> exStop = new java.util.HashMap<Short, TrapData>();
    5.38 -        for (int i=0 ; i < exceptionTable.size(); i++) {
    5.39 -            final TrapData td = (TrapData)exceptionTable.elementAt(i);
    5.40 -            exStart.put(td.start_pc, td);
    5.41 -            exStop.put(td.end_pc, td);
    5.42 -        }
    5.43 -        final java.util.Deque<TrapData> current = new java.util.ArrayDeque<TrapData>();
    5.44 +    private void produceCode(MethodData md) throws IOException {
    5.45 +        byte[] byteCodes = md.getCode();
    5.46 +        
    5.47 +        TrapDataIterator trap = md.getTrapDataIterator();
    5.48          out.append("\n  var gt = 0;\n  for(;;) switch(gt) {\n");
    5.49  
    5.50          for (int i = 0; i < byteCodes.length; i++) {
    5.51  
    5.52 -            {
    5.53 -                TrapData e = exStart.get((short)i);
    5.54 -                if (e != null) {
    5.55 -                    current.addFirst(e);
    5.56 -                }
    5.57 -                e = exStop.get((short)i);
    5.58 -                if (e != null) {
    5.59 -                    current.remove(e);
    5.60 -                }
    5.61 -            }
    5.62 +            trap.advanceTo(i);
    5.63              int prev = i;
    5.64              out.append("    case " + i).append(": ");            
    5.65 -            if (!current.isEmpty()) {
    5.66 +            if (trap.useTry()) {
    5.67                  out.append("try {");
    5.68              }
    5.69              final int c = readByte(byteCodes, i);
    5.70 @@ -810,9 +795,9 @@
    5.71                  }
    5.72                      
    5.73              }
    5.74 -            if (!current.isEmpty()) {
    5.75 +            if (trap.useTry()) {
    5.76                  out.append("} catch (e) {");
    5.77 -                for (TrapData e : current) {
    5.78 +                for (TrapData e : trap.current()) {
    5.79                      if (e.catch_cpx != 0) { //not finally
    5.80                          final String classInternalName = jc.getClassName(e.catch_cpx);
    5.81                          addReference(classInternalName);