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 *
017 */
018package org.apache.bcel.classfile;
019
020import java.io.DataInput;
021import java.io.DataOutputStream;
022import java.io.IOException;
023
024/**
025 * This class represents a (PC offset, line number) pair, i.e., a line number in
026 * the source that corresponds to a relative address in the byte code. This
027 * is used for debugging purposes.
028 *
029 * @see     LineNumberTable
030 */
031public final class LineNumber implements Cloneable, Node {
032
033    /** Program Counter (PC) corresponds to line */
034    private short start_pc;
035
036    /** number in source file */
037    private short line_number;
038
039    /**
040     * Initialize from another object.
041     *
042     * @param c the object to copy
043     */
044    public LineNumber(final LineNumber c) {
045        this(c.getStartPC(), c.getLineNumber());
046    }
047
048
049    /**
050     * Construct object from file stream.
051     *
052     * @param file Input stream
053     * @throws IOException if an I/O Exception occurs in readUnsignedShort
054     */
055    LineNumber(final DataInput file) throws IOException {
056        this(file.readUnsignedShort(), file.readUnsignedShort());
057    }
058
059
060    /**
061     * @param start_pc Program Counter (PC) corresponds to
062     * @param line_number line number in source file
063     */
064    public LineNumber(final int start_pc, final int line_number) {
065        this.start_pc = (short) start_pc;
066        this.line_number = (short)line_number;
067    }
068
069
070    /**
071     * Called by objects that are traversing the nodes of the tree implicitely
072     * defined by the contents of a Java class. I.e., the hierarchy of methods,
073     * 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    /**
084     * Dump line number/pc pair to file stream in binary format.
085     *
086     * @param file Output file stream
087     * @throws IOException if an I/O Exception occurs in writeShort
088     */
089    public void dump( final DataOutputStream file ) throws IOException {
090        file.writeShort(start_pc);
091        file.writeShort(line_number);
092    }
093
094
095    /**
096     * @return Corresponding source line
097     */
098    public int getLineNumber() {
099        return 0xffff & line_number;
100    }
101
102
103    /**
104     * @return PC in code
105     */
106    public int getStartPC() {
107        return  0xffff & start_pc;
108    }
109
110
111    /**
112     * @param line_number the source line number
113     */
114    public void setLineNumber( final int line_number ) {
115        this.line_number = (short) line_number;
116    }
117
118
119    /**
120     * @param start_pc the pc for this line number
121     */
122    public void setStartPC( final int start_pc ) {
123        this.start_pc = (short) start_pc;
124    }
125
126
127    /**
128     * @return String representation
129     */
130    @Override
131    public String toString() {
132        return "LineNumber(" + start_pc + ", " + line_number + ")";
133    }
134
135
136    /**
137     * @return deep copy of this object
138     */
139    public LineNumber copy() {
140        try {
141            return (LineNumber) clone();
142        } catch (final CloneNotSupportedException e) {
143            // TODO should this throw?
144        }
145        return null;
146    }
147}