View Javadoc

1   /*
2    * Copyright 2005 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software 
11   * distributed under the License is distributed on an "AS IS" BASIS, 
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.jdo.impl.enhancer.classfile;
19  
20  /***
21   * Environment for decoding byte codes into instructions
22   */
23  class InsnReadEnv {
24  
25      /* The parent method environment */
26      private CodeEnv codeEnv;
27  
28      /* The byte codes to be decoded */
29      private byte[] byteCodes;
30  
31      /* The index into byteCodes for the next instruction to be decoded */
32      private int currPc;
33  
34      /***
35       * Constructor
36       */
37      InsnReadEnv(byte[] bytes, CodeEnv codeEnv) {
38          this.byteCodes = bytes;
39          this.currPc = 0;
40          this.codeEnv = codeEnv;
41      }
42  
43      /***
44       * Return the index of the next instruction to decode
45       */
46      int currentPC() {
47          return currPc;
48      }
49  
50      /***
51       * Are there more byte codes to decode?
52       */
53      boolean more() {
54          return currPc < byteCodes.length;
55      }
56  
57      /***
58       * Get a single byte from the byte code stream
59       */
60      byte getByte() {
61          if (!more())
62              throw new InsnError("out of byte codes");
63  
64          return byteCodes[currPc++];
65      }
66  
67      /***
68       * Get a single unsigned byte from the byte code stream
69       */
70      int getUByte() {
71          return getByte() & 0xff;
72      }
73  
74      /***
75       * Get a short from the byte code stream
76       */
77      int getShort() {
78          byte byte1 = byteCodes[currPc++];
79          byte byte2 = byteCodes[currPc++];
80          return (byte1 << 8) | (byte2 & 0xff);
81      }
82  
83      /***
84       * Get an unsigned short from the byte code stream
85       */
86      int getUShort() {
87          return getShort() & 0xffff;
88      }
89  
90      /***
91       * Get an int from the byte code stream
92       */
93      int getInt() {
94          byte byte1 = byteCodes[currPc++];
95          byte byte2 = byteCodes[currPc++];
96          byte byte3 = byteCodes[currPc++];
97          byte byte4 = byteCodes[currPc++];
98          return (byte1 << 24) | ((byte2 & 0xff) << 16) |
99  	    ((byte3  & 0xff) << 8) | (byte4 & 0xff);
100     }
101 
102     /***
103      * Get the constant pool which applies to the method being decoded
104      */
105     ConstantPool pool() {
106         return codeEnv.pool();
107     }
108 
109     /***
110      * Get the canonical InsnTarget instance for the specified
111      * pc within the method.
112      */
113     InsnTarget getTarget(int targ) {
114         return codeEnv.getTarget(targ);
115     }
116 }