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