View Javadoc

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