8007907: javap, method com.sun.tools.javap.Main.run returns 0 even in case of class not found error

Tue, 11 Jun 2013 09:59:34 +0100

author
vromero
date
Tue, 11 Jun 2013 09:59:34 +0100
changeset 1819
7fe655cad9b1
parent 1818
bbedff0dc37e
child 1820
6b48ebae2569

8007907: javap, method com.sun.tools.javap.Main.run returns 0 even in case of class not found error
Reviewed-by: jjg

src/share/classes/com/sun/tools/javap/JavapTask.java file | annotate | diff | comparison | revisions
test/tools/javac/constDebug/ConstDebugTest.java file | annotate | diff | comparison | revisions
test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java file | annotate | diff | comparison | revisions
test/tools/javap/8007907/JavapReturns0AfterClassNotFoundTest.java file | annotate | diff | comparison | revisions
test/tools/javap/T4777949.java file | annotate | diff | comparison | revisions
test/tools/javap/T7190862.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Jun 11 09:35:58 2013 +0100
     1.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Jun 11 09:59:34 2013 +0100
     1.3 @@ -452,8 +452,7 @@
     1.4              }
     1.5  
     1.6              try {
     1.7 -                boolean ok = run();
     1.8 -                return ok ? EXIT_OK : EXIT_ERROR;
     1.9 +                return run();
    1.10              } finally {
    1.11                  if (defaultFileManager != null) {
    1.12                      try {
    1.13 @@ -569,12 +568,13 @@
    1.14      }
    1.15  
    1.16      public Boolean call() {
    1.17 -        return run();
    1.18 +        return run() == 0;
    1.19      }
    1.20  
    1.21 -    public boolean run() {
    1.22 -        if (classes == null || classes.size() == 0)
    1.23 -            return false;
    1.24 +    public int run() {
    1.25 +        if (classes == null || classes.isEmpty()) {
    1.26 +            return EXIT_ERROR;
    1.27 +        }
    1.28  
    1.29          context.put(PrintWriter.class, log);
    1.30          ClassWriter classWriter = ClassWriter.instance(context);
    1.31 @@ -583,54 +583,55 @@
    1.32  
    1.33          attributeFactory.setCompat(options.compat);
    1.34  
    1.35 -        boolean ok = true;
    1.36 +        int result = EXIT_OK;
    1.37  
    1.38          for (String className: classes) {
    1.39 -            JavaFileObject fo;
    1.40              try {
    1.41 -                writeClass(classWriter, className);
    1.42 +                result = writeClass(classWriter, className);
    1.43              } catch (ConstantPoolException e) {
    1.44                  reportError("err.bad.constant.pool", className, e.getLocalizedMessage());
    1.45 -                ok = false;
    1.46 +                result = EXIT_ERROR;
    1.47              } catch (EOFException e) {
    1.48                  reportError("err.end.of.file", className);
    1.49 -                ok = false;
    1.50 +                result = EXIT_ERROR;
    1.51              } catch (FileNotFoundException e) {
    1.52                  reportError("err.file.not.found", e.getLocalizedMessage());
    1.53 -                ok = false;
    1.54 +                result = EXIT_ERROR;
    1.55              } catch (IOException e) {
    1.56                  //e.printStackTrace();
    1.57                  Object msg = e.getLocalizedMessage();
    1.58 -                if (msg == null)
    1.59 +                if (msg == null) {
    1.60                      msg = e;
    1.61 +                }
    1.62                  reportError("err.ioerror", className, msg);
    1.63 -                ok = false;
    1.64 +                result = EXIT_ERROR;
    1.65              } catch (Throwable t) {
    1.66                  StringWriter sw = new StringWriter();
    1.67                  PrintWriter pw = new PrintWriter(sw);
    1.68                  t.printStackTrace(pw);
    1.69                  pw.close();
    1.70                  reportError("err.crash", t.toString(), sw.toString());
    1.71 -                ok = false;
    1.72 +                result = EXIT_ABNORMAL;
    1.73              }
    1.74          }
    1.75  
    1.76 -        return ok;
    1.77 +        return result;
    1.78      }
    1.79  
    1.80 -    protected boolean writeClass(ClassWriter classWriter, String className)
    1.81 +    protected int writeClass(ClassWriter classWriter, String className)
    1.82              throws IOException, ConstantPoolException {
    1.83          JavaFileObject fo = open(className);
    1.84          if (fo == null) {
    1.85              reportError("err.class.not.found", className);
    1.86 -            return false;
    1.87 +            return EXIT_ERROR;
    1.88          }
    1.89  
    1.90          ClassFileInfo cfInfo = read(fo);
    1.91          if (!className.endsWith(".class")) {
    1.92              String cfName = cfInfo.cf.getName();
    1.93 -            if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", ".")))
    1.94 +            if (!cfName.replaceAll("[/$]", ".").equals(className.replaceAll("[/$]", "."))) {
    1.95                  reportWarning("warn.unexpected.class", className, cfName.replace('/', '.'));
    1.96 +            }
    1.97          }
    1.98          write(cfInfo);
    1.99  
   1.100 @@ -640,7 +641,7 @@
   1.101              if (a instanceof InnerClasses_attribute) {
   1.102                  InnerClasses_attribute inners = (InnerClasses_attribute) a;
   1.103                  try {
   1.104 -                    boolean ok = true;
   1.105 +                    int result = EXIT_OK;
   1.106                      for (int i = 0; i < inners.classes.length; i++) {
   1.107                          int outerIndex = inners.classes[i].outer_class_info_index;
   1.108                          ConstantPool.CONSTANT_Class_info outerClassInfo = cf.constant_pool.getClassInfo(outerIndex);
   1.109 @@ -651,21 +652,22 @@
   1.110                              String innerClassName = innerClassInfo.getName();
   1.111                              classWriter.println("// inner class " + innerClassName.replaceAll("[/$]", "."));
   1.112                              classWriter.println();
   1.113 -                            ok = ok & writeClass(classWriter, innerClassName);
   1.114 +                            result = writeClass(classWriter, innerClassName);
   1.115 +                            if (result != EXIT_OK) return result;
   1.116                          }
   1.117                      }
   1.118 -                    return ok;
   1.119 +                    return result;
   1.120                  } catch (ConstantPoolException e) {
   1.121                      reportError("err.bad.innerclasses.attribute", className);
   1.122 -                    return false;
   1.123 +                    return EXIT_ERROR;
   1.124                  }
   1.125              } else if (a != null) {
   1.126                  reportError("err.bad.innerclasses.attribute", className);
   1.127 -                return false;
   1.128 +                return EXIT_ERROR;
   1.129              }
   1.130          }
   1.131  
   1.132 -        return true;
   1.133 +        return EXIT_OK;
   1.134      }
   1.135  
   1.136      protected JavaFileObject open(String className) throws IOException {
     2.1 --- a/test/tools/javac/constDebug/ConstDebugTest.java	Tue Jun 11 09:35:58 2013 +0100
     2.2 +++ b/test/tools/javac/constDebug/ConstDebugTest.java	Tue Jun 11 09:59:34 2013 +0100
     2.3 @@ -25,26 +25,25 @@
     2.4   * @test
     2.5   * @bug 4645152 4785453
     2.6   * @summary javac compiler incorrectly inserts <clinit> when -g is specified
     2.7 - * @library /tools/javac/lib
     2.8 - * @build ToolBox
     2.9   * @run compile -g ConstDebugTest.java
    2.10   * @run main ConstDebugTest
    2.11   */
    2.12 +import java.nio.file.Paths;
    2.13 +import com.sun.tools.classfile.ClassFile;
    2.14 +import com.sun.tools.classfile.Method;
    2.15  
    2.16 -//original test: test/tools/javac/constDebug/ConstDebug.sh
    2.17  public class ConstDebugTest {
    2.18  
    2.19      public static final long l = 12;
    2.20  
    2.21      public static void main(String args[]) throws Exception {
    2.22 -//        "${TESTJAVA}${FS}bin${FS}javac" ${TESTTOOLVMOPTS} -g -d . -classpath .${PS}${TESTSRC} $1.java 2> ${TMP1}
    2.23 -//        if "${TESTJAVA}${FS}bin${FS}javap" $1.class | grep clinit; then fail
    2.24 -        ToolBox.JavaToolArgs javapArgs =
    2.25 -                new ToolBox.JavaToolArgs().setAllArgs("-v",
    2.26 -                "-classpath", System.getProperty("test.classes"), "ConstDebugTest.class");
    2.27 -        if (ToolBox.javap(javapArgs).contains("clinit")) {
    2.28 -            throw new AssertionError(
    2.29 -                "javac should not create a <clinit> method for ConstDebugTest class");
    2.30 +        ClassFile classFile = ClassFile.read(Paths.get(System.getProperty("test.classes"),
    2.31 +                ConstDebugTest.class.getSimpleName() + ".class"));
    2.32 +        for (Method method: classFile.methods) {
    2.33 +            if (method.getName(classFile.constant_pool).equals("<clinit>")) {
    2.34 +                throw new AssertionError(
    2.35 +                    "javac should not create a <clinit> method for ConstDebugTest class");
    2.36 +            }
    2.37          }
    2.38      }
    2.39  
     3.1 --- a/test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java	Tue Jun 11 09:35:58 2013 +0100
     3.2 +++ b/test/tools/javap/8006334/JavapTaskCtorFailWithNPE.java	Tue Jun 11 09:59:34 2013 +0100
     3.3 @@ -66,8 +66,7 @@
     3.4          JavaFileManager fm = JavapFileManager.create(dc, pw);
     3.5          JavapTask t = new JavapTask(pw, fm, dc, null,
     3.6                  Arrays.asList(classToCheck.getPath()));
     3.7 -        boolean ok = t.run();
     3.8 -        if (!ok)
     3.9 +        if (t.run() != 0)
    3.10              throw new Error("javap failed unexpectedly");
    3.11  
    3.12          List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/test/tools/javap/8007907/JavapReturns0AfterClassNotFoundTest.java	Tue Jun 11 09:59:34 2013 +0100
     4.3 @@ -0,0 +1,75 @@
     4.4 +/*
     4.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4.7 + *
     4.8 + * This code is free software; you can redistribute it and/or modify it
     4.9 + * under the terms of the GNU General Public License version 2 only, as
    4.10 + * published by the Free Software Foundation.
    4.11 + *
    4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    4.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    4.15 + * version 2 for more details (a copy is included in the LICENSE file that
    4.16 + * accompanied this code).
    4.17 + *
    4.18 + * You should have received a copy of the GNU General Public License version
    4.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    4.21 + *
    4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    4.23 + * or visit www.oracle.com if you need additional information or have any
    4.24 + * questions.
    4.25 + */
    4.26 +
    4.27 +/*
    4.28 + * @test
    4.29 + * @bug 8007907
    4.30 + * @summary javap, method com.sun.tools.javap.Main.run returns 0 even in case
    4.31 + * of class not found error
    4.32 + */
    4.33 +
    4.34 +import java.io.IOException;
    4.35 +import java.io.PrintWriter;
    4.36 +import java.io.StringWriter;
    4.37 +
    4.38 +public class JavapReturns0AfterClassNotFoundTest {
    4.39 +
    4.40 +    static final String fileNotFoundErrorMsg =
    4.41 +            "Error:  class not found: Unexisting.class";
    4.42 +    static final String exitCodeClassNotFoundAssertionMsg =
    4.43 +            "Javap's exit code for class not found should be 1";
    4.44 +    static final String classNotFoundMsgAssertionMsg =
    4.45 +            "Javap's error message for class not found is incorrect";
    4.46 +
    4.47 +    public static void main(String args[]) throws Exception {
    4.48 +        new JavapReturns0AfterClassNotFoundTest().run();
    4.49 +    }
    4.50 +
    4.51 +    void run() throws IOException {
    4.52 +        check(exitCodeClassNotFoundAssertionMsg, classNotFoundMsgAssertionMsg,
    4.53 +                fileNotFoundErrorMsg, "-v", "Unexisting.class");
    4.54 +    }
    4.55 +
    4.56 +    void check(String exitCodeAssertionMsg, String errMsgAssertionMsg,
    4.57 +            String expectedErrMsg, String... params) {
    4.58 +        int result;
    4.59 +        StringWriter s;
    4.60 +        String out;
    4.61 +        try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
    4.62 +            result = com.sun.tools.javap.Main.run(params, pw);
    4.63 +            //no line separator, no problem
    4.64 +            out = s.toString().trim();
    4.65 +        }
    4.66 +        if (result != 1) {
    4.67 +            System.out.println("actual exit code " + result);
    4.68 +            throw new AssertionError(exitCodeAssertionMsg);
    4.69 +        }
    4.70 +
    4.71 +        if (!out.equals(expectedErrMsg)) {
    4.72 +            System.out.println("actual " + out);
    4.73 +            System.out.println("expected " + expectedErrMsg);
    4.74 +            throw new AssertionError(errMsgAssertionMsg);
    4.75 +        }
    4.76 +    }
    4.77 +
    4.78 +}
     5.1 --- a/test/tools/javap/T4777949.java	Tue Jun 11 09:35:58 2013 +0100
     5.2 +++ b/test/tools/javap/T4777949.java	Tue Jun 11 09:59:34 2013 +0100
     5.3 @@ -1,5 +1,5 @@
     5.4  /*
     5.5 - * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
     5.6 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     5.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.8   *
     5.9   * This code is free software; you can redistribute it and/or modify it
    5.10 @@ -87,11 +87,11 @@
    5.11          PrintWriter pw = new PrintWriter(sw);
    5.12          JavaFileManager fm = JavapFileManager.create(dc, pw);
    5.13          JavapTask t = new JavapTask(pw, fm, dc, args, classes);
    5.14 -        boolean ok = t.run();
    5.15 +        int ok = t.run();
    5.16  
    5.17          List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();
    5.18  
    5.19 -        if (!ok)
    5.20 +        if (ok != 0)
    5.21              error("javap failed unexpectedly");
    5.22  
    5.23          System.err.println("args=" + args + " classes=" + classes + "\n"
     6.1 --- a/test/tools/javap/T7190862.java	Tue Jun 11 09:35:58 2013 +0100
     6.2 +++ b/test/tools/javap/T7190862.java	Tue Jun 11 09:59:34 2013 +0100
     6.3 @@ -96,8 +96,7 @@
     6.4          PrintWriter pw = new PrintWriter(sw);
     6.5          JavaFileManager fm = JavapFileManager.create(dc, pw);
     6.6          JavapTask t = new JavapTask(pw, fm, dc, args, classes);
     6.7 -        boolean ok = t.run();
     6.8 -        if (!ok)
     6.9 +        if (t.run() != 0)
    6.10              throw new Error("javap failed unexpectedly");
    6.11  
    6.12          List<Diagnostic<? extends JavaFileObject>> diags = dc.getDiagnostics();

mercurial