001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.bcel.generic;
020
021import java.util.Objects;
022
023import org.apache.bcel.classfile.LineNumber;
024
025/**
026 * This class represents a line number within a method, i.e., give an instruction a line number corresponding to the
027 * source code line.
028 *
029 * @see LineNumber
030 * @see MethodGen
031 */
032public class LineNumberGen implements InstructionTargeter, Cloneable {
033
034    static final LineNumberGen[] EMPTY_ARRAY = {};
035
036    private InstructionHandle ih;
037    private int srcLine;
038
039    /**
040     * Create a line number.
041     *
042     * @param ih instruction handle to reference
043     */
044    public LineNumberGen(final InstructionHandle ih, final int srcLine) {
045        setInstruction(ih);
046        setSourceLine(srcLine);
047    }
048
049    @Override
050    public Object clone() {
051        try {
052            return super.clone();
053        } catch (final CloneNotSupportedException e) {
054            throw new UnsupportedOperationException("Clone Not Supported", e); // never happens
055        }
056    }
057
058    /**
059     * @return true, if ih is target of this line number
060     */
061    @Override
062    public boolean containsTarget(final InstructionHandle ih) {
063        return this.ih == ih;
064    }
065
066    public InstructionHandle getInstruction() {
067        return ih;
068    }
069
070    /**
071     * Gets LineNumber attribute.
072     *
073     * This relies on that the instruction list has already been dumped to byte code or that the 'setPositions' methods
074     * has been called for the instruction list.
075     */
076    public LineNumber getLineNumber() {
077        return new LineNumber(ih.getPosition(), srcLine);
078    }
079
080    public int getSourceLine() {
081        return srcLine;
082    }
083
084    public void setInstruction(final InstructionHandle instructionHandle) { // TODO could be package-protected?
085        Objects.requireNonNull(instructionHandle, "instructionHandle");
086        BranchInstruction.notifyTarget(this.ih, instructionHandle, this);
087        this.ih = instructionHandle;
088    }
089
090    public void setSourceLine(final int srcLine) { // TODO could be package-protected?
091        this.srcLine = srcLine;
092    }
093
094    /**
095     * @param oldIh old target
096     * @param newIh new target
097     */
098    @Override
099    public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
100        if (oldIh != ih) {
101            throw new ClassGenException("Not targeting " + oldIh + ", but " + ih + "}");
102        }
103        setInstruction(newIh);
104    }
105}