    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 */
    017package org.apache.bcel.generic;
    018
    019/**
    020 * BranchHandle is returned by specialized InstructionList.append() whenever a BranchInstruction is appended. This is
    021 * useful when the target of this instruction is not known at time of creation and must be set later via setTarget().
    022 *
    023 * @see InstructionHandle
    024 * @see Instruction
    025 * @see InstructionList
    026 */
    027public final class BranchHandle extends InstructionHandle {
    028
    029    /**
    030     * Factory method.
    031     */
    032    static BranchHandle getBranchHandle(final BranchInstruction i) {
    033        return new BranchHandle(i);
    034    }
    035
    036    // This is also a cache in case the InstructionHandle#swapInstruction() method is used
    037    // See BCEL-273
    038    private BranchInstruction bi; // An alias in fact, but saves lots of casts
    039
    040    private BranchHandle(final BranchInstruction i) {
    041        super(i);
    042        bi = i;
    043    }
    044
    045    /*
    046     * Override InstructionHandle methods: delegate to branch instruction. Through this overriding all access to the private
    047     * i_position field should be prevented.
    048     */
    049    @Override
    050    public int getPosition() {
    051        return bi.getPosition();
    052    }
    053
    054    /**
    055     * @return target of instruction.
    056     */
    057    public InstructionHandle getTarget() {
    058        return bi.getTarget();
    059    }
    060
    061    /**
    062     * Set new contents. Old instruction is disposed and may not be used anymore.
    063     */
    064    @Override // This is only done in order to apply the additional type check; could be merged with super impl.
    065    public void setInstruction(final Instruction i) { // TODO could be package-protected?
    066        super.setInstruction(i);
    067        if (!(i instanceof BranchInstruction)) {
    068            throw new ClassGenException("Assigning " + i + " to branch handle which is not a branch instruction");
    069        }
    070        bi = (BranchInstruction) i;
    071    }
    072
    073    @Override
    074    void setPosition(final int pos) {
    075        // Original code: i_position = bi.position = pos;
    076        bi.setPosition(pos);
    077        super.setPosition(pos);
    078    }
    079
    080    /**
    081     * Pass new target to instruction.
    082     */
    083    public void setTarget(final InstructionHandle ih) {
    084        bi.setTarget(ih);
    085    }
    086
    087    @Override
    088    protected int updatePosition(final int offset, final int maxOffset) {
    089        final int x = bi.updatePosition(offset, maxOffset);
    090        super.setPosition(bi.getPosition());
    091        return x;
    092    }
    093
    094    /**
    095     * Update target of instruction.
    096     */
    097    public void updateTarget(final InstructionHandle oldIh, final InstructionHandle newIh) {
    098        bi.updateTarget(oldIh, newIh);
    099    }
    100}