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