001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.j2me.bloat;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.editor.*;
026:
027: //TODO extract generic functionality and move to db4otools
028: public class MethodBuilder {
029: private Map _labels;
030:
031: private Map _localVars;
032:
033: private MethodEditor _editor;
034:
035: private BloatJ2MEContext _context;
036:
037: public MethodBuilder(BloatJ2MEContext context,
038: ClassEditor classEditor, int modifiers, Class type,
039: String name, Class[] params, Class[] exceptions) {
040: _context = context;
041: _editor = new MethodEditor(classEditor, modifiers, type, name,
042: params, exceptions);
043: _labels = new HashMap();
044: _localVars = new HashMap();
045: label(0);
046: }
047:
048: public void label(int id) {
049: Label label = forceLabel(id);
050: _editor.addLabel(label);
051: }
052:
053: private Label forceLabel(int id) {
054: Integer key = new Integer(id);
055: Label label = (Label) _labels.get(key);
056: if (label == null) {
057: label = new Label(id);
058: _labels.put(key, label);
059: }
060: return label;
061: }
062:
063: private LocalVariable forceLocalVar(int id) {
064: Integer key = new Integer(id);
065: LocalVariable localVar = (LocalVariable) _localVars.get(key);
066: if (localVar == null) {
067: localVar = new LocalVariable(id);
068: _localVars.put(key, localVar);
069: }
070: return localVar;
071: }
072:
073: public void aload(int id) {
074: _editor.addInstruction(Opcode.opc_aload, forceLocalVar(id));
075: }
076:
077: public void iload(int id) {
078: _editor.addInstruction(Opcode.opc_iload, forceLocalVar(id));
079: }
080:
081: public void newarray(Class clazz) {
082: _editor.addInstruction(Opcode.opc_newarray, _context
083: .getType(clazz));
084: }
085:
086: public void astore(int id) {
087: _editor.addInstruction(Opcode.opc_astore, forceLocalVar(id));
088: }
089:
090: public void areturn() {
091: _editor.addInstruction(Opcode.opc_areturn);
092: }
093:
094: public void returnInstruction() {
095: _editor.addInstruction(Opcode.opc_return);
096: }
097:
098: public void invokeSpecial(Type parent, String name, Type[] params,
099: Type ret) {
100: invoke(Opcode.opc_invokespecial, parent, name, params, ret);
101: }
102:
103: public void invokeVirtual(Type parent, String name, Type[] params,
104: Type ret) {
105: invoke(Opcode.opc_invokevirtual, parent, name, params, ret);
106: }
107:
108: public void invokeStatic(Type parent, String name, Type[] params,
109: Type ret) {
110: invoke(Opcode.opc_invokestatic, parent, name, params, ret);
111: }
112:
113: public void invokeInterface(Type parent, String name,
114: Type[] params, Type ret) {
115: invoke(Opcode.opc_invokeinterface, parent, name, params, ret);
116: }
117:
118: private void invoke(int mode, Type parent, String name,
119: Type[] params, Type ret) {
120: _editor.addInstruction(mode, _context.methodRef(parent, name,
121: params, ret));
122: }
123:
124: public void newRef(Class clazz) {
125: _editor.addInstruction(Opcode.opc_new, _context.getType(clazz));
126: }
127:
128: public void dup() {
129: _editor.addInstruction(Opcode.opc_dup);
130: }
131:
132: public void athrow() {
133: _editor.addInstruction(Opcode.opc_athrow);
134: }
135:
136: public void ldc(int constant) {
137: ldc(new Integer(constant));
138: }
139:
140: public void ldc(boolean constant) {
141: ldc(constant ? 1 : 0);
142: }
143:
144: public void ldc(Object constant) {
145: _editor.addInstruction(Opcode.opc_ldc, constant);
146: }
147:
148: public void ifeq(int labelId) {
149: _editor.addInstruction(Opcode.opc_ifeq, forceLabel(labelId));
150: }
151:
152: public void addTryCatch(int from, int to, int handler, Class thrown) {
153: _editor.addTryCatch(new TryCatch(forceLabel(from),
154: forceLabel(to), forceLabel(handler), _context
155: .getType(thrown)));
156: }
157:
158: public void getstatic(Class parent, Class type, String name) {
159: getstatic(_context.getType(parent), type, name);
160: }
161:
162: public void getstatic(Type parent, Class type, String name) {
163: getstatic(parent, _context.getType(type), name);
164: }
165:
166: public void getstatic(Type parent, Type type, String name) {
167: _editor.addInstruction(Opcode.opc_getstatic, _context.fieldRef(
168: parent, type, name));
169: }
170:
171: public void putstatic(Type parent, Class type, String name) {
172: _editor.addInstruction(Opcode.opc_putstatic, _context.fieldRef(
173: parent, _context.getType(type), name));
174: }
175:
176: public void checkcast(Class type) {
177: checkcast(_context.getType(type));
178: }
179:
180: public void checkcast(Type type) {
181: _editor.addInstruction(Opcode.opc_checkcast, type);
182: }
183:
184: public void commit() {
185: _editor.commit();
186: }
187:
188: public MemberRef memberRef() {
189: return _editor.memberRef();
190: }
191:
192: public Type parentType() {
193: return _editor.declaringClass().type();
194: }
195:
196: public void getfield(MemberRef field) {
197: _editor.addInstruction(Opcode.opc_getfield, field);
198: }
199:
200: public void putfield(MemberRef field) {
201: _editor.addInstruction(Opcode.opc_putfield, field);
202: }
203:
204: public void anewarray(Class clazz) {
205: _editor.addInstruction(Opcode.opc_newarray, _context
206: .getType(clazz));
207: }
208:
209: public void aastore() {
210: _editor.addInstruction(Opcode.opc_aastore);
211: }
212:
213: public void pop() {
214: _editor.addInstruction(Opcode.opc_pop);
215: }
216:
217: public void invokeLoadClassConstMethod(Class clazz) {
218: invokeLoadClassConstMethod(clazz.getName());
219: }
220:
221: public void invokeLoadClassConstMethod(String clazzName) {
222: _context.invokeLoadClassConstMethod(this, clazzName);
223: }
224: }
|