1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  package org.apache.bcel.generic;
18  
19  import org.apache.bcel.classfile.LineNumber;
20  
21  /*** 
22   * This class represents a line number within a method, i.e., give an instruction
23   * a line number corresponding to the source code line.
24   *
25   * @version $Id: LineNumberGen.java 386056 2006-03-15 11:31:56Z tcurdt $
26   * @author  <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
27   * @see     LineNumber
28   * @see     MethodGen
29   */
30  public class LineNumberGen implements InstructionTargeter, Cloneable, java.io.Serializable {
31  
32      private InstructionHandle ih;
33      private int src_line;
34  
35  
36      /***
37       * Create a line number.
38       *
39       * @param ih instruction handle to reference
40       */
41      public LineNumberGen(InstructionHandle ih, int src_line) {
42          setInstruction(ih);
43          setSourceLine(src_line);
44      }
45  
46  
47      /***
48       * @return true, if ih is target of this line number
49       */
50      public boolean containsTarget( InstructionHandle ih ) {
51          return this.ih == ih;
52      }
53  
54  
55      /***
56       * @param old_ih old target
57       * @param new_ih new target
58       */
59      public void updateTarget( InstructionHandle old_ih, InstructionHandle new_ih ) {
60          if (old_ih != ih) {
61              throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}");
62          } else {
63              setInstruction(new_ih);
64          }
65      }
66  
67  
68      /***
69       * Get LineNumber attribute .
70       *
71       * This relies on that the instruction list has already been dumped to byte code or
72       * or that the `setPositions' methods has been called for the instruction list.
73       */
74      public LineNumber getLineNumber() {
75          return new LineNumber(ih.getPosition(), src_line);
76      }
77  
78  
79      public void setInstruction( InstructionHandle ih ) {
80          BranchInstruction.notifyTarget(this.ih, ih, this);
81          this.ih = ih;
82      }
83  
84  
85      public Object clone() {
86          try {
87              return super.clone();
88          } catch (CloneNotSupportedException e) {
89              System.err.println(e);
90              return null;
91          }
92      }
93  
94  
95      public InstructionHandle getInstruction() {
96          return ih;
97      }
98  
99  
100     public void setSourceLine( int src_line ) {
101         this.src_line = src_line;
102     }
103 
104 
105     public int getSourceLine() {
106         return src_line;
107     }
108 }