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 */
018package org.apache.bcel.generic;
019
020import 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 * @see     LineNumber
027 * @see     MethodGen
028 */
029public class LineNumberGen implements InstructionTargeter, Cloneable {
030
031    private InstructionHandle ih;
032    private int src_line;
033
034
035    /**
036     * Create a line number.
037     *
038     * @param ih instruction handle to reference
039     */
040    public LineNumberGen(final InstructionHandle ih, final int src_line) {
041        setInstruction(ih);
042        setSourceLine(src_line);
043    }
044
045
046    /**
047     * @return true, if ih is target of this line number
048     */
049    @Override
050    public boolean containsTarget( final InstructionHandle ih ) {
051        return this.ih == ih;
052    }
053
054
055    /**
056     * @param old_ih old target
057     * @param new_ih new target
058     */
059    @Override
060    public void updateTarget( final InstructionHandle old_ih, final InstructionHandle new_ih ) {
061        if (old_ih != ih) {
062            throw new ClassGenException("Not targeting " + old_ih + ", but " + ih + "}");
063        }
064        setInstruction(new_ih);
065    }
066
067
068    /**
069     * Get LineNumber attribute .
070     *
071     * This relies on that the instruction list has already been dumped to byte code or
072     * or that the `setPositions' methods has been called for the instruction list.
073     */
074    public LineNumber getLineNumber() {
075        return new LineNumber(ih.getPosition(), src_line);
076    }
077
078
079    public void setInstruction( final InstructionHandle ih ) { // TODO could be package-protected?
080        if (ih == null) {
081            throw new NullPointerException("InstructionHandle may not be null");
082        }
083        BranchInstruction.notifyTarget(this.ih, ih, this);
084        this.ih = ih;
085    }
086
087
088    @Override
089    public Object clone() {
090        try {
091            return super.clone();
092        } catch (final CloneNotSupportedException e) {
093            throw new Error("Clone Not Supported"); // never happens
094        }
095    }
096
097
098    public InstructionHandle getInstruction() {
099        return ih;
100    }
101
102
103    public void setSourceLine( final int src_line ) { // TODO could be package-protected?
104        this.src_line = src_line;
105    }
106
107
108    public int getSourceLine() {
109        return src_line;
110    }
111}