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  package org.apache.bcel.generic;
18  
19  import java.io.DataOutputStream;
20  import java.io.IOException;
21  
22  import org.apache.bcel.util.ByteSequence;
23  
24  /**
25   * RET - Return from subroutine
26   *
27   * <PRE>
28   * Stack: ... -&gt; ...
29   * </PRE>
30   */
31  public class RET extends Instruction implements IndexedInstruction, TypedInstruction {
32  
33      private boolean wide;
34      private int index; // index to local variable containg the return address
35  
36      /**
37       * Empty constructor needed for Instruction.readInstruction. Not to be used otherwise.
38       */
39      RET() {
40      }
41  
42      public RET(final int index) {
43          super(org.apache.bcel.Const.RET, (short) 2);
44          setIndex(index); // May set wide as side effect
45      }
46  
47      /**
48       * Call corresponding visitor method(s). The order is: Call visitor methods of implemented interfaces first, then call
49       * methods according to the class hierarchy in descending order, i.e., the most specific visitXXX() call comes last.
50       *
51       * @param v Visitor object
52       */
53      @Override
54      public void accept(final Visitor v) {
55          v.visitRET(this);
56      }
57  
58      /**
59       * Dump instruction as byte code to stream out.
60       *
61       * @param out Output stream
62       */
63      @Override
64      public void dump(final DataOutputStream out) throws IOException {
65          if (wide) {
66              out.writeByte(org.apache.bcel.Const.WIDE);
67          }
68          out.writeByte(super.getOpcode());
69          if (wide) {
70              out.writeShort(index);
71          } else {
72              out.writeByte(index);
73          }
74      }
75  
76      /**
77       * @return index of local variable containg the return address
78       */
79      @Override
80      public final int getIndex() {
81          return index;
82      }
83  
84      /**
85       * @return return address type
86       */
87      @Override
88      public Type getType(final ConstantPoolGen cp) {
89          return ReturnaddressType.NO_TARGET;
90      }
91  
92      /**
93       * Read needed data (e.g. index) from file.
94       */
95      @Override
96      protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException {
97          this.wide = wide;
98          if (wide) {
99              index = bytes.readUnsignedShort();
100             super.setLength(4);
101         } else {
102             index = bytes.readUnsignedByte();
103             super.setLength(2);
104         }
105     }
106 
107     /**
108      * Sets index of local variable containg the return address
109      */
110     @Override
111     public final void setIndex(final int n) {
112         if (n < 0) {
113             throw new ClassGenException("Negative index value: " + n);
114         }
115         index = n;
116         setWide();
117     }
118 
119     private void setWide() {
120         wide = index > org.apache.bcel.Const.MAX_BYTE;
121         if (wide) {
122             super.setLength(4); // Including the wide byte
123         } else {
124             super.setLength(2);
125         }
126     }
127 
128     /**
129      * @return mnemonic for instruction
130      */
131     @Override
132     public String toString(final boolean verbose) {
133         return super.toString(verbose) + " " + index;
134     }
135 }