LineNumber.java

  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. import java.io.DataInput;
  19. import java.io.DataOutputStream;
  20. import java.io.IOException;

  21. import org.apache.bcel.util.Args;

  22. /**
  23.  * This class represents a (PC offset, line number) pair, i.e., a line number in the source that corresponds to a
  24.  * relative address in the byte code. This is used for debugging purposes.
  25.  *
  26.  * @see LineNumberTable
  27.  */
  28. public final class LineNumber implements Cloneable, Node {

  29.     static final LineNumber[] EMPTY_ARRAY = {};

  30.     /** Program Counter (PC) corresponds to line */
  31.     private int startPc;

  32.     /** Number in source file */
  33.     private int lineNumber;

  34.     /**
  35.      * Constructs object from file stream.
  36.      *
  37.      * @param file Input stream
  38.      * @throws IOException if an I/O Exception occurs in readUnsignedShort
  39.      */
  40.     LineNumber(final DataInput file) throws IOException {
  41.         this(file.readUnsignedShort(), file.readUnsignedShort());
  42.     }

  43.     /**
  44.      * @param startPc Program Counter (PC) corresponds to
  45.      * @param lineNumber line number in source file
  46.      */
  47.     public LineNumber(final int startPc, final int lineNumber) {
  48.         this.startPc = Args.requireU2(startPc, "startPc");
  49.         this.lineNumber = Args.requireU2(lineNumber, "lineNumber");
  50.     }

  51.     /**
  52.      * Initialize from another object.
  53.      *
  54.      * @param c the object to copy
  55.      */
  56.     public LineNumber(final LineNumber c) {
  57.         this(c.getStartPC(), c.getLineNumber());
  58.     }

  59.     /**
  60.      * Called by objects that are traversing the nodes of the tree implicitly defined by the contents of a Java class.
  61.      * I.e., the hierarchy of methods, fields, attributes, etc. spawns a tree of objects.
  62.      *
  63.      * @param v Visitor object
  64.      */
  65.     @Override
  66.     public void accept(final Visitor v) {
  67.         v.visitLineNumber(this);
  68.     }

  69.     /**
  70.      * @return deep copy of this object
  71.      */
  72.     public LineNumber copy() {
  73.         try {
  74.             return (LineNumber) clone();
  75.         } catch (final CloneNotSupportedException e) {
  76.             // TODO should this throw?
  77.         }
  78.         return null;
  79.     }

  80.     /**
  81.      * Dump line number/pc pair to file stream in binary format.
  82.      *
  83.      * @param file Output file stream
  84.      * @throws IOException if an I/O Exception occurs in writeShort
  85.      */
  86.     public void dump(final DataOutputStream file) throws IOException {
  87.         file.writeShort(startPc);
  88.         file.writeShort(lineNumber);
  89.     }

  90.     /**
  91.      * @return Corresponding source line
  92.      */
  93.     public int getLineNumber() {
  94.         return lineNumber & 0xffff;
  95.     }

  96.     /**
  97.      * @return PC in code
  98.      */
  99.     public int getStartPC() {
  100.         return startPc & 0xffff;
  101.     }

  102.     /**
  103.      * @param lineNumber the source line number
  104.      */
  105.     public void setLineNumber(final int lineNumber) {
  106.         this.lineNumber = (short) lineNumber;
  107.     }

  108.     /**
  109.      * @param startPc the pc for this line number
  110.      */
  111.     public void setStartPC(final int startPc) {
  112.         this.startPc = (short) startPc;
  113.     }

  114.     /**
  115.      * @return String representation
  116.      */
  117.     @Override
  118.     public String toString() {
  119.         return "LineNumber(" + getStartPC() + ", " + getLineNumber() + ")";
  120.     }
  121. }