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