4884240: additional option required for javap

Mon, 04 Aug 2008 17:54:15 -0700

author
jjg
date
Mon, 04 Aug 2008 17:54:15 -0700
changeset 88
05684554f040
parent 87
fd1d361ae294
child 89
b6d5f53b3b29

4884240: additional option required for javap
Reviewed-by: ksrini

src/share/classes/com/sun/tools/javap/ClassWriter.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javap/JavapTask.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javap/Options.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javap/resources/javap.properties file | annotate | diff | comparison | revisions
test/tools/javap/T4884240.java file | annotate | diff | comparison | revisions
test/tools/javap/T6622260.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/ClassWriter.java	Mon Aug 04 15:09:02 2008 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javap/ClassWriter.java	Mon Aug 04 17:54:15 2008 -0700
     1.3 @@ -25,6 +25,7 @@
     1.4  
     1.5  package com.sun.tools.javap;
     1.6  
     1.7 +import java.net.URI;
     1.8  import java.util.Collection;
     1.9  import java.util.List;
    1.10  
    1.11 @@ -46,6 +47,8 @@
    1.12  import com.sun.tools.classfile.SourceFile_attribute;
    1.13  import com.sun.tools.classfile.Type;
    1.14  
    1.15 +import java.text.DateFormat;
    1.16 +import java.util.Date;
    1.17  import static com.sun.tools.classfile.AccessFlags.*;
    1.18  
    1.19  /*
    1.20 @@ -73,6 +76,23 @@
    1.21          constantWriter = ConstantWriter.instance(context);
    1.22      }
    1.23  
    1.24 +    void setDigest(String name, byte[] digest) {
    1.25 +        this.digestName = name;
    1.26 +        this.digest = digest;
    1.27 +    }
    1.28 +
    1.29 +    void setFile(URI uri) {
    1.30 +        this.uri = uri;
    1.31 +    }
    1.32 +
    1.33 +    void setFileSize(int size) {
    1.34 +        this.size = size;
    1.35 +    }
    1.36 +
    1.37 +    void setLastModified(long lastModified) {
    1.38 +        this.lastModified = lastModified;
    1.39 +    }
    1.40 +
    1.41      ClassFile getClassFile() {
    1.42          return classFile;
    1.43      }
    1.44 @@ -85,6 +105,32 @@
    1.45          classFile = cf;
    1.46          constant_pool = classFile.constant_pool;
    1.47  
    1.48 +        if ((options.sysInfo || options.verbose) && !options.compat) {
    1.49 +            if (uri != null) {
    1.50 +                if (uri.getScheme().equals("file"))
    1.51 +                    println("Classfile " + uri.getPath());
    1.52 +                else
    1.53 +                    println("Classfile " + uri);
    1.54 +            }
    1.55 +            if (lastModified != -1) {
    1.56 +                Date lm = new Date(lastModified);
    1.57 +                DateFormat df = DateFormat.getDateInstance();
    1.58 +                if (size > 0) {
    1.59 +                    println("Last modified " + df.format(lm) + "; size " + size + " bytes");
    1.60 +                } else {
    1.61 +                    println("Last modified " + df.format(lm));
    1.62 +                }
    1.63 +            } else if (size > 0) {
    1.64 +                println("Size " + size + " bytes");
    1.65 +            }
    1.66 +            if (digestName != null && digest != null) {
    1.67 +                StringBuilder sb = new StringBuilder();
    1.68 +                for (byte b: digest)
    1.69 +                    sb.append(String.format("%02x", b));
    1.70 +                println(digestName + " checksum " + sb);
    1.71 +            }
    1.72 +        }
    1.73 +
    1.74          Attribute sfa = cf.getAttribute(Attribute.SourceFile);
    1.75          if (sfa instanceof SourceFile_attribute) {
    1.76              println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
    1.77 @@ -570,6 +616,11 @@
    1.78      private CodeWriter codeWriter;
    1.79      private ConstantWriter constantWriter;
    1.80      private ClassFile classFile;
    1.81 +    private URI uri;
    1.82 +    private long lastModified;
    1.83 +    private String digestName;
    1.84 +    private byte[] digest;
    1.85 +    private int size;
    1.86      private ConstantPool constant_pool;
    1.87      private Method method;
    1.88      private static final String NEWLINE = System.getProperty("line.separator", "\n");
     2.1 --- a/src/share/classes/com/sun/tools/javap/JavapTask.java	Mon Aug 04 15:09:02 2008 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java	Mon Aug 04 17:54:15 2008 -0700
     2.3 @@ -27,11 +27,15 @@
     2.4  
     2.5  import java.io.EOFException;
     2.6  import java.io.FileNotFoundException;
     2.7 +import java.io.FilterInputStream;
     2.8 +import java.io.InputStream;
     2.9  import java.io.IOException;
    2.10  import java.io.OutputStream;
    2.11  import java.io.PrintWriter;
    2.12  import java.io.StringWriter;
    2.13  import java.io.Writer;
    2.14 +import java.security.DigestInputStream;
    2.15 +import java.security.MessageDigest;
    2.16  import java.text.MessageFormat;
    2.17  import java.util.ArrayList;
    2.18  import java.util.Arrays;
    2.19 @@ -199,6 +203,12 @@
    2.20              }
    2.21          },
    2.22  
    2.23 +        new Option(false, "-sysinfo") {
    2.24 +            void process(JavapTask task, String opt, String arg) {
    2.25 +                task.options.sysInfo = true;
    2.26 +            }
    2.27 +        },
    2.28 +
    2.29          new Option(false, "-Xold") {
    2.30              void process(JavapTask task, String opt, String arg) throws BadArgs {
    2.31                  // -Xold is only supported as first arg when invoked from
    2.32 @@ -494,8 +504,27 @@
    2.33                  Attribute.Factory attributeFactory = new Attribute.Factory();
    2.34                  attributeFactory.setCompat(options.compat);
    2.35                  attributeFactory.setJSR277(options.jsr277);
    2.36 -                ClassFile cf = ClassFile.read(fo.openInputStream(), attributeFactory);
    2.37 +
    2.38 +                InputStream in = fo.openInputStream();
    2.39 +                SizeInputStream sizeIn = null;
    2.40 +                MessageDigest md  = null;
    2.41 +                if (options.sysInfo || options.verbose) {
    2.42 +                    md = MessageDigest.getInstance("MD5");
    2.43 +                    in = new DigestInputStream(in, md);
    2.44 +                    in = sizeIn = new SizeInputStream(in);
    2.45 +                }
    2.46 +
    2.47 +                ClassFile cf = ClassFile.read(in, attributeFactory);
    2.48 +
    2.49 +                if (options.sysInfo || options.verbose) {
    2.50 +                    classWriter.setFile(fo.toUri());
    2.51 +                    classWriter.setLastModified(fo.getLastModified());
    2.52 +                    classWriter.setDigest("MD5", md.digest());
    2.53 +                    classWriter.setFileSize(sizeIn.size());
    2.54 +                }
    2.55 +
    2.56                  classWriter.write(cf);
    2.57 +
    2.58              } catch (ConstantPoolException e) {
    2.59                  diagnosticListener.report(createDiagnostic("err.bad.constant.pool", className, e.getLocalizedMessage()));
    2.60                  ok = false;
    2.61 @@ -665,4 +694,31 @@
    2.62      Map<Locale, ResourceBundle> bundles;
    2.63  
    2.64      private static final String progname = "javap";
    2.65 +
    2.66 +    private static class SizeInputStream extends FilterInputStream {
    2.67 +        SizeInputStream(InputStream in) {
    2.68 +            super(in);
    2.69 +        }
    2.70 +
    2.71 +        int size() {
    2.72 +            return size;
    2.73 +        }
    2.74 +
    2.75 +        @Override
    2.76 +        public int read(byte[] buf, int offset, int length) throws IOException {
    2.77 +            int n = super.read(buf, offset, length);
    2.78 +            if (n > 0)
    2.79 +                size += n;
    2.80 +            return n;
    2.81 +        }
    2.82 +
    2.83 +        @Override
    2.84 +        public int read() throws IOException {
    2.85 +            int b = super.read();
    2.86 +            size += 1;
    2.87 +            return b;
    2.88 +        }
    2.89 +
    2.90 +        private int size;
    2.91 +    }
    2.92  }
     3.1 --- a/src/share/classes/com/sun/tools/javap/Options.java	Mon Aug 04 15:09:02 2008 -0700
     3.2 +++ b/src/share/classes/com/sun/tools/javap/Options.java	Mon Aug 04 17:54:15 2008 -0700
     3.3 @@ -81,6 +81,7 @@
     3.4      public boolean showInternalSignatures;
     3.5      public boolean showAllAttrs;
     3.6      public boolean showConstants;
     3.7 +    public boolean sysInfo;
     3.8  
     3.9      public boolean compat;             // bug-for-bug compatibility mode with old javap
    3.10      public boolean jsr277;
     4.1 --- a/src/share/classes/com/sun/tools/javap/resources/javap.properties	Mon Aug 04 15:09:02 2008 -0700
     4.2 +++ b/src/share/classes/com/sun/tools/javap/resources/javap.properties	Mon Aug 04 17:54:15 2008 -0700
     4.3 @@ -67,5 +67,6 @@
     4.4  \  -constants               Show static final constants
     4.5  
     4.6  
     4.7 -
     4.8 -
     4.9 +main.opt.sysinfo=\
    4.10 +\  -sysinfo                 Show system info (path, size, date, MD5 hash)\n\
    4.11 +\                           of class being processed
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javap/T4884240.java	Mon Aug 04 17:54:15 2008 -0700
     5.3 @@ -0,0 +1,56 @@
     5.4 +/*
     5.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.  Sun designates this
    5.11 + * particular file as subject to the "Classpath" exception as provided
    5.12 + * by Sun in the LICENSE file that accompanied this code.
    5.13 + *
    5.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.17 + * version 2 for more details (a copy is included in the LICENSE file that
    5.18 + * accompanied this code).
    5.19 + *
    5.20 + * You should have received a copy of the GNU General Public License version
    5.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-15301 USA.
    5.23 + *
    5.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    5.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    5.26 + * have any questions.
    5.27 + */
    5.28 +
    5.29 +/*
    5.30 + * @test
    5.31 + * @bug 4884240
    5.32 + * @summary additional option required for javap
    5.33 + */
    5.34 +
    5.35 +import java.io.*;
    5.36 +
    5.37 +public class T4884240 {
    5.38 +    public static void main(String... args) throws Exception {
    5.39 +        new T4884240().run();
    5.40 +    }
    5.41 +
    5.42 +    public void run() throws Exception {
    5.43 +        StringWriter sw = new StringWriter();
    5.44 +        PrintWriter pw = new PrintWriter(sw);
    5.45 +        String[] args = { "-sysinfo", "java.lang.Object" };
    5.46 +        int rc = com.sun.tools.javap.Main.run(args, pw);
    5.47 +        if (rc != 0)
    5.48 +            throw new Exception("unexpected return code: " + rc);
    5.49 +        pw.close();
    5.50 +        String[] lines = sw.toString().split("\n");
    5.51 +        if (lines.length < 3
    5.52 +            || !lines[0].startsWith("Classfile")
    5.53 +            || !lines[1].startsWith("Last modified")
    5.54 +            || !lines[2].startsWith("MD5")) {
    5.55 +            System.out.println(sw);
    5.56 +            throw new Exception("unexpected output");
    5.57 +        }
    5.58 +    }
    5.59 +}
     6.1 --- a/test/tools/javap/T6622260.java	Mon Aug 04 15:09:02 2008 -0700
     6.2 +++ b/test/tools/javap/T6622260.java	Mon Aug 04 17:54:15 2008 -0700
     6.3 @@ -189,6 +189,10 @@
     6.4  
     6.5      void verify(String output) {
     6.6          System.out.println(output);
     6.7 +        if (output.startsWith("Classfile")) {
     6.8 +            // make sure to ignore filename
     6.9 +            output = output.substring(output.indexOf('\n'));
    6.10 +        }
    6.11          if (output.indexOf("-") >= 0)
    6.12              throw new Error("- found in output");
    6.13          if (output.indexOf("FFFFFF") >= 0)

mercurial