001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 */
018 package org.apache.bcel.generic;
019
020 import org.apache.bcel.classfile.LineNumber;
021
022 /**
023 * This class represents a line number within a method, i.e., give an instruction
024 * a line number corresponding to the source code line.
025 *
026 * @version $Id: LineNumberGen.java 1152072 2011-07-29 01:54:05Z dbrosius $
027 * @author <A HREF="mailto:m.dahm@gmx.de">M. Dahm</A>
028 * @see LineNumber
029 * @see MethodGen
030 */
031 public class LineNumberGen implements InstructionTargeter, Cloneable, java.io.Serializable {
032
033 private static final long serialVersionUID = 4939965573936108738L;
034 private InstructionHandle ih;
035 private int src_line;
036
037
038 /**
039 * Create a line number.
040 *
041 * @param ih instruction handle to reference
042 */
043 public LineNumberGen(InstructionHandle ih, int src_line) {
044 setInstruction(ih);
045 setSourceLine(src_line);
046 }
047
048
049 /**
050 * @return true, if ih is target of this line number
051 */
052 public boolean containsTarget( InstructionHandle ih ) {
053 return this.ih == ih;
054 }
055
056
057 /**
058 * @param old_ih old target
059 * @param new_ih new target
060 */
061 public void updateTarget( InstructionHandle old_ih, InstructionHandle new_ih ) {
062 if (old_ih != ih) {
063 throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}");
064 } else {
065 setInstruction(new_ih);
066 }
067 }
068
069
070 /**
071 * Get LineNumber attribute .
072 *
073 * This relies on that the instruction list has already been dumped to byte code or
074 * or that the `setPositions' methods has been called for the instruction list.
075 */
076 public LineNumber getLineNumber() {
077 return new LineNumber(ih.getPosition(), src_line);
078 }
079
080
081 public void setInstruction( InstructionHandle ih ) {
082 BranchInstruction.notifyTarget(this.ih, ih, this);
083 this.ih = ih;
084 }
085
086
087 @Override
088 public Object clone() {
089 try {
090 return super.clone();
091 } catch (CloneNotSupportedException e) {
092 System.err.println(e);
093 return null;
094 }
095 }
096
097
098 public InstructionHandle getInstruction() {
099 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 }