KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > reflect > instruction > CPInstruction


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.reflect.instruction;
20
21 import alt.jiapi.reflect.Instruction;
22 import alt.jiapi.file.ConstantPool;
23
24 /**
25  * This class represents an Instruction, that has a reference to Constant
26  * pool
27  *
28  * @author Mika Riekkinen
29  * @author Joni Suominen
30  * @version $Revision: 1.8 $ $Date: 2004/08/08 10:16:06 $
31  */

32 public class CPInstruction extends Instruction {
33     protected ConstantPool cp;
34
35     public CPInstruction(byte[] bytes, ConstantPool cp) {
36         super(bytes);
37         this.cp = cp;
38     }
39
40     /**
41      * Gets the index to the constant pool, that is referenced by this
42      * CPInstruction.
43      */

44     public int getIndex() {
45         byte[] bytes = getBytes();
46
47         int index = 0;
48         if (bytes.length == 2) {
49             index = bytes[1] & 0xff;
50         }
51         else {
52             index = (short)(bytes[1] << 8);
53             index |= bytes[2] & 0xff;
54         }
55
56         return (0xffff) & index;
57     }
58
59
60     // private for the moment
61
private void setIndex(short index) {
62         byte[] bytes = getBytes();
63         
64         if (bytes.length == 2) {
65             if (index > 256) {
66                 // BUG: We should deal with this.
67
// LDC should change into LDC_W, etc. etc.
68
if (bytes[0] == Opcodes.LDC) {
69                     bytes = new byte[3];
70                     bytes[0] = Opcodes.LDC_W;
71                     bytes[1] = (byte)((index >> 8) & 0xff);
72                     bytes[2] = (byte)(index & 0xff);
73                     setBytes(bytes);
74                 }
75                 else {
76                     System.err.println("ERROR: cannot change index to " +index+
77                                        ", because byte[] has only one byte. " +
78                                        this);
79                 }
80             }
81             else {
82                 bytes[1] = (byte)index;
83             }
84         }
85         else {
86             bytes[1] = (byte)((index >> 8) & 0xff);
87             bytes[2] = (byte)(index & 0xff);
88         }
89
90         if (getIndex() != index) {
91             System.out.println("ERROR setting index: " + getIndex() + " != " +
92                                index + ": " + this);
93         }
94     }
95
96
97     public void replaceConstantPool(ConstantPool newCP) {
98         ConstantPool.Entry entry = getEntry();
99
100         short index = newCP.add(entry);
101
102         setIndex(index);
103         this.cp = newCP;
104     }
105
106
107     /**
108      * Get the ConstantPool entry, this CPInstruction is referencing.
109      */

110     public ConstantPool.Entry getEntry() {
111         return cp.get(getIndex());
112     }
113
114     /**
115      * Get ConstantPool
116      */

117     public ConstantPool getConstantPool() {
118         return cp;
119     }
120
121     public String JavaDoc toString() {
122         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(super.toString());
123         sb.append(" ; ");
124         sb.append(cp.get(getIndex()));
125
126         return sb.toString();
127     }
128
129 }
130
Popular Tags