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  import java.io.DataOutputStream;
21  import java.io.IOException;
22  
23  /**
24   * GOTO - Branch always (to relative offset, not absolute address)
25   *
26   */
27  public class GOTO extends GotoInstruction implements VariableLengthInstruction {
28  
29      /**
30       * Empty constructor needed for Instruction.readInstruction.
31       * Not to be used otherwise.
32       */
33      GOTO() {
34      }
35  
36  
37      public GOTO(final InstructionHandle target) {
38          super(org.apache.bcel.Const.GOTO, target);
39      }
40  
41  
42      /**
43       * Dump instruction as byte code to stream out.
44       * @param out Output stream
45       */
46      @Override
47      public void dump( final DataOutputStream out ) throws IOException {
48          super.setIndex(getTargetOffset());
49          final short _opcode = getOpcode();
50          if (_opcode == org.apache.bcel.Const.GOTO) {
51              super.dump(out);
52          } else { // GOTO_W
53              super.setIndex(getTargetOffset());
54              out.writeByte(_opcode);
55              out.writeInt(super.getIndex());
56          }
57      }
58  
59  
60      /**
61       * Called in pass 2 of InstructionList.setPositions() in order to update
62       * the branch target, that may shift due to variable length instructions.
63       *
64       * @param offset additional offset caused by preceding (variable length) instructions
65       * @param max_offset the maximum offset that may be caused by these instructions
66       * @return additional offset caused by possible change of this instruction's length
67       */
68      @Override
69      protected int updatePosition( final int offset, final int max_offset ) {
70          final int i = getTargetOffset(); // Depending on old position value
71          setPosition(getPosition() + offset); // Position may be shifted by preceding expansions
72          if (Math.abs(i) >= (Short.MAX_VALUE - max_offset)) { // to large for short (estimate)
73              super.setOpcode(org.apache.bcel.Const.GOTO_W);
74              final short old_length = (short) super.getLength();
75              super.setLength(5);
76              return super.getLength() - old_length;
77          }
78          return 0;
79      }
80  
81  
82      /**
83       * Call corresponding visitor method(s). The order is:
84       * Call visitor methods of implemented interfaces first, then
85       * call methods according to the class hierarchy in descending order,
86       * i.e., the most specific visitXXX() call comes last.
87       *
88       * @param v Visitor object
89       */
90      @Override
91      public void accept( final Visitor v ) {
92          v.visitVariableLengthInstruction(this);
93          v.visitUnconditionalBranch(this);
94          v.visitBranchInstruction(this);
95          v.visitGotoInstruction(this);
96          v.visitGOTO(this);
97      }
98  }