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
023import org.apache.bcel.util.Args;
024
025/**
026 * This class represents a (PC offset, line number) pair, i.e., a line number in the source that corresponds to a
027 * relative address in the byte code. This is used for debugging purposes.
028 *
029 * @see LineNumberTable
030 */
031public final class LineNumber implements Cloneable, Node {
032
033    static final LineNumber[] EMPTY_ARRAY = {};
034
035    /** Program Counter (PC) corresponds to line */
036    private int startPc;
037
038    /** Number in source file */
039    private int lineNumber;
040
041    /**
042     * Constructs object from file stream.
043     *
044     * @param file Input stream
045     * @throws IOException if an I/O Exception occurs in readUnsignedShort
046     */
047    LineNumber(final DataInput file) throws IOException {
048        this(file.readUnsignedShort(), file.readUnsignedShort());
049    }
050
051    /**
052     * @param startPc Program Counter (PC) corresponds to
053     * @param lineNumber line number in source file
054     */
055    public LineNumber(final int startPc, final int lineNumber) {
056        this.startPc = Args.requireU2(startPc, "startPc");
057        this.lineNumber = Args.requireU2(lineNumber, "lineNumber");
058    }
059
060    /**
061     * Initialize from another object.
062     *
063     * @param c the object to copy
064     */
065    public LineNumber(final LineNumber c) {
066        this(c.getStartPC(), c.getLineNumber());
067    }
068
069    /**
070     * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
071     * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
072     *
073     * @param v Visitor object
074     */
075    @Override
076    public void accept(final Visitor v) {
077        v.visitLineNumber(this);
078    }
079
080    /**
081     * @return deep copy of this object
082     */
083    public LineNumber copy() {
084        try {
085            return (LineNumber) clone();
086        } catch (final CloneNotSupportedException e) {
087            // TODO should this throw?
088        }
089        return null;
090    }
091
092    /**
093     * Dump line number/pc pair to file stream in binary format.
094     *
095     * @param file Output file stream
096     * @throws IOException if an I/O Exception occurs in writeShort
097     */
098    public void dump(final DataOutputStream file) throws IOException {
099        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}