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