changeset 2335:57005bdeba6d

7118295: javac does not explicitly close -Xstdout file Reviewed-by: ksrini, jjg
author kizune
date Thu, 20 Mar 2014 15:13:26 +0400
parents 3b4e7a027fcc
children 2303c31516a8
files src/share/classes/com/sun/tools/javac/main/Main.java src/share/classes/com/sun/tools/javac/main/Option.java test/tools/javac/StdoutCloseTest.java
diffstat 3 files changed, 96 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/tools/javac/main/Main.java	Wed Mar 19 17:39:28 2014 -0400
+++ b/src/share/classes/com/sun/tools/javac/main/Main.java	Thu Mar 20 15:13:26 2014 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -506,6 +506,11 @@
                 }
             }
 
+            if (options.get(XSTDOUT) != null) {
+                // Stdout reassigned - ask compiler to close it when it is done
+                comp.closeables = comp.closeables.prepend(log.getWriter(WriterKind.NOTICE));
+            }
+
             fileManager = context.get(JavaFileManager.class);
 
             if (!files.isEmpty()) {
--- a/src/share/classes/com/sun/tools/javac/main/Option.java	Wed Mar 19 17:39:28 2014 -0400
+++ b/src/share/classes/com/sun/tools/javac/main/Option.java	Thu Mar 20 15:13:26 2014 +0400
@@ -399,7 +399,6 @@
         public boolean process(OptionHelper helper, String option, String arg) {
             try {
                 Log log = helper.getLog();
-                // TODO: this file should be closed at the end of compilation
                 log.setWriters(new PrintWriter(new FileWriter(arg), true));
             } catch (java.io.IOException e) {
                 helper.error("err.error.writing.file", arg, e);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/tools/javac/StdoutCloseTest.java	Thu Mar 20 15:13:26 2014 +0400
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 7118295
+ * @summary javac does not explicitly close -Xstdout file
+ * @run main StdoutCloseTest
+ */
+
+
+import com.sun.tools.javac.util.Log;
+import com.sun.tools.javac.main.Main;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+
+public class StdoutCloseTest {
+
+    public static void main(String[] args) throws Exception {
+        new StdoutCloseTest().test();
+    }
+
+    static final String program = "public class Test {\n" +
+                                  "  public boolean test() {\n" +
+                                  "    int i;\n" +
+                                  "    if (i > 0) return true;\n" +
+                                  "    return false;\n" +
+                                  "  }\n" +
+                                  "}\n";
+
+    public void test() throws Exception {
+        final String sourceName = "Test.java";
+        final String outName = "Test.out";
+        File source = new File(sourceName);
+        PrintWriter pw = new PrintWriter(source);
+        pw.write(program);
+        pw.flush();
+        pw.close();
+
+        PrintWriter log = compileClass(sourceName, outName);
+
+        File outFile = new File(outName);
+        if (!outFile.exists()) {
+            throw new Exception("Output file was not created!");
+        }
+        if (!log.checkError()) { // will return true if the stream is still open
+            log.close(); // Close output PrintWriter manually
+            throw new Exception("Output file was still open!");
+        }
+    }
+
+    public PrintWriter compileClass(String src, String out) {
+        List<String> options = new ArrayList<>();
+        options.add("-Xstdout");
+        options.add(out);
+        options.add(src);
+
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        Main compiler = new Main("javac", pw);
+        compiler.compile(options.toArray(new String[options.size()]));
+        pw.flush();
+        if (sw.getBuffer().length() > 0) {
+            System.err.println(sw.toString());
+        }
+        return compiler.log.getWriter(Log.WriterKind.NOTICE);
+    }
+}