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