6980017: javap -XDdetail:source behaves badly if source not available.

Thu, 26 Aug 2010 16:13:33 -0700

author
jjg
date
Thu, 26 Aug 2010 16:13:33 -0700
changeset 660
ae3acbf63943
parent 659
cfd047f3cf60
child 661
3a9f319be48a

6980017: javap -XDdetail:source behaves badly if source not available.
Reviewed-by: ksrini

src/share/classes/com/sun/tools/javap/CodeWriter.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javap/SourceWriter.java file | annotate | diff | comparison | revisions
test/tools/javap/T6980017.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/CodeWriter.java	Thu Aug 26 15:17:17 2010 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javap/CodeWriter.java	Thu Aug 26 16:13:33 2010 -0700
     1.3 @@ -239,7 +239,10 @@
     1.4                  new ArrayList<InstructionDetailWriter>();
     1.5          if (options.details.contains(InstructionDetailWriter.Kind.SOURCE)) {
     1.6              sourceWriter.reset(classWriter.getClassFile(), attr);
     1.7 -            detailWriters.add(sourceWriter);
     1.8 +            if (sourceWriter.hasSource())
     1.9 +                detailWriters.add(sourceWriter);
    1.10 +            else
    1.11 +                println("(Source code not available)");
    1.12          }
    1.13  
    1.14          if (options.details.contains(InstructionDetailWriter.Kind.LOCAL_VARS)) {
     2.1 --- a/src/share/classes/com/sun/tools/javap/SourceWriter.java	Thu Aug 26 15:17:17 2010 -0700
     2.2 +++ b/src/share/classes/com/sun/tools/javap/SourceWriter.java	Thu Aug 26 16:13:33 2010 -0700
     2.3 @@ -99,7 +99,10 @@
     2.4                  }
     2.5              }
     2.6          }
     2.7 +    }
     2.8  
     2.9 +    public boolean hasSource() {
    2.10 +        return (sourceLines.length > 0);
    2.11      }
    2.12  
    2.13      private void setLineMap(Code_attribute attr) {
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javap/T6980017.java	Thu Aug 26 16:13:33 2010 -0700
     3.3 @@ -0,0 +1,78 @@
     3.4 +/*
     3.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 6980017
    3.30 + * @summary javap -XDdetail:source behaves badly if source not available.
    3.31 + */
    3.32 +
    3.33 +import java.io.*;
    3.34 +
    3.35 +public class T6980017 {
    3.36 +    public static void main(String... args) throws Exception {
    3.37 +        new T6980017().run();
    3.38 +    }
    3.39 +
    3.40 +    void run() throws Exception {
    3.41 +
    3.42 +        String[] args = {
    3.43 +            "-v",
    3.44 +            "-XDdetails:source",
    3.45 +            "java.lang.String"
    3.46 +        };
    3.47 +
    3.48 +        StringWriter sw = new StringWriter();
    3.49 +        PrintWriter pw = new PrintWriter(sw);
    3.50 +        int rc = com.sun.tools.javap.Main.run(args, pw);
    3.51 +        pw.close();
    3.52 +        if (rc != 0)
    3.53 +            error("Unexpected exit code: " + rc);
    3.54 +
    3.55 +        boolean foundBlankSourceLine = false;
    3.56 +        boolean foundNoSourceLine = false;
    3.57 +        for (String line: sw.toString().split("[\r\n]+")) {
    3.58 +            System.err.println(line);
    3.59 +            if (line.contains("Source code not available"))
    3.60 +                foundNoSourceLine = true;
    3.61 +            if (line.matches("\\s*\\( *[0-9]+\\)\\s*"))
    3.62 +                foundBlankSourceLine = true;
    3.63 +        }
    3.64 +
    3.65 +        if (foundBlankSourceLine)
    3.66 +            error("found blank source lines");
    3.67 +
    3.68 +        if (!foundNoSourceLine)
    3.69 +            error("did not find \"Source code not available\" message");
    3.70 +
    3.71 +        if (errors > 0)
    3.72 +            throw new Exception(errors + " errors occurred");
    3.73 +    }
    3.74 +
    3.75 +    void error(String msg) {
    3.76 +        System.err.println(msg);
    3.77 +        errors++;
    3.78 +    }
    3.79 +
    3.80 +    int errors;
    3.81 +}

mercurial