8022163: javac exits with 0 status and no messages on error to construct an ann-procesor

Fri, 04 Oct 2013 13:59:13 -0700

author
jjg
date
Fri, 04 Oct 2013 13:59:13 -0700
changeset 2086
2fa6ced325cc
parent 2085
3344ea7404b1
child 2087
515d54c1b063

8022163: javac exits with 0 status and no messages on error to construct an ann-procesor
Reviewed-by: darcy

src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java file | annotate | diff | comparison | revisions
test/tools/javac/processing/errors/TestBadProcessor.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Oct 04 13:41:13 2013 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java	Fri Oct 04 13:59:13 2013 -0700
     1.3 @@ -1034,6 +1034,13 @@
     1.4                  } else {
     1.5                      discoverAndRunProcs(context, annotationsPresent, topLevelClasses, packageInfoFiles);
     1.6                  }
     1.7 +            } catch (Throwable t) {
     1.8 +                // we're specifically expecting Abort here, but if any Throwable
     1.9 +                // comes by, we should flush all deferred diagnostics, rather than
    1.10 +                // drop them on the ground.
    1.11 +                deferredDiagnosticHandler.reportDeferredDiagnostics();
    1.12 +                log.popDiagnosticHandler(deferredDiagnosticHandler);
    1.13 +                throw t;
    1.14              } finally {
    1.15                  if (!taskListener.isEmpty())
    1.16                      taskListener.finished(new TaskEvent(TaskEvent.Kind.ANNOTATION_PROCESSING_ROUND));
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/processing/errors/TestBadProcessor.java	Fri Oct 04 13:59:13 2013 -0700
     2.3 @@ -0,0 +1,119 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8022163
    2.30 + * @summary javac exits with 0 status and no messages on error to construct an ann-procesor
    2.31 + */
    2.32 +
    2.33 +import java.io.*;
    2.34 +
    2.35 +public class TestBadProcessor {
    2.36 +    public static void main(String... args) throws Exception {
    2.37 +        new TestBadProcessor().run();
    2.38 +    }
    2.39 +
    2.40 +    public static final String badAnnoProcSrc =
    2.41 +        "import java.util.*;\n" +
    2.42 +        "import javax.annotation.processing.*;\n" +
    2.43 +        "import javax.lang.model.element.*;\n" +
    2.44 +
    2.45 +        "public class AnnoProc extends AbstractProcessor {\n" +
    2.46 +        "    public AnnoProc() {\n" +
    2.47 +        "        throw new Error();\n" +
    2.48 +        "    }\n" +
    2.49 +
    2.50 +        "    public boolean process(Set<? extends TypeElement> elems, \n" +
    2.51 +        "                        RoundEnvironment rEnv) {\n" +
    2.52 +        "        return false;\n" +
    2.53 +        "    }\n" +
    2.54 +        "}\n";
    2.55 +
    2.56 +    public void run() throws Exception {
    2.57 +        // setup
    2.58 +        File srcDir = new File("src");
    2.59 +        File classesDir = new File("classes");
    2.60 +        classesDir.mkdirs();
    2.61 +        File srcFile = writeFile(srcDir, "AnnoProc.java", badAnnoProcSrc);
    2.62 +        compile("-d", classesDir.getPath(), srcFile.getPath());
    2.63 +        writeFile(classesDir, "META-INF/services/javax.annotation.processing.Processor", "AnnoProc");
    2.64 +
    2.65 +        // run the primary compilation
    2.66 +        int rc;
    2.67 +        StringWriter sw = new StringWriter();
    2.68 +        try (PrintWriter pw = new PrintWriter(sw)) {
    2.69 +            String[] args = { "-processorpath", classesDir.getPath(), srcFile.getPath() };
    2.70 +            rc = com.sun.tools.javac.Main.compile(args, pw);
    2.71 +        }
    2.72 +
    2.73 +        // verify that it failed as expected, with the expected message
    2.74 +        String out = sw.toString();
    2.75 +        System.err.println(out);
    2.76 +        String expect = "error: Bad service configuration file, " +
    2.77 +                "or exception thrown while constructing Processor object: " +
    2.78 +                "javax.annotation.processing.Processor: " +
    2.79 +                "Provider AnnoProc could not be instantiated: java.lang.Error";
    2.80 +        if (!out.trim().equals(expect)) {
    2.81 +            System.err.println("expected: " + expect);
    2.82 +            error("output not as expected");
    2.83 +        }
    2.84 +
    2.85 +        if (rc == 0) {
    2.86 +            error("unexpected exit code: " + rc + "; expected: not zero");
    2.87 +        }
    2.88 +
    2.89 +        // summary
    2.90 +        if (errors > 0)
    2.91 +            throw new Exception(errors + " errors found");
    2.92 +    }
    2.93 +
    2.94 +    void compile(String... args) throws Exception {
    2.95 +        int rc;
    2.96 +        StringWriter sw = new StringWriter();
    2.97 +        try (PrintWriter pw = new PrintWriter(sw)) {
    2.98 +            rc = com.sun.tools.javac.Main.compile(args, pw);
    2.99 +        }
   2.100 +        String out = sw.toString();
   2.101 +        if (!out.isEmpty())
   2.102 +            System.err.println(out);
   2.103 +        if (rc != 0)
   2.104 +            throw new Exception("compilation failed");
   2.105 +    }
   2.106 +
   2.107 +    File writeFile(File dir, String path, String body) throws IOException {
   2.108 +        File f = new File(dir, path);
   2.109 +        f.getParentFile().mkdirs();
   2.110 +        try (FileWriter out = new FileWriter(f)) {
   2.111 +            out.write(body);
   2.112 +        }
   2.113 +        return f;
   2.114 +    }
   2.115 +
   2.116 +    void error(String msg) {
   2.117 +        System.err.println("Error: " + msg);
   2.118 +        errors++;
   2.119 +    }
   2.120 +
   2.121 +    int errors;
   2.122 +}

mercurial