package org.objectweb.proactive.ext.util;

import java.io.*;
import java.util.StringTokenizer;
import org.objectweb.proactive.core.mop.BytecodeStubBuilder;

public class StubGenerator
{
    public static void main (String[] args)
    {

	String fileName = null;

	// Check number of arguments
	if (args.length <= 0)
	    {
		printUsageAndExit();
	    }

	// Name of the class
	String classname = args[0];



	try
	    {	
		// Generates the bytecode for the class
		BytecodeStubBuilder bsb = new BytecodeStubBuilder(classname);
		byte[] data = bsb.create();
		classname = bsb.getStubClassFullName ();

	// Deals with directory name
	String directoryName;
	if (args.length == 2)
	    {
		directoryName = args [1];
	    }
	else
	    {
		directoryName = ".";
	    }
	// If the directory name does not end with a file separator, add one
	if (!directoryName.endsWith (System.getProperty ("file.separator")))
	    {
		directoryName = directoryName + System.getProperty ("file.separator");		
	    }
	char sep = System.getProperty ("file.separator").toCharArray() [0];
	fileName = directoryName + classname.replace ('.', sep) + ".class";

		// And writes it to a file
		new File (fileName.substring (0, fileName.lastIndexOf (sep))).mkdirs();
		//	String fileName = directoryName + System.getProperty ("file.separator") + 
		File f = new File (fileName);
		FileOutputStream fos = new FileOutputStream (f);
		fos.write(data);
		fos.flush();
		fos.close();
		System.out.println ("Wrote file "+fileName);
	    }
	catch (ClassNotFoundException e)
	    {
		System.err.println ("Cannot find class "+classname);	
	    }
	catch (Exception e)
	    {
		System.err.println ("Cannot write file "+fileName);
		System.err.println ("Reason is "+e);
	    }
    }

    public static void printUsageAndExit ()
    {
	System.out.println ("usage: pac <fully-qualified name of the class> [directory where to output stub .class file]");
	System.exit (0);
    }

}

@@ -229,16 +229,95 @@
 )%0A    %7B%0A
+%09// This is the file into which we are about to write the bytecode for the stub
 %0A%09String
@@ -457,25 +457,25 @@
 String class
-n
+N
 ame = args%5B0
@@ -477,17 +477,38 @@
 rgs%5B0%5D;%0A
-%0A
+%09String stubClassName;
 %0A%0A%09try%0A%09
@@ -609,25 +609,25 @@
 uilder(class
-n
+N
 ame);%0A%09%09byte
@@ -652,22 +652,26 @@
 te();%0A%09%09
-c
+stubC
 lass
-n
+N
 ame = bs
@@ -698,16 +698,17 @@
 me ();%0A%0A
+%09
 %09// Deal
@@ -729,16 +729,17 @@
 ry name%0A
+%09
 %09String 
@@ -753,16 +753,17 @@
 ryName;%0A
+%09
 %09if (arg
@@ -777,23 +777,25 @@
 h == 2)%0A
+%09
 %09    %7B%0A
+%09
 %09%09direct
@@ -818,28 +818,32 @@
 1%5D;%0A
+%09
 %09    %7D%0A
+%09
 %09else%0A
+%09
 %09    %7B%0A
+%09
 %09%09di
@@ -861,23 +861,25 @@
  = %22.%22;%0A
+%09
 %09    %7D%0A
+%09
 %09// If t
@@ -940,16 +940,17 @@
 add one%0A
+%09
 %09if (!di
@@ -1011,23 +1011,25 @@
 tor%22)))%0A
+%09
 %09    %7B%0A
+%09
 %09%09direct
@@ -1095,23 +1095,25 @@
 or%22);%09%09%0A
+%09
 %09    %7D%0A
+%09
 %09char se
@@ -1173,16 +1173,17 @@
 () %5B0%5D;%0A
+%09
 %09fileNam
@@ -1202,22 +1202,26 @@
 yName + 
-c
+stubC
 lass
-n
+N
 ame.repl
@@ -1247,16 +1247,18 @@
 class%22;%0A
+%09%09
 %0A%09%09// An
@@ -1708,17 +1708,17 @@
  %22+class
-n
+N
 ame);%09%0A%09

I finalized the fix to the previous bug.