KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > classes > InstructionBuilder


1 /*
2 Copyright (c) 2003, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.classes;
30
31 import java.security.InvalidParameterException JavaDoc;
32
33 import org.apache.bcel.Constants;
34 import org.apache.bcel.generic.*;
35
36 /**
37  * Instruction builder. Extends the basic instruction construction tools in
38  * BCEL with some convenience methods.
39  *
40  * @author Dennis M. Sosnoski
41  * @version 1.0
42  */

43
44 public class InstructionBuilder extends InstructionFactory
45 {
46     /**
47      * Constructor.
48      *
49      * @param cg class generation information
50      * @param cp constant pool generator
51      */

52
53     public InstructionBuilder(ClassGen cg, ConstantPoolGen cp) {
54         super(cg, cp);
55     }
56
57     /**
58      * Get constant pool generator.
59      *
60      * @return constant pool generator for class
61      */

62
63     public ConstantPoolGen getConstantPoolGen() {
64         return cp;
65     }
66
67     /**
68      * Create load constant instruction. Builds the most appropriate type of
69      * instruction for the value.
70      *
71      * @param value constant value to be loaded
72      * @return generated instruction information
73      */

74
75     public CompoundInstruction createLoadConstant(int value) {
76         return new PUSH(cp, value);
77     }
78
79     /**
80      * Create load constant instruction. Loads a <code>String</code> reference
81      * from the constant pool.
82      *
83      * @param value constant value to be loaded
84      * @return generated instruction information
85      */

86
87     public CompoundInstruction createLoadConstant(String JavaDoc value) {
88         return new PUSH(cp, value);
89     }
90
91     /**
92      * Create load constant instruction. Loads an unwrapped primitive value or
93      * String from the constant pool.
94      *
95      * @param value constant value to be loaded
96      * @return generated instruction information
97      */

98
99     public CompoundInstruction createLoadConstant(Object JavaDoc value) {
100         if (value instanceof Boolean JavaDoc) {
101             return new PUSH(cp, (Boolean JavaDoc)value);
102         } else if (value instanceof Character JavaDoc) {
103             return new PUSH(cp, (Character JavaDoc)value);
104         } else if (value instanceof Number JavaDoc) {
105             return new PUSH(cp, (Number JavaDoc)value);
106         } else if (value instanceof String JavaDoc) {
107             return new PUSH(cp, (String JavaDoc)value);
108         } else {
109             throw new InvalidParameterException JavaDoc
110                 ("Internal code generation error!");
111         }
112     }
113
114     /**
115      * Create getfield instruction. Uses the field information to generate
116      * the instruction.
117      *
118      * @param item information for field to be set
119      * @return generated instruction information
120      */

121
122     public FieldInstruction createGetField(ClassItem item) {
123         String JavaDoc cname = item.getClassFile().getName();
124         String JavaDoc fname = item.getName();
125         return new GETFIELD(cp.addFieldref(cname, fname, item.getSignature()));
126     }
127
128     /**
129      * Create putfield instruction. Uses the field information to generate
130      * the instruction.
131      *
132      * @param item information for field to be set
133      * @return generated instruction information
134      */

135
136     public FieldInstruction createPutField(ClassItem item) {
137         String JavaDoc cname = item.getClassFile().getName();
138         String JavaDoc fname = item.getName();
139         return new PUTFIELD(cp.addFieldref(cname, fname, item.getSignature()));
140     }
141
142     /**
143      * Create getstatic instruction. Uses the field information to generate
144      * the instruction.
145      *
146      * @param item information for field to be set
147      * @return generated instruction information
148      */

149
150     public FieldInstruction createGetStatic(ClassItem item) {
151         String JavaDoc cname = item.getClassFile().getName();
152         String JavaDoc fname = item.getName();
153         return new GETSTATIC(cp.addFieldref(cname, fname, item.getSignature()));
154     }
155
156     /**
157      * Create putstatic instruction. Uses the field information to generate
158      * the instruction.
159      *
160      * @param item information for field to be set
161      * @return generated instruction information
162      */

163
164     public FieldInstruction createPutStatic(ClassItem item) {
165         String JavaDoc cname = item.getClassFile().getName();
166         String JavaDoc fname = item.getName();
167         return new PUTSTATIC(cp.addFieldref(cname, fname, item.getSignature()));
168     }
169
170     /**
171      * Create invoke instruction for static method. Uses the method information
172      * to generate the instruction.
173      *
174      * @param item information for method to be called
175      * @return generated instruction information
176      */

177
178     public InvokeInstruction createCallStatic(ClassItem item) {
179         String JavaDoc cname = item.getClassFile().getName();
180         String JavaDoc mname = item.getName();
181         int index = cp.addMethodref(cname, mname, item.getSignature());
182         return new INVOKESTATIC(index);
183     }
184
185     /**
186      * Create invoke instruction for virtual method. Uses the method information
187      * to generate the instruction.
188      *
189      * @param item information for method to be called
190      * @return generated instruction information
191      */

192
193     public InvokeInstruction createCallVirtual(ClassItem item) {
194         String JavaDoc cname = item.getClassFile().getName();
195         String JavaDoc mname = item.getName();
196         int index = cp.addMethodref(cname, mname, item.getSignature());
197         return new INVOKEVIRTUAL(index);
198     }
199
200     /**
201      * Create invoke instruction for interface method. Uses the method
202      * information to generate the instruction.
203      *
204      * @param item information for method to be called
205      * @return generated instruction information
206      */

207
208     public InvokeInstruction createCallInterface(ClassItem item) {
209         String JavaDoc cname = item.getClassFile().getName();
210         String JavaDoc mname = item.getName();
211         String JavaDoc signature = item.getSignature();
212         return createInvoke(cname, mname, Type.getReturnType(signature),
213             Type.getArgumentTypes(signature), Constants.INVOKEINTERFACE);
214     }
215
216     /**
217      * Create invoke static method instruction from signature.
218      *
219      * @param method fully qualified class and method name
220      * @param signature method signature in standard form
221      * @return generated instruction information
222      */

223
224     public InvokeInstruction createCallStatic(String JavaDoc method, String JavaDoc signature) {
225         int split = method.lastIndexOf('.');
226         String JavaDoc cname = method.substring(0, split);
227         String JavaDoc mname = method.substring(split+1);
228         int index = cp.addMethodref(cname, mname, signature);
229         return new INVOKESTATIC(index);
230     }
231
232     /**
233      * Create invoke virtual method instruction from signature.
234      *
235      * @param method fully qualified class and method name
236      * @param signature method signature in standard form
237      * @return generated instruction information
238      */

239
240     public InvokeInstruction createCallVirtual(String JavaDoc method,
241         String JavaDoc signature) {
242         int split = method.lastIndexOf('.');
243         String JavaDoc cname = method.substring(0, split);
244         String JavaDoc mname = method.substring(split+1);
245         int index = cp.addMethodref(cname, mname, signature);
246         return new INVOKEVIRTUAL(index);
247     }
248
249     /**
250      * Create invoke interface method instruction from signature.
251      *
252      * @param method fully qualified interface and method name
253      * @param signature method signature in standard form
254      * @return generated instruction information
255      */

256
257     public InvokeInstruction createCallInterface(String JavaDoc method,
258         String JavaDoc signature) {
259         int split = method.lastIndexOf('.');
260         String JavaDoc cname = method.substring(0, split);
261         String JavaDoc mname = method.substring(split+1);
262         return createInvoke(cname, mname, Type.getReturnType(signature),
263             Type.getArgumentTypes(signature), Constants.INVOKEINTERFACE);
264     }
265
266     /**
267      * Create invoke initializer instruction from signature.
268      *
269      * @param name fully qualified class name
270      * @param signature method signature in standard form
271      * @return generated instruction information
272      */

273
274     public InvokeInstruction createCallInit(String JavaDoc name, String JavaDoc signature) {
275         int index = cp.addMethodref(name, "<init>", signature);
276         return new INVOKESPECIAL(index);
277     }
278
279     /**
280      * Create instanceof check for type.
281      *
282      * @param type method signature in standard form
283      * @return generated instruction information
284      */

285
286     public CPInstruction createInstanceOf(ObjectType type) {
287         int index = cp.addClass(type);
288         return new INSTANCEOF(index);
289     }
290 }
291
Popular Tags