001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.classfile; 020 021import java.io.DataInput; 022import java.io.DataOutputStream; 023import java.io.IOException; 024 025import org.apache.bcel.util.Args; 026 027/** 028 * This class represents a (PC offset, line number) pair, i.e., a line number in the source that corresponds to a 029 * relative address in the byte code. This is used for debugging purposes. 030 * 031 * @see LineNumberTable 032 */ 033public final class LineNumber implements Cloneable, Node { 034 035 static final LineNumber[] EMPTY_ARRAY = {}; 036 037 /** Program Counter (PC) corresponds to line */ 038 private int startPc; 039 040 /** Number in source file */ 041 private int lineNumber; 042 043 /** 044 * Constructs object from file stream. 045 * 046 * @param file Input stream 047 * @throws IOException if an I/O Exception occurs in readUnsignedShort 048 */ 049 LineNumber(final DataInput file) throws IOException { 050 this(file.readUnsignedShort(), file.readUnsignedShort()); 051 } 052 053 /** 054 * @param startPc Program Counter (PC) corresponds to 055 * @param lineNumber line number in source file 056 */ 057 public LineNumber(final int startPc, final int lineNumber) { 058 this.startPc = Args.requireU2(startPc, "startPc"); 059 this.lineNumber = Args.requireU2(lineNumber, "lineNumber"); 060 } 061 062 /** 063 * Initialize from another object. 064 * 065 * @param c the object to copy 066 */ 067 public LineNumber(final LineNumber c) { 068 this(c.getStartPC(), c.getLineNumber()); 069 } 070 071 /** 072 * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class. 073 * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects. 074 * 075 * @param v Visitor object 076 */ 077 @Override 078 public void accept(final Visitor v) { 079 v.visitLineNumber(this); 080 } 081 082 /** 083 * @return deep copy of this object 084 */ 085 public LineNumber copy() { 086 try { 087 return (LineNumber) clone(); 088 } catch (final CloneNotSupportedException e) { 089 // TODO should this throw? 090 } 091 return null; 092 } 093 094 /** 095 * Dump line number/pc pair to file stream in binary format. 096 * 097 * @param file Output file stream 098 * @throws IOException if an I/O Exception occurs in writeShort 099 */ 100 public void dump(final DataOutputStream file) throws IOException { 101 file.writeShort(startPc); 102 file.writeShort(lineNumber); 103 } 104 105 /** 106 * @return Corresponding source line 107 */ 108 public int getLineNumber() { 109 return lineNumber & 0xffff; 110 } 111 112 /** 113 * @return PC in code 114 */ 115 public int getStartPC() { 116 return startPc & 0xffff; 117 } 118 119 /** 120 * @param lineNumber the source line number 121 */ 122 public void setLineNumber(final int lineNumber) { 123 this.lineNumber = (short) lineNumber; 124 } 125 126 /** 127 * @param startPc the pc for this line number 128 */ 129 public void setStartPC(final int startPc) { 130 this.startPc = (short) startPc; 131 } 132 133 /** 134 * @return String representation 135 */ 136 @Override 137 public String toString() { 138 return "LineNumber(" + getStartPC() + ", " + getLineNumber() + ")"; 139 } 140}