View Javadoc

1   /*
2    * Copyright  2000-2004 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  package org.apache.bcel.classfile;
18  
19  import java.io.DataInputStream;
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.io.Serializable;
23  
24  /***
25   * This class represents a (PC offset, line number) pair, i.e., a line number in
26   * the source that corresponds to a relative address in the byte code. This
27   * is used for debugging purposes.
28   *
29   * @version $Id: LineNumber.java 386056 2006-03-15 11:31:56Z tcurdt $
30   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
31   * @see     LineNumberTable
32   */
33  public final class LineNumber implements Cloneable, Node, Serializable {
34  
35      private int start_pc; // Program Counter (PC) corresponds to line
36      private int line_number; // number in source file
37  
38  
39      /***
40       * Initialize from another object.
41       */
42      public LineNumber(LineNumber c) {
43          this(c.getStartPC(), c.getLineNumber());
44      }
45  
46  
47      /***
48       * Construct object from file stream.
49       * @param file Input stream
50       * @throws IOException
51       */
52      LineNumber(DataInputStream file) throws IOException {
53          this(file.readUnsignedShort(), file.readUnsignedShort());
54      }
55  
56  
57      /***
58       * @param start_pc Program Counter (PC) corresponds to
59       * @param line_number line number in source file
60       */
61      public LineNumber(int start_pc, int line_number) {
62          this.start_pc = start_pc;
63          this.line_number = line_number;
64      }
65  
66  
67      /***
68       * Called by objects that are traversing the nodes of the tree implicitely
69       * defined by the contents of a Java class. I.e., the hierarchy of methods,
70       * fields, attributes, etc. spawns a tree of objects.
71       *
72       * @param v Visitor object
73       */
74      public void accept( Visitor v ) {
75          v.visitLineNumber(this);
76      }
77  
78  
79      /***
80       * Dump line number/pc pair to file stream in binary format.
81       *
82       * @param file Output file stream
83       * @throws IOException
84       */
85      public final void dump( DataOutputStream file ) throws IOException {
86          file.writeShort(start_pc);
87          file.writeShort(line_number);
88      }
89  
90  
91      /***
92       * @return Corresponding source line
93       */
94      public final int getLineNumber() {
95          return line_number;
96      }
97  
98  
99      /***
100      * @return PC in code
101      */
102     public final int getStartPC() {
103         return start_pc;
104     }
105 
106 
107     /***
108      * @param line_number the source line number
109      */
110     public final void setLineNumber( int line_number ) {
111         this.line_number = line_number;
112     }
113 
114 
115     /***
116      * @param start_pc the pc for this line number
117      */
118     public final void setStartPC( int start_pc ) {
119         this.start_pc = start_pc;
120     }
121 
122 
123     /***
124      * @return String representation
125      */
126     public final String toString() {
127         return "LineNumber(" + start_pc + ", " + line_number + ")";
128     }
129 
130 
131     /***
132      * @return deep copy of this object
133      */
134     public LineNumber copy() {
135         try {
136             return (LineNumber) clone();
137         } catch (CloneNotSupportedException e) {
138         }
139         return null;
140     }
141 }