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  
19  package org.apache.jdo.impl.enhancer.classfile;
20  
21  /***
22   * Environment for decoding byte codes into instructions
23   */
24  class InsnReadEnv {
25  
26      /* The parent method environment */
27      private CodeEnv codeEnv;
28  
29      /* The byte codes to be decoded */
30      private byte[] byteCodes;
31  
32      /* The index into byteCodes for the next instruction to be decoded */
33      private int currPc;
34  
35      /***
36       * Constructor
37       */
38      InsnReadEnv(byte[] bytes, CodeEnv codeEnv) {
39          this.byteCodes = bytes;
40          this.currPc = 0;
41          this.codeEnv = codeEnv;
42      }
43  
44      /***
45       * Return the index of the next instruction to decode
46       */
47      int currentPC() {
48          return currPc;
49      }
50  
51      /***
52       * Are there more byte codes to decode?
53       */
54      boolean more() {
55          return currPc < byteCodes.length;
56      }
57  
58      /***
59       * Get a single byte from the byte code stream
60       */
61      byte getByte() {
62          if (!more())
63              throw new InsnError("out of byte codes");
64  
65          return byteCodes[currPc++];
66      }
67  
68      /***
69       * Get a single unsigned byte from the byte code stream
70       */
71      int getUByte() {
72          return getByte() & 0xff;
73      }
74  
75      /***
76       * Get a short from the byte code stream
77       */
78      int getShort() {
79          byte byte1 = byteCodes[currPc++];
80          byte byte2 = byteCodes[currPc++];
81          return (byte1 << 8) | (byte2 & 0xff);
82      }
83  
84      /***
85       * Get an unsigned short from the byte code stream
86       */
87      int getUShort() {
88          return getShort() & 0xffff;
89      }
90  
91      /***
92       * Get an int from the byte code stream
93       */
94      int getInt() {
95          byte byte1 = byteCodes[currPc++];
96          byte byte2 = byteCodes[currPc++];
97          byte byte3 = byteCodes[currPc++];
98          byte byte4 = byteCodes[currPc++];
99          return (byte1 << 24) | ((byte2 & 0xff) << 16) |
100 	    ((byte3  & 0xff) << 8) | (byte4 & 0xff);
101     }
102 
103     /***
104      * Get the constant pool which applies to the method being decoded
105      */
106     ConstantPool pool() {
107         return codeEnv.pool();
108     }
109 
110     /***
111      * Get the canonical InsnTarget instance for the specified
112      * pc within the method.
113      */
114     InsnTarget getTarget(int targ) {
115         return codeEnv.getTarget(targ);
116     }
117 }