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