View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  import java.io.*;
21  import java.util.Stack;
22  
23  /***
24   * LineNumberTableAttribute represents a line number table attribute
25   * within a CodeAttribute within a class file
26   */
27  public class LineNumberTableAttribute extends ClassAttribute {
28      /* The expected attribute name */
29      public final static String expectedAttrName = "LineNumberTable";
30  
31      /* The line numbers */
32      private short lineNumbers[];
33  
34      /* The corresponding instructions */
35      private InsnTarget targets[];
36  
37      /* public accessors */
38  
39      /***
40       * Constructor
41       */
42      public LineNumberTableAttribute(
43  	ConstUtf8 nameAttr, short lineNums[], InsnTarget targets[]) {
44          super(nameAttr);
45          lineNumbers = lineNums;
46          this.targets = targets;
47      }
48  
49      /***
50       * Compares this instance with another for structural equality.
51       */
52      //@olsen: added method
53      public boolean isEqual(Stack msg, Object obj) {
54          if (!(obj instanceof LineNumberTableAttribute)) {
55              msg.push("obj/obj.getClass() = "
56                       + (obj == null ? null : obj.getClass()));
57              msg.push("this.getClass() = "
58                       + this.getClass());
59              return false;
60          }
61          LineNumberTableAttribute other = (LineNumberTableAttribute)obj;
62  
63          if (!super.isEqual(msg, other)) {
64              return false;
65          }
66  
67          // intentionally ingore any linenumber differences
68          return true;
69      }
70  
71      /* package local methods *//package-summary/html">class="comment"> package local methods *//package-summary.html">/* package local methods *//package-summary.html">class="comment"> package local methods */
72  
73      static LineNumberTableAttribute read(
74  	ConstUtf8 attrName, DataInputStream data, CodeEnv env)
75          throws IOException {
76          int nLnums = data.readUnsignedShort();
77          short lineNums[] = new short[nLnums];
78          InsnTarget targs[] = new InsnTarget[nLnums];
79          for (int i=0; i<nLnums; i++) {
80              targs[i] = env.getTarget(data.readShort());
81              lineNums[i] = data.readShort();
82          }
83          return  new LineNumberTableAttribute(attrName, lineNums, targs);
84      }
85  
86      void write(DataOutputStream out) throws IOException {
87          out.writeShort(attrName().getIndex());
88          int nlines = lineNumbers.length;
89          out.writeInt(2+4*nlines);
90          out.writeShort(nlines);
91          for (int i=0; i<nlines; i++) {
92              out.writeShort(targets[i].offset());
93              out.writeShort(lineNumbers[i]);
94          }
95      }
96  
97      void print(PrintStream out, int indent) {
98          ClassPrint.spaces(out, indent);
99          out.println("Line Numbers: ");
100         for (int i=0; i<lineNumbers.length; i++) {
101             ClassPrint.spaces(out, indent+2);
102             out.println(Integer.toString(lineNumbers[i]) + " @ " +
103                         Integer.toString(targets[i].offset()));
104         }
105     }
106 }
107