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