KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > util > StubGenerator


1 package org.objectweb.proactive.ext.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5
6 import org.objectweb.proactive.core.mop.ASMBytecodeStubBuilder;
7 import org.objectweb.proactive.core.mop.BytecodeStubBuilder;
8 import org.objectweb.proactive.core.mop.MOPClassLoader;
9
10 public class StubGenerator {
11     public static void main(String JavaDoc[] args) {
12         // This is the file into which we are about to write the bytecode for the stub
13
String JavaDoc fileName = null;
14
15         // Check number of arguments
16
if (args.length <= 0) {
17             printUsageAndExit();
18         }
19
20         // Name of the class
21
String JavaDoc className = args[0];
22         String JavaDoc stubClassName;
23
24         try {
25             // Generates the bytecode for the class
26

27             //ASM is now the default bytecode manipulator
28
byte[] data;
29             if (MOPClassLoader.BYTE_CODE_MANIPULATOR.equals("ASM")) {
30                 ASMBytecodeStubBuilder bsb = new ASMBytecodeStubBuilder(className);
31                 data = bsb.create();
32                 stubClassName = bsb.getStubClassFullName();
33             } else if (MOPClassLoader.BYTE_CODE_MANIPULATOR.equals("BCEL")) {
34                 BytecodeStubBuilder bsb = new BytecodeStubBuilder(className);
35                 data = bsb.create();
36                 stubClassName = bsb.getStubClassFullName();
37             } else {
38                 // that shouldn't happen, unless someone manually sets the BYTE_CODE_MANIPULATOR static variable
39
System.err.println(
40                     "byteCodeManipulator argument is optionnal. If specified, it can only be set to BCEL.");
41                 System.err.println(
42                     "Any other setting will result in the use of ASM, the default bytecode manipulator framework");
43                 stubClassName = null;
44                 data = null;
45             }
46             // Deals with directory name
47
String JavaDoc directoryName;
48             if (args.length == 2) {
49                 directoryName = args[1];
50             } else {
51                 directoryName = ".";
52             }
53             // If the directory name does not end with a file separator, add one
54
if (!directoryName.endsWith(System.getProperty("file.separator"))) {
55                 directoryName = directoryName + System.getProperty("file.separator");
56             }
57             char sep = System.getProperty("file.separator").toCharArray()[0];
58             fileName = directoryName + stubClassName.replace('.', sep) + ".class";
59
60             // And writes it to a file
61
new File JavaDoc(fileName.substring(0, fileName.lastIndexOf(sep))).mkdirs();
62             // String fileName = directoryName + System.getProperty ("file.separator") +
63
File JavaDoc f = new File JavaDoc(fileName);
64             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(f);
65             fos.write(data);
66             fos.flush();
67             fos.close();
68             System.out.println("Wrote file " + fileName);
69         } catch (ClassNotFoundException JavaDoc e) {
70             System.err.println("Cannot find class " + className);
71         } catch (Exception JavaDoc e) {
72             System.err.println("Cannot write file " + fileName);
73             System.err.println("Reason is " + e);
74         }
75     }
76
77     public static void printUsageAndExit() {
78         System.out.println(
79             "usage: pac <fully-qualified name of the class> [directory where to output stub .class file]");
80         System.exit(0);
81     }
82
83 }
84
Popular Tags