    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 * Super class for JSR - Jump to subroutine
    022 *
    023 * @version $Id$
    024 */
    025public abstract class JsrInstruction extends BranchInstruction implements UnconditionalBranch,
    026        TypedInstruction, StackProducer {
    027
    028    JsrInstruction(final short opcode, final InstructionHandle target) {
    029        super(opcode, target);
    030    }
    031
    032
    033    /**
    034     * Empty constructor needed for Instruction.readInstruction.
    035     * Not to be used otherwise.
    036     */
    037    JsrInstruction() {
    038    }
    039
    040
    041    /** @return return address type
    042     */
    043    @Override
    044    public Type getType( final ConstantPoolGen cp ) {
    045        return new ReturnaddressType(physicalSuccessor());
    046    }
    047
    048
    049    /**
    050     * Returns an InstructionHandle to the physical successor
    051     * of this JsrInstruction. <B>For this method to work,
    052     * this JsrInstruction object must not be shared between
    053     * multiple InstructionHandle objects!</B>
    054     * Formally, there must not be InstructionHandle objects
    055     * i, j where i != j and i.getInstruction() == this ==
    056     * j.getInstruction().
    057     * @return an InstructionHandle to the "next" instruction that
    058     * will be executed when RETurned from a subroutine.
    059     */
    060    public InstructionHandle physicalSuccessor() {
    061        InstructionHandle ih = super.getTarget();
    062        // Rewind!
    063        while (ih.getPrev() != null) {
    064            ih = ih.getPrev();
    065        }
    066        // Find the handle for "this" JsrInstruction object.
    067        while (ih.getInstruction() != this) {
    068            ih = ih.getNext();
    069        }
    070        final InstructionHandle toThis = ih;
    071        while (ih != null) {
    072            ih = ih.getNext();
    073            if ((ih != null) && (ih.getInstruction() == this)) {
    074                throw new RuntimeException("physicalSuccessor() called on a shared JsrInstruction.");
    075            }
    076        }
    077        // Return the physical successor
    078        return toThis.getNext();
    079    }
    080}