KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.PrintStream JavaDoc;
29
30 /**
31  * An instruction which requires a single constant from the constant
32  * pool as an immediate operand
33  */

34
35 public class InsnConstOp extends Insn {
36   /* The constant from the constant pool */
37   private ConstBasic constValue;
38
39   /* public accessors */
40
41   public int nStackArgs() {
42     int n = VMOp.ops[opcode()].nStackArgs();
43     if (n >= 0)
44       return n;
45     switch (opcode()) {
46     case opc_putstatic:
47     case opc_putfield:
48       {
49     ConstFieldRef fld = (ConstFieldRef) constValue;
50     String JavaDoc sig = fld.nameAndType().signature().asString();
51     if (sig.equals("J") || sig.equals("D"))//NOI18N
52
return (opcode() == opc_putfield) ? 3 : 2;
53     return (opcode() == opc_putfield) ? 2 : 1;
54       }
55     case opc_invokevirtual:
56     case opc_invokespecial:
57     case opc_invokestatic:
58       /* handle interface invoke too */
59     case opc_invokeinterface:
60       {
61     ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue;
62     String JavaDoc sig = meth.nameAndType().signature().asString();
63     int nMethodArgWords = Descriptor.countMethodArgWords(sig);
64     return nMethodArgWords +
65       ((opcode() == opc_invokestatic) ? 0 : 1);
66       }
67     default:
68         throw new InsnError("unexpected variable opcode");//NOI18N
69
}
70   }
71
72   public int nStackResults() {
73     int n = VMOp.ops[opcode()].nStackResults();
74     if (n >= 0)
75       return n;
76     switch (opcode()) {
77     case opc_getstatic:
78     case opc_getfield:
79       {
80     ConstFieldRef fld = (ConstFieldRef) constValue;
81     String JavaDoc sig = fld.nameAndType().signature().asString();
82     if (sig.equals("J") || sig.equals("D"))//NOI18N
83
return 2;
84     return 1;
85       }
86     case opc_invokevirtual:
87     case opc_invokespecial:
88     case opc_invokestatic:
89       /* handle interface invoke too */
90     case opc_invokeinterface:
91       {
92     ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue;
93     return Descriptor.countMethodReturnWords(
94       meth.nameAndType().signature().asString());
95       }
96     default:
97         throw new InsnError("unexpected variable opcode");//NOI18N
98
}
99   }
100
101   public String JavaDoc argTypes() {
102     switch (opcode()) {
103     case opc_putstatic:
104     case opc_putfield:
105       {
106     ConstFieldRef fld = (ConstFieldRef) constValue;
107     String JavaDoc sig = fld.nameAndType().signature().asString();
108     if (opcode() == opc_putstatic)
109       return sig;
110     else
111       return descriptorTypeOfObject(fld) + sig;
112       }
113     case opc_invokevirtual:
114     case opc_invokespecial:
115     case opc_invokestatic:
116       /* handle interface invoke too */
117     case opc_invokeinterface:
118       {
119     ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue;
120     String JavaDoc argSig =
121       Descriptor.extractArgSig(meth.nameAndType().signature().asString());
122     if (opcode() == opc_invokestatic)
123       return argSig;
124     else
125       return descriptorTypeOfObject(meth) + argSig;
126       }
127     default:
128       return VMOp.ops[opcode()].argTypes();
129     }
130   }
131
132   public String JavaDoc resultTypes() {
133     switch (opcode()) {
134     case opc_invokevirtual:
135     case opc_invokespecial:
136     case opc_invokestatic:
137       /* handle interface invoke too */
138     case opc_invokeinterface:
139       {
140     ConstBasicMemberRef meth = (ConstBasicMemberRef) constValue;
141     String JavaDoc resultSig = Descriptor.extractResultSig(
142       meth.nameAndType().signature().asString());
143     if (resultSig.equals("V"))//NOI18N
144
return "";//NOI18N
145
return resultSig;
146       }
147     case opc_getstatic:
148     case opc_getfield:
149       {
150     ConstFieldRef fld = (ConstFieldRef) constValue;
151     return fld.nameAndType().signature().asString();
152       }
153     case opc_ldc:
154     case opc_ldc_w:
155     case opc_ldc2_w:
156       {
157     ConstValue constVal = (ConstValue) constValue;
158     return constVal.descriptor();
159       }
160     default:
161       return VMOp.ops[opcode()].resultTypes();
162     }
163   }
164
165   public boolean branches() {
166     /* invokes don't count as a branch */
167     return false;
168   }
169
170   /**
171    * Return the constant pool entry which is the immediate operand
172    */

173   public ConstBasic value() {
174     return constValue;
175   }
176     
177   /**
178    * Modify the referenced constant
179    */

180   public void setValue(ConstBasic newValue) {
181     checkConstant(newValue);
182     constValue = newValue;
183   }
184     
185   /* package local methods */
186
187   void print (PrintStream JavaDoc out, int indent) {
188     ClassPrint.spaces(out, indent);
189     out.println(offset() + " " + opName(opcode()) + " pool(" + //NOI18N
190
constValue.getIndex() + ")");//NOI18N
191
}
192
193   int store(byte[] buf, int index) {
194     if (opcode() == opc_ldc && !isNarrowldc())
195       buf[index++] = (byte) opc_ldc_w;
196     else
197       buf[index++] = (byte) opcode();
198     int constIndex = constValue.getIndex();
199     if (size() == 3)
200       buf[index++] = (byte) (constIndex >> 8);
201     buf[index++] = (byte)(constIndex & 0xff);
202     return index;
203   }
204
205   int size() {
206     return isNarrowldc() ? 2 : 3;
207   }
208
209   private boolean isNarrowldc() {
210     return (opcode() == opc_ldc && constValue.getIndex() < 256);
211   }
212     
213
214   InsnConstOp (int theOpcode, ConstBasic theOperand) {
215     this(theOpcode, theOperand, NO_OFFSET);
216   }
217
218   InsnConstOp (int theOpcode, ConstBasic theOperand, int pc) {
219     super(theOpcode, pc);
220     constValue = theOperand;
221     checkConstant(theOperand);
222     if (theOpcode == opc_invokeinterface)
223         throw new InsnError("attempt to create an " + opName(theOpcode) +//NOI18N
224
" as an InsnConstOp instead of InsnInterfaceInvoke");//NOI18N
225
}
226
227   /* used only by InsnInterfaceInvoke, to make sure that opc_invokeinterface cannot
228    * come through the wrong path and miss its extra nArgsOp */

229   InsnConstOp (int theOpcode, ConstInterfaceMethodRef theOperand, int pc) {
230     super(theOpcode, pc);
231     constValue = theOperand;
232     checkConstant(theOperand);
233   }
234
235   private void checkConstant (ConstBasic operand) {
236     switch(opcode()) {
237     case opc_ldc:
238     case opc_ldc_w:
239     case opc_ldc2_w:
240       /* ConstValue */
241       if (operand == null ||
242       (! (operand instanceof ConstValue)))
243           throw new InsnError ("attempt to create an " + opName(opcode()) +//NOI18N
244
" without a ConstValue operand");//NOI18N
245
break;
246
247     case opc_getstatic:
248     case opc_putstatic:
249     case opc_getfield:
250     case opc_putfield:
251       /* ConstFieldRef */
252       if (operand == null ||
253       (! (operand instanceof ConstFieldRef)))
254           throw new InsnError ("attempt to create an " + opName(opcode()) +//NOI18N
255
" without a ConstFieldRef operand");//NOI18N
256
break;
257
258     case opc_invokevirtual:
259     case opc_invokespecial:
260     case opc_invokestatic:
261       /* ConstMethodRef */
262       if (operand == null ||
263       (! (operand instanceof ConstMethodRef)))
264           throw new InsnError ("attempt to create an " + opName(opcode()) +//NOI18N
265
" without a ConstMethodRef operand");//NOI18N
266
break;
267       
268     case opc_invokeinterface:
269       /* ConstInterfaceMethodRef */
270       if (operand == null ||
271       (! (operand instanceof ConstInterfaceMethodRef)))
272           throw new InsnError("Attempt to create an " + opName(opcode()) +//NOI18N
273
" without a ConstInterfaceMethodRef operand");//NOI18N
274
break;
275
276     case opc_new:
277     case opc_anewarray:
278     case opc_checkcast:
279     case opc_instanceof:
280       /* ConstClass */
281       if (operand == null ||
282       (! (operand instanceof ConstClass)))
283           throw new InsnError ("attempt to create an " + opName(opcode()) +//NOI18N
284
" without a ConstClass operand");//NOI18N
285
break;
286
287     default:
288         throw new InsnError ("attempt to create an " + opName(opcode()) +//NOI18N
289
" with a constant operand");//NOI18N
290
}
291   }
292
293   private final String JavaDoc descriptorTypeOfObject(ConstBasicMemberRef memRef) {
294     String JavaDoc cname = memRef.className().className().asString();
295     return "L" + cname + ";";//NOI18N
296
}
297
298 }
299
Popular Tags