KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
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 JavaDoc INIT_METHODNAME = "<init>";
32     public static final String JavaDoc EQUALS_METHODNAME = "equals";
33     private static final String JavaDoc LOADCLASSCONSTMETHODNAME = "db4o$class$";
34     private ClassFileLoader _loader;
35
36     public BloatContext(ClassFileLoader loader, String JavaDoc outputDirPath) {
37         _loader = loader;
38         _loader.setOutputDir(new File JavaDoc(outputDirPath));
39     }
40
41     public ClassFileLoader getLoader() {
42         return _loader;
43     }
44
45     public ClassEditor loadClass(String JavaDoc classPath, String JavaDoc 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 JavaDoc e) {
52             e.printStackTrace();
53         }
54         return null;
55     }
56
57     public ClassEditor createClass(int modifiers, String JavaDoc 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 JavaDoc type, String JavaDoc methodName, Class JavaDoc[] params, Class JavaDoc[] 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 JavaDoc fieldName) {
71         FieldEditor fe = new FieldEditor(ce, modifiers, type, fieldName);
72         fe.commit();
73         return fe;
74     }
75
76     public MemberRef fieldRef(Class JavaDoc parent, Class JavaDoc fieldClass, String JavaDoc name) {
77         return fieldRef(getType(parent), fieldClass, name);
78     }
79
80     public MemberRef fieldRef(Type parent, Class JavaDoc fieldClass, String JavaDoc name) {
81         return fieldRef(parent, getType(fieldClass), name);
82     }
83
84     public MemberRef fieldRef(Type parent, Type type, String JavaDoc name) {
85         return new MemberRef(parent, new NameAndType(name, type));
86     }
87
88     public MemberRef fieldRef(String JavaDoc parent, Class JavaDoc fieldClass, String JavaDoc name) {
89         Type type = Type.getType(Type.classDescriptor(parent));
90         return fieldRef(type, fieldClass, name);
91     }
92
93     public MemberRef methodRef(Type parent, String JavaDoc 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 JavaDoc name, Class JavaDoc[] param,
99             Class JavaDoc 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 JavaDoc parent, String JavaDoc name, Class JavaDoc[] param,
108             Class JavaDoc ret) {
109         return methodRef(getType(parent), name, param, ret);
110     }
111
112     public Type getType(Class JavaDoc clazz) {
113         return Type.getType(clazz);
114     }
115
116     public Type getType(String JavaDoc 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     // TODO: Why is an empty 'throws' generated according to javap?
137
public void createLoadClassConstMethod(ClassEditor ce) {
138         MethodBuilder builder = new MethodBuilder(this, ce, Modifiers.PROTECTED
139                 | Modifiers.STATIC, Class JavaDoc.class, LOADCLASSCONSTMETHODNAME,
140                 new Class JavaDoc[] { String JavaDoc.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 JavaDoc.class);
149         builder.dup();
150         builder.aload(1);
151         builder.invokeVirtual(getType(ClassNotFoundException JavaDoc.class),
152                 "getMessage", new Type[] {}, Type.STRING);
153         builder.invokeSpecial(getType(NoClassDefFoundError JavaDoc.class),
154                 INIT_METHODNAME, new Type[] { Type.STRING }, Type.VOID);
155         builder.athrow();
156         builder.addTryCatch(0, 1, 2, ClassNotFoundException JavaDoc.class);
157         builder.commit();
158     }
159
160     public void invokeLoadClassConstMethod(MethodBuilder builder,
161             String JavaDoc clazzName) {
162         builder.ldc(normalizeClassName(clazzName));
163         builder.invokeStatic(builder.parentType(),
164                 LOADCLASSCONSTMETHODNAME, new Type[] { Type.STRING }, Type.CLASS);
165     }
166
167     public String JavaDoc normalizeClassName(String JavaDoc 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 JavaDoc[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 JavaDoc 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 JavaDoc e) {
201             e.printStackTrace();
202         }
203         return f;
204     }
205 }
206
Popular Tags