6879371: javap does not close internal default file manager jdk7-b72

Tue, 08 Sep 2009 11:43:57 -0700

author
jjg
date
Tue, 08 Sep 2009 11:43:57 -0700
changeset 402
261c54b2312e
parent 401
dd98acd9f717
child 403
bfad32768345
child 405
ebb6ad5a95bb

6879371: javap does not close internal default file manager
Reviewed-by: darcy

src/share/classes/com/sun/tools/javap/JavapTask.java file | annotate | diff | comparison | revisions
test/tools/javap/T6879371.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Sep 08 11:29:58 2009 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Sep 08 11:43:57 2009 -0700
     1.3 @@ -455,8 +455,19 @@
     1.4                      return EXIT_CMDERR;
     1.5              }
     1.6  
     1.7 -            boolean ok = run();
     1.8 -            return ok ? EXIT_OK : EXIT_ERROR;
     1.9 +            try {
    1.10 +                boolean ok = run();
    1.11 +                return ok ? EXIT_OK : EXIT_ERROR;
    1.12 +            } finally {
    1.13 +                if (defaultFileManager != null) {
    1.14 +                    try {
    1.15 +                        defaultFileManager.close();
    1.16 +                        defaultFileManager = null;
    1.17 +                    } catch (IOException e) {
    1.18 +                        throw new InternalError(e);
    1.19 +                    }
    1.20 +                }
    1.21 +            }
    1.22          } catch (BadArgs e) {
    1.23              reportError(e.key, e.args);
    1.24              if (e.showUsage) {
    1.25 @@ -856,7 +867,9 @@
    1.26      }
    1.27  
    1.28      private JavaFileManager getDefaultFileManager(final DiagnosticListener<? super JavaFileObject> dl, PrintWriter log) {
    1.29 -        return JavapFileManager.create(dl, log);
    1.30 +        if (defaultFileManager == null)
    1.31 +            defaultFileManager = JavapFileManager.create(dl, log);
    1.32 +        return defaultFileManager;
    1.33      }
    1.34  
    1.35      private JavaFileObject getClassFileObject(String className) throws IOException {
    1.36 @@ -1004,6 +1017,7 @@
    1.37  
    1.38      protected Context context;
    1.39      JavaFileManager fileManager;
    1.40 +    JavaFileManager defaultFileManager;
    1.41      PrintWriter log;
    1.42      DiagnosticListener<? super JavaFileObject> diagnosticListener;
    1.43      List<String> classes;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javap/T6879371.java	Tue Sep 08 11:43:57 2009 -0700
     2.3 @@ -0,0 +1,119 @@
     2.4 +/*
     2.5 + * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    2.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    2.24 + * have any questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 6879371
    2.30 + * @summary javap does not close internal default file manager
    2.31 + */
    2.32 +
    2.33 +import java.io.*;
    2.34 +import java.util.zip.*;
    2.35 +
    2.36 +public class T6879371 {
    2.37 +    public static void main(String[] args) throws Exception {
    2.38 +        new T6879371().run();
    2.39 +    }
    2.40 +
    2.41 +    public void run() throws Exception {
    2.42 +        // create a simple test class which we can put into
    2.43 +        // a test zip file and know that it will be used by
    2.44 +        // javap.
    2.45 +        File classDir = new File("classes");
    2.46 +        classDir.mkdir();
    2.47 +
    2.48 +        String className = "Test";
    2.49 +        File javaFile = writeTestFile(className);
    2.50 +        compileTestFile(classDir, javaFile);
    2.51 +
    2.52 +        test(classDir, className, false);
    2.53 +        test(classDir, className, true);
    2.54 +    }
    2.55 +
    2.56 +    void test(File classDir, String className, boolean useJavaUtilZip) throws Exception {
    2.57 +        // javac should really not be using system properties like this
    2.58 +        // -- it should really be using (hidden) options -- but until then
    2.59 +        // take care to leave system properties as we find them, so as not
    2.60 +        // to adversely affect other tests that might follow.
    2.61 +        String prev = System.getProperty("useJavaUtilZip");
    2.62 +        setProperty("useJavaUtilZip", (useJavaUtilZip ? "true" : null));
    2.63 +        try {
    2.64 +            File zipFile = zip(classDir, new File(classDir + ".zip"));
    2.65 +            javap("-classpath", zipFile.getPath(), className);
    2.66 +
    2.67 +            if (!zipFile.delete())
    2.68 +                throw new Exception("failed to delete " + zipFile);
    2.69 +        } finally {
    2.70 +            setProperty("useJavaUtilZip", prev);
    2.71 +        }
    2.72 +    }
    2.73 +
    2.74 +    File writeTestFile(String name) throws IOException {
    2.75 +        File f = new File(name + ".java");
    2.76 +        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f)));
    2.77 +        out.println("class " + name + " { }");
    2.78 +        out.close();
    2.79 +        return f;
    2.80 +    }
    2.81 +
    2.82 +    void compileTestFile(File classDir, File file) {
    2.83 +        int rc = com.sun.tools.javac.Main.compile(
    2.84 +           new String[] { "-d", classDir.getPath(), file.getPath() });
    2.85 +        if (rc != 0)
    2.86 +            throw new Error("compilation failed. rc=" + rc);
    2.87 +    }
    2.88 +
    2.89 +    File zip(File dir, File zipFile) throws IOException {
    2.90 +        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
    2.91 +        for (File file: dir.listFiles()) {
    2.92 +            if (file.isFile()) {
    2.93 +                byte[] data = new byte[(int) file.length()];
    2.94 +                DataInputStream in = new DataInputStream(new FileInputStream(file));
    2.95 +                in.readFully(data);
    2.96 +                in.close();
    2.97 +                zipOut.putNextEntry(new ZipEntry(file.getName()));
    2.98 +                zipOut.write(data, 0, data.length);
    2.99 +                zipOut.closeEntry();
   2.100 +            }
   2.101 +        }
   2.102 +        zipOut.close();
   2.103 +        return zipFile;
   2.104 +    }
   2.105 +
   2.106 +    String javap(String... args) {
   2.107 +        StringWriter sw = new StringWriter();
   2.108 +        PrintWriter out = new PrintWriter(sw);
   2.109 +        int rc = com.sun.tools.javap.Main.run(args, out);
   2.110 +        if (rc != 0)
   2.111 +            throw new Error("javap failed. rc=" + rc);
   2.112 +        out.close();
   2.113 +        return sw.toString();
   2.114 +    }
   2.115 +
   2.116 +    void setProperty(String key, String value) {
   2.117 +        if (value != null)
   2.118 +            System.setProperty(key, value);
   2.119 +        else
   2.120 +            System.getProperties().remove(key);
   2.121 +    }
   2.122 +}

mercurial