Index: modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java (revision 529912) +++ modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java (working copy) @@ -17,6 +17,8 @@ package org.apache.harmony.tools.javac; +import java.io.*; + /** * This is the entry point for the javac tool. */ @@ -44,10 +46,25 @@ * @return true on compilation success, false on failure */ public boolean compile(String[] args) { + return compile(args, new PrintWriter(System.out), new PrintWriter(System.err)); + } + + /** + * Invokes the ECJ compiler with the given arguments. + * + * @param args + * the arguments passed through to the compiler + * @param out + * get the output from System.out + * @param err + * get the output from System.err + * @return true on compilation success, false on failure + */ + public boolean compile(String[] args, PrintWriter out, PrintWriter err) { /* Give me something to do */ if (args == null || args.length == 0) { - new Compiler().printUsage(); + new Compiler(out, err).printUsage(); return false; } @@ -55,9 +72,9 @@ String[] newArgs = addBootclasspath(args); /* Invoke the compiler */ - return Compiler.main(newArgs); - } - + return Compiler.main(newArgs, out, err); + } + /* * Set up the compiler option to compile against the running JRE class * libraries. Index: modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java =================================================================== --- modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java (revision 529912) +++ modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java (working copy) @@ -18,9 +18,7 @@ package org.apache.harmony.tools.javac; import java.io.File; -import java.io.OutputStreamWriter; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -53,12 +51,16 @@ * succeeded, and false otherwise. */ public static boolean main(String[] args) { - Compiler myself = new Compiler(); - myself.initialize(); + return main(args, new PrintWriter(System.out), new PrintWriter(System.err)); + } + + public static boolean main(String[] args, PrintWriter out, PrintWriter err) { + Compiler myself = new Compiler(out, err); + // If there is a problem invoking the method, simply dump the trace for // now try { - Object result = myself.staticMainMth.invoke(myself.mainInst, + Object result = myself.staticCompileMth.invoke(myself.mainInst, new Object[] { args }); return (Boolean) result; } catch (IllegalArgumentException e) { @@ -80,25 +82,30 @@ // The Main#printUsage() method. Method printUsageMth; - // The static Main#main(string[]) method on the ECJ compiler - Method staticMainMth; + // The static Main#compile(string[]) method on the ECJ compiler + Method staticCompileMth; + /** * Default constructor. Returns a new initialized instance of the Java * compiler. */ public Compiler() { super(); - initialize(); + initialize(new PrintWriter(System.out), new PrintWriter(System.err)); + } + public Compiler(PrintWriter out, PrintWriter err) { + super(); + initialize(out, err); } /* * Initialize our local variables. Called during type construction. */ - protected void initialize() { + protected void initialize(PrintWriter out, PrintWriter err) { try { initializeMainClass(); - initializeInstance(); + initializeInstance(out, err); initializeMethods(); } catch (Exception e) { // If there is a problem we log it to the console @@ -109,29 +116,16 @@ /* * Defines the local instance of the ECJ compiler */ - protected void initializeInstance() throws SecurityException, + protected void initializeInstance(PrintWriter out, PrintWriter err) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { - // Set up reasonable defaults for the messages - OutputStreamWriter osw; - OutputStreamWriter esw; - try { - osw = new OutputStreamWriter(System.out, CONSOLE_ENCODING); - esw = new OutputStreamWriter(System.err, CONSOLE_ENCODING); - } catch (UnsupportedEncodingException e) { - osw = new OutputStreamWriter(System.out); - esw = new OutputStreamWriter(System.err); - } - PrintWriter outWriter = new PrintWriter(osw); - PrintWriter errWriter = new PrintWriter(esw); - // Create a new instance of the compiler Constructor ctor = ecjCompilerClass.getConstructor(new Class[] { PrintWriter.class, PrintWriter.class, Boolean.TYPE }); - mainInst = ctor.newInstance(new Object[] { outWriter, errWriter, + mainInst = ctor.newInstance(new Object[] { out, err, Boolean.TRUE }); } @@ -222,7 +216,7 @@ */ protected void initializeMethods() throws SecurityException, NoSuchMethodException { - staticMainMth = ecjCompilerClass.getMethod("main", //$NON-NLS-1$ + staticCompileMth = ecjCompilerClass.getMethod("compile", //$NON-NLS-1$ new Class[] { String[].class }); printUsageMth = ecjCompilerClass .getMethod("printUsage", (Class[]) null); //$NON-NLS-1$ Index: modules/tools/src/main/java/com/sun/tools/javac/Main.java =================================================================== --- modules/tools/src/main/java/com/sun/tools/javac/Main.java (revision 529912) +++ modules/tools/src/main/java/com/sun/tools/javac/Main.java (working copy) @@ -17,14 +17,24 @@ package com.sun.tools.javac; +import java.io.*; + public class Main { public Main() { super(); } - public int compile(String[] args) { - org.apache.harmony.tools.javac.Main hyMain = new org.apache.harmony.tools.javac.Main(); - return hyMain.compile(args) ? 0 : 1; + public static int compile(String[] args) { + return compile(args, new PrintWriter(System.out), new PrintWriter(System.err)); } + + public static int compile(String[] args, PrintWriter out) { + return compile(args, out, out); + } + + public static int compile(String[] args, PrintWriter out, PrintWriter err) { + org.apache.harmony.tools.javac.Main hyMain = new org.apache.harmony.tools.javac.Main(); + return hyMain.compile(args, out, err) ? 0 : 1; + } }