View Javadoc

1   // jq_BytecodeMap.java, created Mon Feb  5 23:23:20 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Class;
5   
6   import jwutil.util.Assert;
7   
8   /***
9    * This class implements a mapping from code offsets to bytecode indices.
10   *
11   * @author  John Whaley <jwhaley@alum.mit.edu>
12   * @version $Id: jq_BytecodeMap.java 1931 2004-09-22 22:17:47Z joewhaley $
13   */
14  public class jq_BytecodeMap {
15  
16      /*** Stores the code offsets. */
17      private final int[] offset;
18      /*** Stores the bytecode indices. */
19      private final int[] bytecode_index;
20      
21      /*** Constructs a new bytecode map, using the given code offset and bytecode index array.
22       *  The two arrays are co-indexed.  Each entry in the code offset array corresponds
23       *  to an inclusive start offset of the instructions corresponding to the bytecode index
24       *  in the co-indexed bytecode array.
25       *  The length of the two arrays must be equal.
26       *
27       * @param offset  code offset array
28       * @param bytecode_index  bytecode index array
29       */
30      public jq_BytecodeMap(int[] offset, int[] bytecode_index) {
31          Assert._assert(offset.length == bytecode_index.length);
32          this.offset = offset;
33          this.bytecode_index = bytecode_index;
34      }
35      
36      /*** Returns the bytecode index corresponding to the given code offset, or -1 if the
37       *  offset is out of range.
38       * @param off  code offset to match
39       * @return  bytecode index for the code offset, or -1
40       */
41      public int getBytecodeIndex(int off) {
42          // todo: binary search
43          for (int i=offset.length-1; i>=0; --i) {
44              if (off > offset[i]) return bytecode_index[i];
45          }
46          return -1;
47      }
48  }