1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * https://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.bcel.generic;
20
21 import java.util.Objects;
22
23 import org.apache.bcel.classfile.LineNumber;
24
25 /**
26 * This class represents a line number within a method, i.e., give an instruction a line number corresponding to the
27 * source code line.
28 *
29 * @see LineNumber
30 * @see MethodGen
31 */
32 public class LineNumberGen implements InstructionTargeter, Cloneable {
33
34 static final LineNumberGen[] EMPTY_ARRAY = {};
35
36 private InstructionHandle ih;
37 private int srcLine;
38
39 /**
40 * Create a line number.
41 *
42 * @param ih instruction handle to reference
43 */
44 public LineNumberGen(final InstructionHandle ih, final int srcLine) {
45 setInstruction(ih);
46 setSourceLine(srcLine);
47 }
48
49 @Override
50 public Object clone() {
51 try {
52 return super.clone();
53 } catch (final CloneNotSupportedException e) {
54 throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
55 }
56 }
57
58 /**
59 * @return true, if ih is target of this line number
60 */
61 @Override
62 public boolean containsTarget(final InstructionHandle ih) {
63 return this.ih == ih;
64 }
65
66 public InstructionHandle getInstruction() {
67 return ih;
68 }
69
70 /**
71 * Gets LineNumber attribute.
72 *
73 * This relies on that the instruction list has already been dumped to byte code or that the 'setPositions' methods
74 * has been called for the instruction list.
75 */
76 public LineNumber getLineNumber() {
77 return new LineNumber(ih.getPosition(), srcLine);
78 }
79
80 public int getSourceLine() {
81 return srcLine;
82 }
83
84 public void setInstruction(final InstructionHandle instructionHandle) { // TODO could be package-protected?
85 Objects.requireNonNull(instructionHandle, "instructionHandle");
86 BranchInstruction.notifyTarget(this.ih, instructionHandle, this);
87 this.ih = instructionHandle;
88 }
89
90 public void setSourceLine(final int srcLine) { // TODO could be package-protected?
91 this.srcLine = srcLine;
92 }
93
94 /**
95 * @param oldIh old target
96 * @param newIh new target
97 */
98 @Override
99 public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
100 if (oldIh != ih) {
101 throw new ClassGenException("Not targeting " + oldIh + ", but " + ih + "}");
102 }
103 setInstruction(newIh);
104 }
105 }