1 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 type, String name, Class [] params, 38 Class [] 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 key = new Integer (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 key = new Integer (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 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 name, Type[] params, 97 Type ret) { 98 invoke(Opcode.opc_invokespecial,parent,name,params,ret); 99 } 100 101 public void invokeVirtual(Type parent, String name, Type[] params, 102 Type ret) { 103 invoke(Opcode.opc_invokevirtual,parent,name,params,ret); 104 } 105 106 public void invokeStatic(Type parent, String name, Type[] params, 107 Type ret) { 108 invoke(Opcode.opc_invokestatic,parent,name,params,ret); 109 } 110 111 public void invokeInterface(Type parent, String 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 name, Type[] params, 117 Type ret) { 118 _editor.addInstruction(mode, _context.methodRef(parent, name, params, 119 ret)); 120 } 121 122 public void newRef(Class 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 (constant)); 136 } 137 138 public void ldc(boolean constant) { 139 ldc(constant ? 1 : 0); 140 } 141 142 public void ldc(Object 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 thrown) { 151 _editor.addTryCatch(new TryCatch(forceLabel(from), forceLabel(to), 152 forceLabel(handler), _context.getType(thrown))); 153 } 154 155 public void getstatic(Class parent, Class type, String name) { 156 getstatic(_context.getType(parent), type, name); 157 } 158 159 public void getstatic(Type parent, Class type, String name) { 160 getstatic(parent, _context.getType(type), name); 161 } 162 163 public void getstatic(Type parent, Type type, String name) { 164 _editor.addInstruction(Opcode.opc_getstatic, _context.fieldRef(parent, 165 type, name)); 166 } 167 168 public void putstatic(Type parent, Class type, String name) { 169 _editor.addInstruction(Opcode.opc_putstatic, _context.fieldRef(parent, 170 _context.getType(type), name)); 171 } 172 173 public void checkcast(Class 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 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 clazz) { 214 invokeLoadClassConstMethod(clazz.getName()); 215 } 216 217 public void invokeLoadClassConstMethod(String clazzName) { 218 _context.invokeLoadClassConstMethod(this, clazzName); 219 } 220 } 221 | Popular Tags |