View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.bcel.generic;
20  
21  import java.io.DataOutputStream;
22  import java.io.IOException;
23  
24  import org.apache.bcel.util.ByteSequence;
25  
26  /**
27   * RET - Return from subroutine
28   *
29   * <PRE>
30   * Stack: ... -&gt; ...
31   * </PRE>
32   */
33  public class RET extends Instruction implements IndexedInstruction, TypedInstruction {
34  
35      private boolean wide;
36      private int index; // index to local variable containg the return address
37  
38      /**
39       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
40       */
41      RET() {
42      }
43  
44      public RET(final int index) {
45          super(org.apache.bcel.Const.RET, (short) 2);
46          setIndex(index); // May set wide as side effect
47      }
48  
49      /**
50       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
51       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
52       *
53       * @param v Visitor object
54       */
55      @Override
56      public void accept(final Visitor v) {
57          v.visitRET(this);
58      }
59  
60      /**
61       * Dump instruction as byte code to stream out.
62       *
63       * @param out Output stream
64       */
65      @Override
66      public void dump(final DataOutputStream out) throws IOException {
67          if (wide) {
68              out.writeByte(org.apache.bcel.Const.WIDE);
69          }
70          out.writeByte(super.getOpcode());
71          if (wide) {
72              out.writeShort(index);
73          } else {
74              out.writeByte(index);
75          }
76      }
77  
78      /**
79       * @return index of local variable containg the return address
80       */
81      @Override
82      public final int getIndex() {
83          return index;
84      }
85  
86      /**
87       * @return return address type
88       */
89      @Override
90      public Type getType(final ConstantPoolGen cp) {
91          return ReturnaddressType.NO_TARGET;
92      }
93  
94      /**
95       * Reads needed data (for example index) from file.
96       */
97      @Override
98      protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
99          this.wide = wide;
100         if (wide) {
101             index = bytes.readUnsignedShort();
102             super.setLength(4);
103         } else {
104             index = bytes.readUnsignedByte();
105             super.setLength(2);
106         }
107     }
108 
109     /**
110      * Sets index of local variable containg the return address
111      */
112     @Override
113     public final void setIndex(final int n) {
114         if (n < 0) {
115             throw new ClassGenException("Negative index value: " + n);
116         }
117         index = n;
118         setWide();
119     }
120 
121     private void setWide() {
122         wide = index > org.apache.bcel.Const.MAX_BYTE;
123         if (wide) {
124             super.setLength(4); // Including the wide byte
125         } else {
126             super.setLength(2);
127         }
128     }
129 
130     /**
131      * @return mnemonic for instruction
132      */
133     @Override
134     public String toString(final boolean verbose) {
135         return super.toString(verbose) + " " + index;
136     }
137 }