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