KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > j2me > bloat > MethodBuilder


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.j2me.bloat;
22
23 import java.util.*;
24
25 import EDU.purdue.cs.bloat.editor.*;
26
27 public class MethodBuilder {
28     private Map _labels;
29
30     private Map _localVars;
31
32     private MethodEditor _editor;
33
34     private BloatContext _context;
35
36     public MethodBuilder(BloatContext context, ClassEditor classEditor,
37             int modifiers, Class JavaDoc type, String JavaDoc name, Class JavaDoc[] params,
38             Class JavaDoc[] exceptions) {
39         _context = context;
40         _editor = new MethodEditor(classEditor, modifiers, type, name, params,
41                 exceptions);
42         _labels = new HashMap();
43         _localVars = new HashMap();
44         label(0);
45     }
46
47     public void label(int id) {
48         Label label = forceLabel(id);
49         _editor.addLabel(label);
50     }
51
52     private Label forceLabel(int id) {
53         Integer JavaDoc key = new Integer JavaDoc(id);
54         Label label = (Label) _labels.get(key);
55         if (label == null) {
56             label = new Label(id);
57             _labels.put(key, label);
58         }
59         return label;
60     }
61
62     private LocalVariable forceLocalVar(int id) {
63         Integer JavaDoc key = new Integer JavaDoc(id);
64         LocalVariable localVar = (LocalVariable) _localVars.get(key);
65         if (localVar == null) {
66             localVar = new LocalVariable(id);
67             _localVars.put(key, localVar);
68         }
69         return localVar;
70     }
71
72     public void aload(int id) {
73         _editor.addInstruction(Opcode.opc_aload, forceLocalVar(id));
74     }
75
76     public void iload(int id) {
77         _editor.addInstruction(Opcode.opc_iload, forceLocalVar(id));
78     }
79
80     public void newarray(Class JavaDoc clazz) {
81         _editor.addInstruction(Opcode.opc_newarray, _context.getType(clazz));
82     }
83
84     public void astore(int id) {
85         _editor.addInstruction(Opcode.opc_astore, forceLocalVar(id));
86     }
87
88     public void areturn() {
89         _editor.addInstruction(Opcode.opc_areturn);
90     }
91
92     public void returnInstruction() {
93         _editor.addInstruction(Opcode.opc_return);
94     }
95
96     public void invokeSpecial(Type parent, String JavaDoc name, Type[] params,
97             Type ret) {
98         invoke(Opcode.opc_invokespecial,parent,name,params,ret);
99     }
100
101     public void invokeVirtual(Type parent, String JavaDoc name, Type[] params,
102             Type ret) {
103         invoke(Opcode.opc_invokevirtual,parent,name,params,ret);
104     }
105
106     public void invokeStatic(Type parent, String JavaDoc name, Type[] params,
107             Type ret) {
108         invoke(Opcode.opc_invokestatic,parent,name,params,ret);
109     }
110
111     public void invokeInterface(Type parent, String JavaDoc name, Type[] params,
112             Type ret) {
113         invoke(Opcode.opc_invokeinterface,parent,name,params,ret);
114     }
115
116     private void invoke(int mode, Type parent, String JavaDoc name, Type[] params,
117             Type ret) {
118         _editor.addInstruction(mode, _context.methodRef(parent, name, params,
119                 ret));
120     }
121
122     public void newRef(Class JavaDoc clazz) {
123         _editor.addInstruction(Opcode.opc_new, _context.getType(clazz));
124     }
125
126     public void dup() {
127         _editor.addInstruction(Opcode.opc_dup);
128     }
129
130     public void athrow() {
131         _editor.addInstruction(Opcode.opc_athrow);
132     }
133
134     public void ldc(int constant) {
135         ldc(new Integer JavaDoc(constant));
136     }
137
138     public void ldc(boolean constant) {
139         ldc(constant ? 1 : 0);
140     }
141
142     public void ldc(Object JavaDoc constant) {
143         _editor.addInstruction(Opcode.opc_ldc, constant);
144     }
145
146     public void ifeq(int labelId) {
147         _editor.addInstruction(Opcode.opc_ifeq, forceLabel(labelId));
148     }
149
150     public void addTryCatch(int from, int to, int handler, Class JavaDoc thrown) {
151         _editor.addTryCatch(new TryCatch(forceLabel(from), forceLabel(to),
152                 forceLabel(handler), _context.getType(thrown)));
153     }
154
155     public void getstatic(Class JavaDoc parent, Class JavaDoc type, String JavaDoc name) {
156         getstatic(_context.getType(parent), type, name);
157     }
158
159     public void getstatic(Type parent, Class JavaDoc type, String JavaDoc name) {
160         getstatic(parent, _context.getType(type), name);
161     }
162
163     public void getstatic(Type parent, Type type, String JavaDoc name) {
164         _editor.addInstruction(Opcode.opc_getstatic, _context.fieldRef(parent,
165                 type, name));
166     }
167
168     public void putstatic(Type parent, Class JavaDoc type, String JavaDoc name) {
169         _editor.addInstruction(Opcode.opc_putstatic, _context.fieldRef(parent,
170                 _context.getType(type), name));
171     }
172
173     public void checkcast(Class JavaDoc type) {
174         checkcast(_context.getType(type));
175     }
176
177     public void checkcast(Type type) {
178         _editor.addInstruction(Opcode.opc_checkcast, type);
179     }
180
181     public void commit() {
182         _editor.commit();
183     }
184
185     public MemberRef memberRef() {
186         return _editor.memberRef();
187     }
188
189     public Type parentType() {
190         return _editor.declaringClass().type();
191     }
192
193     public void getfield(MemberRef field) {
194         _editor.addInstruction(Opcode.opc_getfield, field);
195     }
196
197     public void putfield(MemberRef field) {
198         _editor.addInstruction(Opcode.opc_putfield, field);
199     }
200
201     public void anewarray(Class JavaDoc clazz) {
202         _editor.addInstruction(Opcode.opc_newarray, _context.getType(clazz));
203     }
204
205     public void aastore() {
206         _editor.addInstruction(Opcode.opc_aastore);
207     }
208
209     public void pop() {
210         _editor.addInstruction(Opcode.opc_pop);
211     }
212
213     public void invokeLoadClassConstMethod(Class JavaDoc clazz) {
214         invokeLoadClassConstMethod(clazz.getName());
215     }
216
217     public void invokeLoadClassConstMethod(String JavaDoc clazzName) {
218         _context.invokeLoadClassConstMethod(this, clazzName);
219     }
220 }
221
Popular Tags