00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.ext.util;
00032
00033 import java.io.File;
00034 import java.io.FileOutputStream;
00035
00036
00037 import org.objectweb.proactive.core.mop.JavassistByteCodeStubBuilder;
00038 import org.objectweb.proactive.core.mop.MOPClassLoader;
00039 import org.objectweb.proactive.core.mop.Utils;
00040
00041
00042 public class StubGenerator {
00043
00048 protected static String processClassName(String name) {
00049 int i = name.indexOf(".class");
00050 String tmp = name;
00051 if (i < 0) {
00052 return name;
00053 }
00054 tmp = name.substring(0, i);
00055
00056 String tmp2 = tmp.replace(File.separatorChar, '.');
00057
00058 if (tmp2.indexOf('.') == 0) {
00059 return tmp2.substring(1);
00060 }
00061 return tmp2;
00062 }
00063
00064 public static void printUsageAndExit() {
00065 System.err.println("usage: java " + StubGenerator.class.getName() +
00066 " <classes> ");
00067 System.exit(0);
00068 }
00069
00070 public static void main(String[] args) {
00071
00072 String fileName = null;
00073
00074
00075 int classIndex = 0;
00076
00077
00078 if (args.length <= 0) {
00079 printUsageAndExit();
00080 }
00081
00082 String directoryName = "./";
00083
00084 if (args[0].equals("-d")) {
00085 directoryName = args[1];
00086 classIndex = 2;
00087 }
00088
00089
00090 if (!directoryName.endsWith(System.getProperty("file.separator"))) {
00091 directoryName = directoryName +
00092 System.getProperty("file.separator");
00093 }
00094
00095
00096 for (int i = classIndex; i < args.length; i++) {
00097 try {
00098 generateClass(args[i], directoryName);
00099 } catch (Throwable e) {
00100 System.err.println(e);
00101 }
00102 }
00103 }
00104
00109 protected static void generateClass(String arg, String directoryName) {
00110 String className = processClassName(arg);
00111 String fileName = null;
00112
00113 String stubClassName;
00114
00115 try {
00116
00117
00118 byte[] data;
00119
00120
00121
00122
00123
00124
00125 if (MOPClassLoader.BYTE_CODE_MANIPULATOR.equals("javassist")) {
00126
00127 data = JavassistByteCodeStubBuilder.create(className, null);
00128 stubClassName = Utils.convertClassNameToStubClassName(className, null);
00129 } else {
00130
00131 System.err.println(
00132 "byteCodeManipulator argument is optionnal. If specified, it can only be set to javassist (ASM is no longer supported).");
00133 System.err.println(
00134 "Any other setting will result in the use of javassist, the default bytecode manipulator framework");
00135 stubClassName = null;
00136 data = null;
00137 }
00138
00139 char sep = System.getProperty("file.separator").toCharArray()[0];
00140 fileName = directoryName + stubClassName.replace('.', sep) +
00141 ".class";
00142
00143
00144 new File(fileName.substring(0, fileName.lastIndexOf(sep))).mkdirs();
00145
00146
00147 File f = new File(fileName);
00148 FileOutputStream fos = new FileOutputStream(f);
00149 fos.write(data);
00150 fos.flush();
00151 fos.close();
00152 } catch (Exception e) {
00153 System.err.println("Cannot write file " + fileName);
00154 System.err.println("Reason is " + e);
00155 }
00156 }
00157 }