Fixing bug 258144: my take on patch patch from Andrew Madigan <andymadigan@yahoo.com> to add class compiler option controlling which class will be used for main method implementation.

git-svn-id: svn://10.0.0.236/trunk@161864 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
igor%mir2.org 2004-09-07 19:11:39 +00:00
parent b7ef74828b
commit 515f364a35
3 changed files with 43 additions and 2 deletions

View File

@ -56,6 +56,30 @@ public class ClassCompiler
{
if (compilerEnv == null) throw new IllegalArgumentException();
this.compilerEnv = compilerEnv;
this.mainMethodClassName = Codegen.DEFAULT_MAIN_METHOD_CLASS;
}
/**
* Set the class name to use for main method implementation.
* The class must have a method matching
* <tt>public static void main(Script sc, String[] args)</tt>, it will be
* called when <tt>main(String[] args)</tt> is called in the generated
* class. The class name should be fully qulified name and include the
* package name like in <tt>org.foo.Bar<tt>.
*/
public void setMainMethodClass(String className)
{
// XXX Should this check for a valid class name?
mainMethodClassName = className;
}
/**
* Get the name of the class for main method implementation.
* @see #setMainMethodClass(String)
*/
public String getMainMethodClass()
{
return mainMethodClassName;
}
/**
@ -149,6 +173,7 @@ public class ClassCompiler
}
Codegen codegen = new Codegen();
codegen.setMainMethodClass(mainMethodClassName);
byte[] scriptClassBytes
= codegen.compileToClassFile(compilerEnv, scriptClassName,
tree, encodedSource,
@ -178,6 +203,7 @@ public class ClassCompiler
scriptClassName, scriptClassBytes };
}
private String mainMethodClassName;
private CompilerEnvirons compilerEnv;
private Class targetExtends;
private Class[] targetImplements;

View File

@ -484,7 +484,7 @@ public class Codegen extends Interpreter
// 5: this, cx, scope, js this, args[]
}
private static void generateMain(ClassFileWriter cfw)
private void generateMain(ClassFileWriter cfw)
{
cfw.startMethod("main", "([Ljava/lang/String;)V",
(short)(ClassFileWriter.ACC_PUBLIC
@ -497,8 +497,9 @@ public class Codegen extends Interpreter
"<init>", "()V");
// load 'args'
cfw.add(ByteCode.ALOAD_0);
// Call mainMethodClass.main(Script script, String[] args)
cfw.addInvoke(ByteCode.INVOKESTATIC,
"org/mozilla/javascript/optimizer/OptRuntime",
mainMethodClass,
"main",
"(Lorg/mozilla/javascript/Script;[Ljava/lang/String;)V");
cfw.add(ByteCode.RETURN);
@ -1031,6 +1032,14 @@ public class Codegen extends Interpreter
throw new RuntimeException("Bad tree in codegen");
}
void setMainMethodClass(String className)
{
mainMethodClass = className;
}
static final String DEFAULT_MAIN_METHOD_CLASS
= "org.mozilla.javascript.optimizer.OptRuntime";
private static final String SUPER_CLASS_NAME
= "org.mozilla.javascript.NativeFunction";
@ -1064,6 +1073,8 @@ public class Codegen extends Interpreter
ScriptOrFnNode[] scriptOrFnNodes;
private ObjToIntMap scriptOrFnIndexes;
private String mainMethodClass = DEFAULT_MAIN_METHOD_CLASS;
String mainClassName;
String mainClassSignature;

View File

@ -136,6 +136,10 @@ public class Main {
compilerEnv.setGenerateDebugInfo(true);
continue;
}
if (arg.equals("-main-method-class") && ++i < args.length) {
compiler.setMainMethodClass(args[i]);
continue;
}
if (arg.equals("-o") && ++i < args.length) {
String name = args[i];
int end = name.length();