View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  package org.apache.bcel.generic;
19  
20  /**
21   * Super class for JSR - Jump to subroutine
22   *
23   */
24  public abstract class JsrInstruction extends BranchInstruction implements UnconditionalBranch,
25          TypedInstruction, StackProducer {
26  
27      JsrInstruction(final short opcode, final InstructionHandle target) {
28          super(opcode, target);
29      }
30  
31  
32      /**
33       * Empty constructor needed for Instruction.readInstruction.
34       * Not to be used otherwise.
35       */
36      JsrInstruction() {
37      }
38  
39  
40      /** @return return address type
41       */
42      @Override
43      public Type getType( final ConstantPoolGen cp ) {
44          return new ReturnaddressType(physicalSuccessor());
45      }
46  
47  
48      /**
49       * Returns an InstructionHandle to the physical successor
50       * of this JsrInstruction. <B>For this method to work,
51       * this JsrInstruction object must not be shared between
52       * multiple InstructionHandle objects!</B>
53       * Formally, there must not be InstructionHandle objects
54       * i, j where i != j and i.getInstruction() == this ==
55       * j.getInstruction().
56       * @return an InstructionHandle to the "next" instruction that
57       * will be executed when RETurned from a subroutine.
58       */
59      public InstructionHandle physicalSuccessor() {
60          InstructionHandle ih = super.getTarget();
61          // Rewind!
62          while (ih.getPrev() != null) {
63              ih = ih.getPrev();
64          }
65          // Find the handle for "this" JsrInstruction object.
66          while (ih.getInstruction() != this) {
67              ih = ih.getNext();
68          }
69          final InstructionHandle toThis = ih;
70          while (ih != null) {
71              ih = ih.getNext();
72              if ((ih != null) && (ih.getInstruction() == this)) {
73                  throw new IllegalStateException("physicalSuccessor() called on a shared JsrInstruction.");
74              }
75          }
76          // Return the physical successor
77          return toThis.getNext();
78      }
79  }