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.generic;
  23 
  24 import com.sun.org.apache.bcel.internal.classfile.LineNumber;
  25 
  26 /**
  27  * This class represents a line number within a method, i.e., give an instruction
  28  * a line number corresponding to the source code line.
  29  *
  30  * @see     LineNumber
  31  * @see     MethodGen
  32  */
  33 public class LineNumberGen implements InstructionTargeter, Cloneable {
  34 
  35     private InstructionHandle ih;
  36     private int src_line;
  37 
  38 
  39     /**
  40      * Create a line number.
  41      *
  42      * @param ih instruction handle to reference
  43      */
  44     public LineNumberGen(final InstructionHandle ih, final int src_line) {
  45         setInstruction(ih);
  46         setSourceLine(src_line);
  47     }
  48 
  49 
  50     /**
  51      * @return true, if ih is target of this line number
  52      */
  53     @Override
  54     public boolean containsTarget( final InstructionHandle ih ) {
  55         return this.ih == ih;
  56     }
  57 
  58 
  59     /**
  60      * @param old_ih old target
  61      * @param new_ih new target
  62      */
  63     @Override
  64     public void updateTarget( final InstructionHandle old_ih, final InstructionHandle new_ih ) {
  65         if (old_ih != ih) {
  66             throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}");
  67         }
  68         setInstruction(new_ih);
  69     }
  70 
  71 
  72     /**
  73      * Get LineNumber attribute .
  74      *
  75      * This relies on that the instruction list has already been dumped to byte code or
  76      * or that the `setPositions' methods has been called for the instruction list.
  77      */
  78     public LineNumber getLineNumber() {
  79         return new LineNumber(ih.getPosition(), src_line);
  80     }
  81 
  82 
  83     public void setInstruction( final InstructionHandle ih ) { // TODO could be package-protected?
  84         if (ih == null) {
  85             throw new NullPointerException("InstructionHandle may not be null");
  86         }
  87         BranchInstruction.notifyTarget(this.ih, ih, this);
  88         this.ih = ih;
  89     }
  90 
  91 
  92     @Override
  93     public Object clone() {
  94         try {
  95             return super.clone();
  96         } catch (final CloneNotSupportedException e) {
  97             throw new Error("Clone Not Supported"); // never happens
  98         }
  99     }
 100 
 101 
 102     public InstructionHandle getInstruction() {
 103         return ih;
 104     }
 105 
 106 
 107     public void setSourceLine( final int src_line ) { // TODO could be package-protected?
 108         this.src_line = src_line;
 109     }
 110 
 111 
 112     public int getSourceLine() {
 113         return src_line;
 114     }
 115 }