View Javadoc

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