1 21 package com.db4o.j2me.bloat; 22 23 import java.io.File ; 24 25 import EDU.purdue.cs.bloat.context.PersistentBloatContext; 26 import EDU.purdue.cs.bloat.editor.*; 27 import EDU.purdue.cs.bloat.file.ClassFileLoader; 28 import EDU.purdue.cs.bloat.reflect.*; 29 30 public class BloatContext { 31 public static final String INIT_METHODNAME = "<init>"; 32 public static final String EQUALS_METHODNAME = "equals"; 33 private static final String LOADCLASSCONSTMETHODNAME = "db4o$class$"; 34 private ClassFileLoader _loader; 35 36 public BloatContext(ClassFileLoader loader, String outputDirPath) { 37 _loader = loader; 38 _loader.setOutputDir(new File (outputDirPath)); 39 } 40 41 public ClassFileLoader getLoader() { 42 return _loader; 43 } 44 45 public ClassEditor loadClass(String classPath, String className) { 46 _loader.appendClassPath(classPath); 47 try { 48 ClassInfo info = _loader.loadClass(className); 49 EditorContext context = new PersistentBloatContext(info.loader()); 50 return context.editClass(info); 51 } catch (ClassNotFoundException e) { 52 e.printStackTrace(); 53 } 54 return null; 55 } 56 57 public ClassEditor createClass(int modifiers, String className, 58 Type superType, Type[] Interfaces) { 59 EditorContext context = new PersistentBloatContext(_loader); 60 return context.newClass(modifiers, className, superType, Interfaces); 61 } 62 63 public MethodEditor createMethod(ClassEditor ce, int modiefiers, 64 Class type, String methodName, Class [] params, Class [] exeptions) { 65 return new MethodEditor(ce, modiefiers, type, methodName, params, 66 exeptions); 67 } 68 69 public FieldEditor createField(ClassEditor ce, int modifiers, Type type, 70 String fieldName) { 71 FieldEditor fe = new FieldEditor(ce, modifiers, type, fieldName); 72 fe.commit(); 73 return fe; 74 } 75 76 public MemberRef fieldRef(Class parent, Class fieldClass, String name) { 77 return fieldRef(getType(parent), fieldClass, name); 78 } 79 80 public MemberRef fieldRef(Type parent, Class fieldClass, String name) { 81 return fieldRef(parent, getType(fieldClass), name); 82 } 83 84 public MemberRef fieldRef(Type parent, Type type, String name) { 85 return new MemberRef(parent, new NameAndType(name, type)); 86 } 87 88 public MemberRef fieldRef(String parent, Class fieldClass, String name) { 89 Type type = Type.getType(Type.classDescriptor(parent)); 90 return fieldRef(type, fieldClass, name); 91 } 92 93 public MemberRef methodRef(Type parent, String name, Type[] param, Type ret) { 94 NameAndType nat = new NameAndType(name, Type.getType(param, ret)); 95 return new MemberRef(parent, nat); 96 } 97 98 public MemberRef methodRef(Type parent, String name, Class [] param, 99 Class ret) { 100 Type[] paramTypes = new Type[param.length]; 101 for (int i = 0; i < paramTypes.length; i++) { 102 paramTypes[i] = getType(param[i]); 103 } 104 return methodRef(parent, name, paramTypes, getType(ret)); 105 } 106 107 public MemberRef methodRef(Class parent, String name, Class [] param, 108 Class ret) { 109 return methodRef(getType(parent), name, param, ret); 110 } 111 112 public Type getType(Class clazz) { 113 return Type.getType(clazz); 114 } 115 116 public Type getType(String desc) { 117 return Type.getType(desc); 118 } 119 120 public Label[] createLabels(int num) { 121 Label[] labels = new Label[num + 1]; 122 for (int i = 0; i <= num; i++) { 123 labels[i] = new Label(i); 124 } 125 return labels; 126 } 127 128 public LocalVariable[] createLocalVariables(int num) { 129 LocalVariable[] localVars = new LocalVariable[num + 1]; 130 for (int i = 0; i <= num; i++) { 131 localVars[i] = new LocalVariable(i); 132 } 133 return localVars; 134 } 135 136 public void createLoadClassConstMethod(ClassEditor ce) { 138 MethodBuilder builder = new MethodBuilder(this, ce, Modifiers.PROTECTED 139 | Modifiers.STATIC, Class .class, LOADCLASSCONSTMETHODNAME, 140 new Class [] { String .class }, null); 141 builder.aload(0); 142 builder.invokeStatic(Type.CLASS, "forName", 143 new Type[] { Type.STRING }, Type.CLASS); 144 builder.label(1); 145 builder.areturn(); 146 builder.label(2); 147 builder.astore(1); 148 builder.newRef(NoClassDefFoundError .class); 149 builder.dup(); 150 builder.aload(1); 151 builder.invokeVirtual(getType(ClassNotFoundException .class), 152 "getMessage", new Type[] {}, Type.STRING); 153 builder.invokeSpecial(getType(NoClassDefFoundError .class), 154 INIT_METHODNAME, new Type[] { Type.STRING }, Type.VOID); 155 builder.athrow(); 156 builder.addTryCatch(0, 1, 2, ClassNotFoundException .class); 157 builder.commit(); 158 } 159 160 public void invokeLoadClassConstMethod(MethodBuilder builder, 161 String clazzName) { 162 builder.ldc(normalizeClassName(clazzName)); 163 builder.invokeStatic(builder.parentType(), 164 LOADCLASSCONSTMETHODNAME, new Type[] { Type.STRING }, Type.CLASS); 165 } 166 167 public String normalizeClassName(String name) { 168 return name.replace('/', '.'); 169 } 170 171 public MemberRef[] collectDeclaredFields(ClassEditor ce) { 172 FieldInfo[] fields = ce.fields(); 173 MemberRef[] refs = new MemberRef[fields.length]; 174 for (int i = 0; i < fields.length; i++) { 175 refs[i] = new FieldEditor(ce, fields[i]).memberRef(); 176 } 177 return refs; 178 } 179 180 public void addNoArgConstructor(ClassEditor ce) { 181 MethodEditor init = new MethodEditor(ce, Modifiers.PUBLIC, Type.VOID, 182 INIT_METHODNAME, new Type[0], new Type[0]); 183 MemberRef mr = methodRef(ce.superclass(), INIT_METHODNAME, 184 new Class [0], void.class); 185 init.addLabel(new Label(0)); 186 init.addInstruction(Opcode.opcx_aload, init.paramAt(0)); 187 init.addInstruction(Opcode.opcx_invokespecial, mr); 188 init.addInstruction(Opcode.opcx_return); 189 init.commit(); 190 } 191 192 public FieldEditor fieldEditor(Class clazz, FieldInfo fieldInfo) { 193 FieldEditor f = null; 194 195 try { 196 f = new FieldEditor(new ClassEditor(null, new ClassFileLoader() 197 .loadClass(clazz.getName())), fieldInfo); 198 } catch (ClassFormatException e) { 199 System.err.println(e.getMessage()); 200 } catch (ClassNotFoundException e) { 201 e.printStackTrace(); 202 } 203 return f; 204 } 205 } 206 | Popular Tags |