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  package org.apache.bcel.classfile;
19  
20  import java.io.DataInput;
21  import java.io.DataOutputStream;
22  import java.io.IOException;
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   * @see     LineNumberTable
30   */
31  public final class LineNumber implements Cloneable, Node {
32  
33      /** Program Counter (PC) corresponds to line */
34      private short startPc;
35  
36      /** number in source file */
37      private short lineNumber;
38  
39      /**
40       * Initialize from another object.
41       *
42       * @param c the object to copy
43       */
44      public LineNumber/LineNumber.html#LineNumber">LineNumber(final LineNumber c) {
45          this(c.getStartPC(), c.getLineNumber());
46      }
47  
48  
49      /**
50       * Construct object from file stream.
51       *
52       * @param file Input stream
53       * @throws IOException if an I/O Exception occurs in readUnsignedShort
54       */
55      LineNumber(final DataInput file) throws IOException {
56          this(file.readUnsignedShort(), file.readUnsignedShort());
57      }
58  
59  
60      /**
61       * @param startPc Program Counter (PC) corresponds to
62       * @param lineNumber line number in source file
63       */
64      public LineNumber(final int startPc, final int lineNumber) {
65          this.startPc = (short) startPc;
66          this.lineNumber = (short)lineNumber;
67      }
68  
69  
70      /**
71       * Called by objects that are traversing the nodes of the tree implicitely
72       * defined by the contents of a Java class. I.e., the hierarchy of methods,
73       * fields, attributes, etc. spawns a tree of objects.
74       *
75       * @param v Visitor object
76       */
77      @Override
78      public void accept( final Visitor v ) {
79          v.visitLineNumber(this);
80      }
81  
82  
83      /**
84       * Dump line number/pc pair to file stream in binary format.
85       *
86       * @param file Output file stream
87       * @throws IOException if an I/O Exception occurs in writeShort
88       */
89      public void dump( final DataOutputStream file ) throws IOException {
90          file.writeShort(startPc);
91          file.writeShort(lineNumber);
92      }
93  
94  
95      /**
96       * @return Corresponding source line
97       */
98      public int getLineNumber() {
99          return 0xffff & lineNumber;
100     }
101 
102 
103     /**
104      * @return PC in code
105      */
106     public int getStartPC() {
107         return  0xffff & startPc;
108     }
109 
110 
111     /**
112      * @param lineNumber the source line number
113      */
114     public void setLineNumber( final int lineNumber ) {
115         this.lineNumber = (short) lineNumber;
116     }
117 
118 
119     /**
120      * @param startPc the pc for this line number
121      */
122     public void setStartPC( final int startPc ) {
123         this.startPc = (short) startPc;
124     }
125 
126 
127     /**
128      * @return String representation
129      */
130     @Override
131     public String toString() {
132         return "LineNumber(" + startPc + ", " + lineNumber + ")";
133     }
134 
135 
136     /**
137      * @return deep copy of this object
138      */
139     public LineNumber copy() {
140         try {
141             return (LineNumber) clone();
142         } catch (final CloneNotSupportedException e) {
143             // TODO should this throw?
144         }
145         return null;
146     }
147 }