KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > classfile > InsnReadEnv


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.classfile;
26
27 /**
28  * Environment for decoding byte codes into instructions
29  */

30
31 class InsnReadEnv {
32
33   /* The parent method environment */
34   private CodeEnv codeEnv;
35
36   /* The byte codes to be decoded */
37   private byte[] byteCodes;
38
39   /* The index into byteCodes for the next instruction to be decoded */
40   private int currPc;
41
42   /**
43    * Constructor
44    */

45   InsnReadEnv(byte[] bytes, CodeEnv codeEnv) {
46     this.byteCodes = bytes;
47     this.currPc = 0;
48     this.codeEnv = codeEnv;
49   }
50
51   /**
52    * Return the index of the next instruction to decode
53    */

54   int currentPC() {
55     return currPc;
56   }
57
58   /**
59    * Are there more byte codes to decode?
60    */

61   boolean more() {
62     return currPc < byteCodes.length;
63   }
64
65   /**
66    * Get a single byte from the byte code stream
67    */

68   byte getByte() {
69     if (!more())
70         throw new InsnError("out of byte codes");//NOI18N
71

72     return byteCodes[currPc++];
73   }
74
75   /**
76    * Get a single unsigned byte from the byte code stream
77    */

78   int getUByte() {
79     return getByte() & 0xff;
80   }
81
82   /**
83    * Get a short from the byte code stream
84    */

85   int getShort() {
86     byte byte1 = byteCodes[currPc++];
87     byte byte2 = byteCodes[currPc++];
88     return (byte1 << 8) | (byte2 & 0xff);
89   }
90
91   /**
92    * Get an unsigned short from the byte code stream
93    */

94   int getUShort() {
95     return getShort() & 0xffff;
96   }
97
98   /**
99    * Get an int from the byte code stream
100    */

101   int getInt() {
102     byte byte1 = byteCodes[currPc++];
103     byte byte2 = byteCodes[currPc++];
104     byte byte3 = byteCodes[currPc++];
105     byte byte4 = byteCodes[currPc++];
106     return (byte1 << 24) | ((byte2 & 0xff) << 16) |
107         ((byte3 & 0xff) << 8) | (byte4 & 0xff);
108   }
109
110   /**
111    * Get the constant pool which applies to the method being decoded
112    */

113   ConstantPool pool() {
114     return codeEnv.pool();
115   }
116
117   /**
118    * Get the canonical InsnTarget instance for the specified
119    * pc within the method.
120    */

121   InsnTarget getTarget(int targ) {
122     return codeEnv.getTarget(targ);
123   }
124 }
125
Popular Tags