KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > tools > GenPeer


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.tools;
20
21 import gov.nasa.jpf.jvm.Types;
22
23 import java.io.PrintWriter JavaDoc;
24
25 import java.lang.reflect.*;
26
27 import java.util.ArrayList JavaDoc;
28
29
30 /**
31  * tool to automatically generate the framework of a native peer MJI class,
32  * given it's model class. GenPeer collects all the native methods from the
33  * model class, and creates the corresponding native peer methods
34  */

35 public class GenPeer {
36   static final String JavaDoc SYS_PKG = "gov.nasa.jpf.jvm";
37   static final String JavaDoc MJI_ENV = "gov.nasa.jpf.jvm.MJIEnv";
38   static final String JavaDoc INDENT = " ";
39   static final String JavaDoc METHOD_PREFIX = "public static";
40   static final String JavaDoc ENV_ARG = "MJIEnv env";
41   static final String JavaDoc OBJ_ARG = "int robj";
42   static final String JavaDoc CLS_ARG = "int rcls";
43   static final String JavaDoc REF_TYPE = "int";
44   static final String JavaDoc NULL = "MJIEnv.NULL";
45   static final String JavaDoc EXEC_COND = "$isExecutable_";
46   static final String JavaDoc DETERM_COND = "$isDeterministic_";
47   static String JavaDoc clsName;
48   static String JavaDoc[] mths;
49
50   // our options
51
static boolean isSystemPkg;
52   static boolean allMethods;
53   static boolean mangleNames;
54   static boolean clinit;
55   static boolean execCond;
56   static boolean determCond;
57
58   public static void main (String JavaDoc[] args) {
59     if ((args.length == 0) || !readOptions(args)) {
60       showUsage();
61
62       return;
63     }
64
65     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(System.out, true);
66     Class JavaDoc cls = getClass(clsName);
67
68     if (cls != null) {
69       printNativePeer(cls, pw);
70     }
71   }
72
73   static Class JavaDoc getClass (String JavaDoc cname) {
74     Class JavaDoc clazz = null;
75
76     try {
77       clazz = Class.forName(cname);
78     } catch (ClassNotFoundException JavaDoc cnfx) {
79       System.err.println("target class not found: " + cname);
80     } catch (Throwable JavaDoc x) {
81       x.printStackTrace();
82     }
83
84     return clazz;
85   }
86
87   static boolean isMJICandidate (Method m) {
88     if (allMethods) {
89       return true;
90     }
91
92     if (mths != null) {
93       String JavaDoc name = m.getName();
94
95       for (int i = 0; i < mths.length; i++) {
96         if (name.equals(mths[i])) {
97           return true;
98         }
99       }
100     } else {
101       if ((m.getModifiers() & Modifier.NATIVE) != 0) {
102         return true;
103       }
104     }
105
106     return false;
107   }
108
109   static void getMangledName (Method m) {
110     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
111
112     sb.append(m.getName());
113     sb.append("__");
114   }
115
116   static boolean isPrimitiveType (String JavaDoc t) {
117     return ("int".equals(t) || "long".equals(t) || "boolean".equals(t) ||
118            "void".equals(t) || // not really, but useful for returnTypes
119
"byte".equals(t) || "char".equals(t) || "short".equals(t) ||
120            "float".equals(t) || "double".equals(t));
121   }
122
123   static void printClinit (PrintWriter JavaDoc pw) {
124     pw.print(INDENT);
125     pw.print(METHOD_PREFIX);
126     pw.print(" void $clinit (");
127     pw.print(ENV_ARG);
128     pw.print(", ");
129     pw.print(CLS_ARG);
130     pw.println(") {");
131     pw.print(INDENT);
132     pw.println("}");
133   }
134
135   static void printFooter (Class JavaDoc cls, PrintWriter JavaDoc pw) {
136     pw.println("}");
137   }
138
139   static void printHeader (Class JavaDoc cls, PrintWriter JavaDoc pw) {
140     if (isSystemPkg) {
141       pw.print("package ");
142       pw.print(SYS_PKG);
143       pw.println(';');
144       pw.println();
145
146       pw.print("import ");
147       pw.print(MJI_ENV);
148       pw.println(";");
149       pw.println();
150     }
151
152     String JavaDoc cname = cls.getName().replace('.', '_');
153
154     pw.print("class ");
155     pw.print("JPF_");
156     pw.print(cname);
157     pw.println(" {");
158   }
159
160   static void printMethodBody (String JavaDoc rt, String JavaDoc t, PrintWriter JavaDoc pw) {
161     if (!"void".equals(rt)) {
162       pw.print(INDENT);
163       pw.print(INDENT);
164       pw.print(rt);
165
166       if ((rt == REF_TYPE) && (rt != t)) {
167         pw.print(" r");
168         pw.print(t);
169         pw.print(" = ");
170         pw.print(NULL);
171         pw.println(";");
172
173         pw.print(INDENT);
174         pw.print(INDENT);
175         pw.print("return r");
176         pw.print(t);
177         pw.println(";");
178       } else {
179         pw.print(" v = (");
180         pw.print(rt);
181         pw.println(")0;");
182
183         pw.print(INDENT);
184         pw.print(INDENT);
185         pw.println("return v;");
186       }
187     }
188   }
189
190   static void printMethodName (Method m, PrintWriter JavaDoc pw) {
191     String JavaDoc name = null;
192
193     if (mangleNames) {
194       name = Types.getJNIMangledMethodName(m);
195     } else {
196       name = m.getName();
197     }
198
199     pw.print(name);
200   }
201
202   static void printMethodStub (String JavaDoc condPrefix, Method m, PrintWriter JavaDoc pw) {
203     String JavaDoc t = null;
204     String JavaDoc rt;
205
206     pw.print(INDENT);
207     pw.print(METHOD_PREFIX);
208     pw.print(' ');
209
210     if (condPrefix == null) {
211       t = rt = stripType(m.getReturnType().getName());
212
213       if (!isPrimitiveType(rt)) {
214         rt = REF_TYPE;
215       }
216     } else {
217       rt = "boolean";
218     }
219
220     pw.print(rt);
221
222     pw.print(' ');
223
224     if (condPrefix != null) {
225       pw.print(condPrefix);
226     }
227
228     printMethodName(m, pw);
229     pw.print(" (");
230
231     printStdArgs(m, pw);
232     printTargetArgs(m, pw);
233
234     pw.println(") {");
235
236     if (condPrefix == null) {
237       printMethodBody(rt, stripType(null, t), pw);
238     } else {
239       pw.print(INDENT);
240       pw.print(INDENT);
241       pw.println("return true;");
242     }
243
244     pw.print(INDENT);
245     pw.println('}');
246   }
247
248   static void printNativePeer (Class JavaDoc cls, PrintWriter JavaDoc pw) {
249     Method[] mths = cls.getDeclaredMethods();
250
251     printHeader(cls, pw);
252
253     if (clinit) {
254       printClinit(pw);
255     }
256
257     for (int i = 0; i < mths.length; i++) {
258       Method m = mths[i];
259
260       if (isMJICandidate(m)) {
261         if (determCond) {
262           pw.println();
263           printMethodStub(DETERM_COND, m, pw);
264         }
265
266         if (execCond) {
267           pw.println();
268           printMethodStub(EXEC_COND, m, pw);
269         }
270
271         pw.println();
272         printMethodStub(null, m, pw);
273       }
274     }
275
276     printFooter(cls, pw);
277   }
278
279   static void printStdArgs (Method m, PrintWriter JavaDoc pw) {
280     pw.print(ENV_ARG);
281     pw.print(", ");
282
283     if ((m.getModifiers() & Modifier.STATIC) != 0) {
284       pw.print(CLS_ARG);
285     } else {
286       pw.print(OBJ_ARG);
287     }
288   }
289
290   static void printTargetArgs (Method m, PrintWriter JavaDoc pw) {
291     Class JavaDoc[] pt = m.getParameterTypes();
292
293     for (int i = 0; i < pt.length; i++) {
294       String JavaDoc t = stripType(pt[i].getName());
295       boolean isPrim = isPrimitiveType(t);
296
297       pw.print(", ");
298
299       if (isPrim) {
300         pw.print(t);
301         pw.print(" v");
302         pw.print(i);
303       } else {
304         pw.print(REF_TYPE);
305         pw.print(" r");
306         pw.print(stripType(null, t));
307         pw.print(i);
308       }
309     }
310   }
311
312   static String JavaDoc[] readNames (String JavaDoc[] args, int i) {
313     ArrayList JavaDoc a = null;
314
315     for (; (i < args.length) && (args[i].charAt(0) != '-'); i++) {
316       if (a == null) {
317         a = new ArrayList JavaDoc();
318       }
319
320       a.add(args[i]);
321     }
322
323     if (a != null) {
324       String JavaDoc[] names = new String JavaDoc[a.size()];
325       a.toArray(names);
326
327       return names;
328     }
329
330     return null;
331   }
332
333   static boolean readOptions (String JavaDoc[] args) {
334     for (int i = 0; i < args.length; i++) {
335       String JavaDoc arg = args[i];
336
337       if ("-s".equals(arg)) {
338         isSystemPkg = true;
339       } else if ("-m".equals(arg)) {
340         mangleNames = true;
341       } else if ("-a".equals(arg)) {
342         allMethods = true;
343       } else if ("-ci".equals(arg)) {
344         clinit = true;
345       } else if ("-dc".equals(arg)) {
346         determCond = true;
347       } else if ("-ec".equals(arg)) {
348         execCond = true;
349       } else if (arg.charAt(0) != '-') {
350         // rather simple
351
if (clsName == null) {
352           clsName = arg;
353         } else {
354           mths = readNames(args, i);
355           i += mths.length;
356         }
357       } else {
358         System.err.println("unknown option: " + arg);
359         showUsage();
360
361         return false;
362       }
363     }
364
365     return (clsName != null);
366   }
367
368   static void showUsage () {
369     System.out.println(
370           "usage: 'GenPeer [<option>..] <className> [<method>..]'");
371     System.out.println("options: -s : system peer class (gov.nasa.jpf.jvm)");
372     System.out.println(" -ci : create <clinit> MJI method");
373     System.out.println(" -m : create mangled method names");
374     System.out.println(
375           " -a : create MJI methods for all target class methods");
376     System.out.println(
377           " -dc : create isDeterministic condition methods");
378     System.out.println(" -de : create isExecutable condition methods");
379     System.out.println(" -nd : mark methods as non-deterministic");
380   }
381
382   static String JavaDoc stripType (String JavaDoc s) {
383     return stripType("java.lang", s);
384   }
385
386   static String JavaDoc stripType (String JavaDoc prefix, String JavaDoc s) {
387     int i = s.lastIndexOf('.') + 1;
388     int l = s.length() - 1;
389
390     if (s.charAt(l) == ';') {
391       s = s.substring(0, l);
392     }
393
394     if (prefix == null) {
395       if (i == 0) {
396         return s;
397       } else {
398         return s.substring(i);
399       }
400     } else {
401       if (s.startsWith(prefix) && (prefix.length() + 1 == i)) {
402         return s.substring(i);
403       } else {
404         return s;
405       }
406     }
407   }
408 }
Popular Tags