    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 * @version $Id: LineNumberGen.java 1806200 2017-08-25 16:33:06Z ggregory $
    027 * @see     LineNumber
    028 * @see     MethodGen
    029 */
    030public class LineNumberGen implements InstructionTargeter, Cloneable {
    031
    032    private InstructionHandle ih;
    033    private int src_line;
    034
    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(), src_line);
    077    }
    078
    079
    080    public void setInstruction( final InstructionHandle ih ) { // TODO could be package-protected?
    081        if (ih == null) {
    082            throw new NullPointerException("InstructionHandle may not be null");
    083        }
    084        BranchInstruction.notifyTarget(this.ih, ih, this);
    085        this.ih = ih;
    086    }
    087
    088
    089    @Override
    090    public Object clone() {
    091        try {
    092            return super.clone();
    093        } catch (final CloneNotSupportedException e) {
    094            throw new Error("Clone Not Supported"); // never happens
    095        }
    096    }
    097
    098
    099    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}