1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Licensed to the Apache Software Foundation (ASF) under one or more
   7  * contributor license agreements.  See the NOTICE file distributed with
   8  * this work for additional information regarding copyright ownership.
   9  * The ASF licenses this file to You under the Apache License, Version 2.0
  10  * (the "License"); you may not use this file except in compliance with
  11  * the License.  You may obtain a copy of the License at
  12  *
  13  *      http://www.apache.org/licenses/LICENSE-2.0
  14  *
  15  * Unless required by applicable law or agreed to in writing, software
  16  * distributed under the License is distributed on an "AS IS" BASIS,
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18  * See the License for the specific language governing permissions and
  19  * limitations under the License.
  20  */
  21 
  22 package com.sun.org.apache.bcel.internal.classfile;
  23 
  24 import java.io.DataInput;
  25 import java.io.DataOutputStream;
  26 import java.io.IOException;
  27 
  28 /**
  29  * This class represents a (PC offset, line number) pair, i.e., a line number in
  30  * the source that corresponds to a relative address in the byte code. This
  31  * is used for debugging purposes.
  32  *
  33  * @version $Id: LineNumber.java 1749603 2016-06-21 20:50:19Z ggregory $
  34  * @see     LineNumberTable
  35  */
  36 public final class LineNumber implements Cloneable, Node {
  37 
  38     /** Program Counter (PC) corresponds to line */
  39     private short start_pc;
  40 
  41     /** number in source file */
  42     private short line_number;
  43 
  44     /**
  45      * Initialize from another object.
  46      *
  47      * @param c the object to copy
  48      */
  49     public LineNumber(final LineNumber c) {
  50         this(c.getStartPC(), c.getLineNumber());
  51     }
  52 
  53 
  54     /**
  55      * Construct object from file stream.
  56      *
  57      * @param file Input stream
  58      * @throws IOEXception if an I/O Exception occurs in readUnsignedShort
  59      */
  60     LineNumber(final DataInput file) throws IOException {
  61         this(file.readUnsignedShort(), file.readUnsignedShort());
  62     }
  63 
  64 
  65     /**
  66      * @param start_pc Program Counter (PC) corresponds to
  67      * @param line_number line number in source file
  68      */
  69     public LineNumber(final int start_pc, final int line_number) {
  70         this.start_pc = (short) start_pc;
  71         this.line_number = (short)line_number;
  72     }
  73 
  74 
  75     /**
  76      * Called by objects that are traversing the nodes of the tree implicitely
  77      * defined by the contents of a Java class. I.e., the hierarchy of methods,
  78      * fields, attributes, etc. spawns a tree of objects.
  79      *
  80      * @param v Visitor object
  81      */
  82     @Override
  83     public void accept( final Visitor v ) {
  84         v.visitLineNumber(this);
  85     }
  86 
  87 
  88     /**
  89      * Dump line number/pc pair to file stream in binary format.
  90      *
  91      * @param file Output file stream
  92      * @throws IOEXception if an I/O Exception occurs in writeShort
  93      */
  94     public final void dump( final DataOutputStream file ) throws IOException {
  95         file.writeShort(start_pc);
  96         file.writeShort(line_number);
  97     }
  98 
  99 
 100     /**
 101      * @return Corresponding source line
 102      */
 103     public final int getLineNumber() {
 104         return 0xffff & line_number;
 105     }
 106 
 107 
 108     /**
 109      * @return PC in code
 110      */
 111     public final int getStartPC() {
 112         return  0xffff & start_pc;
 113     }
 114 
 115 
 116     /**
 117      * @param line_number the source line number
 118      */
 119     public final void setLineNumber( final int line_number ) {
 120         this.line_number = (short) line_number;
 121     }
 122 
 123 
 124     /**
 125      * @param start_pc the pc for this line number
 126      */
 127     public final void setStartPC( final int start_pc ) {
 128         this.start_pc = (short) start_pc;
 129     }
 130 
 131 
 132     /**
 133      * @return String representation
 134      */
 135     @Override
 136     public final String toString() {
 137         return "LineNumber(" + start_pc + ", " + line_number + ")";
 138     }
 139 
 140 
 141     /**
 142      * @return deep copy of this object
 143      */
 144     public LineNumber copy() {
 145         try {
 146             return (LineNumber) clone();
 147         } catch (final CloneNotSupportedException e) {
 148             // TODO should this throw?
 149         }
 150         return null;
 151     }
 152 }