harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r532653 - in /harmony/enhanced/classlib/trunk/modules/tools/src: main/java/com/sun/tools/javac/ main/java/org/apache/harmony/tools/javac/ main/java/org/apache/harmony/tools/toolutils/ test/java/org/apache/harmony/tests/tools/javac/
Date Thu, 26 Apr 2007 08:23:41 GMT
Author: pyang
Date: Thu Apr 26 01:23:40 2007
New Revision: 532653

URL: http://svn.apache.org/viewvc?view=rev&rev=532653
Log:
Apply patch for HARMONY-3685([classlib][tools] missing method of com.sun.tools.javac.Main.compile([Ljava.lang.String;,
java.io.PrintWriter))

Added:
    harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java
  (with props)
Modified:
    harmony/enhanced/classlib/trunk/modules/tools/src/main/java/com/sun/tools/javac/Main.java
    harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
    harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
    harmony/enhanced/classlib/trunk/modules/tools/src/test/java/org/apache/harmony/tests/tools/javac/MainTest.java

Modified: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/com/sun/tools/javac/Main.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/com/sun/tools/javac/Main.java?view=diff&rev=532653&r1=532652&r2=532653
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/com/sun/tools/javac/Main.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/com/sun/tools/javac/Main.java
Thu Apr 26 01:23:40 2007
@@ -17,14 +17,25 @@
 
 package com.sun.tools.javac;
 
+import java.io.PrintWriter;
+import org.apache.harmony.tools.toolutils.Util;
+
 public class Main {
 
     public Main() {
         super();
     }
 
-    public int compile(String[] args) {
+    public static int compile(String[] args) {
+        return compile(args, Util.getDefaultWriter(System.out), Util.getDefaultWriter(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) ? 0 : 1;
+        return hyMain.compile(args, out, err) ? 0 : 1;
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java?view=diff&rev=532653&r1=532652&r2=532653
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Compiler.java
Thu Apr 26 01:23:40 2007
@@ -18,25 +18,20 @@
 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;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import org.apache.harmony.tools.toolutils.Util;
 
 /**
  * A proxy to the Java source code compiler itself.
  */
 class Compiler {
 
-    /* A default encoding for console messages */
-    static String CONSOLE_ENCODING = System.getProperty("console.encoding", //$NON-NLS-1$
-            "ISO8859_1"); //$NON-NLS-1$
-
     /* FIXME: Hard-coded for now, the name of the ECJ JAR file */
     static final String ECJ_JAR_FILE = "ecj_3.2.jar"; //$NON-NLS-1$
 
@@ -53,12 +48,16 @@
      * succeeded, and false otherwise.
      */
     public static boolean main(String[] args) {
-        Compiler myself = new Compiler();
-        myself.initialize();
+        return main(args, Util.getDefaultWriter(System.out), Util.getDefaultWriter(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 +79,25 @@
     // 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() {
+    public Compiler(PrintWriter out, PrintWriter err) {
         super();
-        initialize();
+        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,30 +108,17 @@
     /*
      * 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,
-                Boolean.TRUE });
+        mainInst = ctor.newInstance(new Object[] { out, err,
+                Boolean.FALSE });
     }
 
     /*
@@ -222,7 +208,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$

Modified: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java?view=diff&rev=532653&r1=532652&r2=532653
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/javac/Main.java
Thu Apr 26 01:23:40 2007
@@ -17,6 +17,9 @@
 
 package org.apache.harmony.tools.javac;
 
+import java.io.PrintWriter;
+import org.apache.harmony.tools.toolutils.Util;
+
 /**
  * This is the entry point for the javac tool.
  */
@@ -45,9 +48,25 @@
      */
     public boolean compile(String[] args) {
 
+        return compile(args,Util.getDefaultWriter(System.out), Util.getDefaultWriter(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,7 +74,7 @@
         String[] newArgs = addBootclasspath(args);
 
         /* Invoke the compiler */
-        return Compiler.main(newArgs);
+        return Compiler.main(newArgs, out, err);
     }
 
     /*

Added: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java?view=auto&rev=532653
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java
Thu Apr 26 01:23:40 2007
@@ -0,0 +1,24 @@
+package org.apache.harmony.tools.toolutils;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+
+public class Util {
+
+    /* A default encoding for console messages */
+    static String CONSOLE_ENCODING = System.getProperty("console.encoding", //$NON-NLS-1$
+            "ISO8859_1"); //$NON-NLS-1$
+
+    public static PrintWriter getDefaultWriter(OutputStream out){
+        OutputStreamWriter osw;
+        try {
+            osw = new OutputStreamWriter(out, CONSOLE_ENCODING);
+        } catch (UnsupportedEncodingException e) {
+            osw = new OutputStreamWriter(out);
+        }
+        return new PrintWriter(osw);
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/tools/src/main/java/org/apache/harmony/tools/toolutils/Util.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: harmony/enhanced/classlib/trunk/modules/tools/src/test/java/org/apache/harmony/tests/tools/javac/MainTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/tools/src/test/java/org/apache/harmony/tests/tools/javac/MainTest.java?view=diff&rev=532653&r1=532652&r2=532653
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/tools/src/test/java/org/apache/harmony/tests/tools/javac/MainTest.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/tools/src/test/java/org/apache/harmony/tests/tools/javac/MainTest.java
Thu Apr 26 01:23:40 2007
@@ -18,8 +18,19 @@
 package org.apache.harmony.tests.tools.javac;
 
 import junit.framework.TestCase;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+import com.sun.tools.javac.Main;
 
 public class MainTest extends TestCase {
+
+    public void test_main() throws Exception {
+        StringWriter out = new StringWriter();
+        String testStr = "no_this_test.java";
+        Main.compile(new String[]{testStr}, new PrintWriter(out));
+        assertTrue("The output should have " + testStr, out.toString().contains(testStr));
+    }
 
 	public void test_nothing() {
 		// bogus test



Mime
View raw message