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.html 1018313 2017-09-18 09:03:04Z britter $
27   * @see     LineNumber
28   * @see     MethodGen
29   */
30  public class LineNumberGen implements InstructionTargeter, Cloneable {
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(final InstructionHandle ih, final 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      @Override
51      public boolean containsTarget( final InstructionHandle ih ) {
52          return this.ih == ih;
53      }
54  
55  
56      /**
57       * @param old_ih old target
58       * @param new_ih new target
59       */
60      @Override
61      public void updateTarget( final InstructionHandle old_ih, final InstructionHandle new_ih ) {
62          if (old_ih != ih) {
63              throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}");
64          }
65          setInstruction(new_ih);
66      }
67  
68  
69      /**
70       * Get LineNumber attribute .
71       *
72       * This relies on that the instruction list has already been dumped to byte code or
73       * or that the `setPositions' methods has been called for the instruction list.
74       */
75      public LineNumber getLineNumber() {
76          return new LineNumber(ih.getPosition(), src_line);
77      }
78  
79  
80      public void setInstruction( final InstructionHandle ih ) { // TODO could be package-protected?
81          if (ih == null) {
82              throw new NullPointerException("InstructionHandle may not be null");
83          }
84          BranchInstruction.notifyTarget(this.ih, ih, this);
85          this.ih = ih;
86      }
87  
88  
89      @Override
90      public Object clone() {
91          try {
92              return super.clone();
93          } catch (final CloneNotSupportedException e) {
94              throw new Error("Clone Not Supported"); // never happens
95          }
96      }
97  
98  
99      public InstructionHandle getInstruction() {
100         return ih;
101     }
102 
103 
104     public void setSourceLine( final int src_line ) { // TODO could be package-protected?
105         this.src_line = src_line;
106     }
107 
108 
109     public int getSourceLine() {
110         return src_line;
111     }
112 }