6942649: add hidden option to identify location and version of javac classes

Fri, 09 Apr 2010 15:39:39 -0700

author
jjg
date
Fri, 09 Apr 2010 15:39:39 -0700
changeset 535
96072ad00783
parent 531
de6375751eb7
child 536
396b117c1743

6942649: add hidden option to identify location and version of javac classes
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/main/Main.java file | annotate | diff | comparison | revisions
test/tools/javac/T6942649.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/main/Main.java	Fri Mar 26 22:37:04 2010 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/main/Main.java	Fri Apr 09 15:39:39 2010 -0700
     1.3 @@ -28,6 +28,9 @@
     1.4  import java.io.File;
     1.5  import java.io.IOException;
     1.6  import java.io.PrintWriter;
     1.7 +import java.net.URL;
     1.8 +import java.security.DigestInputStream;
     1.9 +import java.security.MessageDigest;
    1.10  import java.util.MissingResourceException;
    1.11  
    1.12  import com.sun.tools.javac.code.Source;
    1.13 @@ -281,6 +284,15 @@
    1.14          if (target.hasInvokedynamic()) {
    1.15              options.put("invokedynamic",  "invokedynamic");
    1.16          }
    1.17 +
    1.18 +        // handle this here so it works even if no other options given
    1.19 +        String showClass = options.get("showClass");
    1.20 +        if (showClass != null) {
    1.21 +            if (showClass.equals("showClass")) // no value given for option
    1.22 +                showClass = "com.sun.tools.javac.Main";
    1.23 +            showClass(showClass);
    1.24 +        }
    1.25 +
    1.26          return filenames.toList();
    1.27      }
    1.28      // where
    1.29 @@ -488,6 +500,37 @@
    1.30          ex.getCause().printStackTrace();
    1.31      }
    1.32  
    1.33 +    /** Display the location and checksum of a class. */
    1.34 +    void showClass(String className) {
    1.35 +        out.println("javac: show class: " + className);
    1.36 +        URL url = getClass().getResource('/' + className.replace('.', '/') + ".class");
    1.37 +        if (url == null)
    1.38 +            out.println("  class not found");
    1.39 +        else {
    1.40 +            out.println("  " + url);
    1.41 +            try {
    1.42 +                final String algorithm = "MD5";
    1.43 +                byte[] digest;
    1.44 +                MessageDigest md = MessageDigest.getInstance(algorithm);
    1.45 +                DigestInputStream in = new DigestInputStream(url.openStream(), md);
    1.46 +                try {
    1.47 +                    byte[] buf = new byte[8192];
    1.48 +                    int n;
    1.49 +                    do { n = in.read(buf); } while (n > 0);
    1.50 +                    digest = md.digest();
    1.51 +                } finally {
    1.52 +                    in.close();
    1.53 +                }
    1.54 +                StringBuilder sb = new StringBuilder();
    1.55 +                for (byte b: digest)
    1.56 +                    sb.append(String.format("%02x", b));
    1.57 +                out.println("  " + algorithm + " checksum: " + sb);
    1.58 +            } catch (Exception e) {
    1.59 +                out.println("  cannot compute digest: " + e);
    1.60 +            }
    1.61 +        }
    1.62 +    }
    1.63 +
    1.64      private JavaFileManager fileManager;
    1.65  
    1.66      /* ************************************************************************
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/T6942649.java	Fri Apr 09 15:39:39 2010 -0700
     2.3 @@ -0,0 +1,59 @@
     2.4 +/*
     2.5 + * Copyright 2010 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 +/*
    2.29 + * @test
    2.30 + * @bug 6942649
    2.31 + * @summary add hidden option to identify location and version of javac classes
    2.32 + */
    2.33 +
    2.34 +import java.io.*;
    2.35 +
    2.36 +public class T6942649 {
    2.37 +    public static void main(String... args) throws Exception {
    2.38 +        new T6942649().run();
    2.39 +    }
    2.40 +
    2.41 +    void run() throws Exception {
    2.42 +        test("-XDshowClass", "com.sun.tools.javac.Main");
    2.43 +        test("-XDshowClass=com.sun.tools.javac.util.Log", "com.sun.tools.javac.util.Log");
    2.44 +    }
    2.45 +
    2.46 +    void test(String opt, String clazz) throws Exception {
    2.47 +        System.err.println("test " + opt);
    2.48 +        StringWriter sw = new StringWriter();
    2.49 +        PrintWriter pw = new PrintWriter(sw);
    2.50 +        int rc = com.sun.tools.javac.Main.compile(new String[] { opt }, pw);
    2.51 +        pw.close();
    2.52 +        String out = sw.toString();
    2.53 +        System.err.println("javac rc=" + rc + "\n" + out);
    2.54 +        if (!out.contains(clazz))
    2.55 +            throw new Exception("class name not found in output");
    2.56 +        int lastDot = clazz.lastIndexOf(".");
    2.57 +        if (!out.contains(clazz.substring(lastDot + 1) + ".class"))
    2.58 +            throw new Exception("location of class not found in output");
    2.59 +        if (!out.contains("MD5 checksum: "))
    2.60 +            throw new Exception("checksum not found in output");
    2.61 +    }
    2.62 +}

mercurial