test/tools/javac/annotations/8218152/MalformedAnnotationProcessorTests.java

Fri, 17 May 2019 18:41:21 +0100

author
sgroeger
date
Fri, 17 May 2019 18:41:21 +0100
changeset 3814
12045c5cae33
permissions
-rw-r--r--

8218152: [javac] fails and exits with no error if a bad annotation processor provided
Summary: Handle exceptions thrown while loading annotation processors.
Reviewed-by: jlahoda, andrew

     1 /*
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8218152
    27  * @summary A bad annotation processor class file should fail with an error
    28  * @author Steven Groeger
    29  *
    30  * @library /tools/javac/lib
    31  * @build ToolBox
    32  * @run main MalformedAnnotationProcessorTests
    33  */
    35 import java.io.*;
    36 import java.util.*;
    37 import java.io.RandomAccessFile;
    38 import java.nio.ByteBuffer;
    39 import java.nio.channels.FileChannel;
    40 import java.nio.file.Files;
    41 import java.nio.file.Path;
    42 import java.nio.file.Paths;
    43 import java.util.List;
    44 import javax.annotation.processing.Processor;
    46 public class MalformedAnnotationProcessorTests {
    47     public static void main(String... args) throws Exception {
    48         new MalformedAnnotationProcessorTests().run();
    49     }
    51     void run() throws Exception {
    52         testBadAnnotationProcessor(Paths.get("."));
    53         testMissingAnnotationProcessor(Paths.get("."));
    54         testWrongClassFileVersion(Paths.get("."));
    55     }
    57     public void testBadAnnotationProcessor(Path base) throws Exception {
    58         Path apDir = base.resolve("annoprocessor");
    59         ToolBox.writeFile(apDir.resolve("META-INF").resolve("services")
    60                           .resolve(Processor.class.getCanonicalName()), "BadAnnoProcessor");
    61         ToolBox.writeFile(apDir.resolve("BadAnnoProcessor.class"), "badannoprocessor");
    63         Path classes = base.resolve("classes");
    64         Files.createDirectories(classes);
    66         List<String> actualErrors = new ArrayList<>();
    67         ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
    68         args.setSources("package test; public class Test {}")
    69             .appendArgs("-XDrawDiagnostics",
    70                         "-d", classes.toString(),
    71                         "-classpath", "",
    72                         "-processorpath", apDir.toString())
    73             .set(ToolBox.Expect.FAIL)
    74             .setErrOutput(actualErrors);
    75         ToolBox.javac(args);
    77         System.out.println(actualErrors.get(0));
    78         if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
    79                                           "Incompatible magic value")) {
    80             throw new AssertionError("Unexpected errors reported: " + actualErrors);
    81         }
    82     }
    84     public void testMissingAnnotationProcessor(Path base) throws Exception {
    85         Path apDir = base.resolve("annoprocessor");
    86         ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
    87                      "MissingAnnoProcessor");
    89         Path classes = base.resolve("classes");
    90         Files.createDirectories(classes);
    92         List<String> actualErrors = new ArrayList<>();
    93         ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
    94         args.setSources("package test; public class Test {}")
    95             .appendArgs("-XDrawDiagnostics",
    96                         "-d", classes.toString(),
    97                         "-classpath", "",
    98                         "-processorpath", apDir.toString())
    99             .set(ToolBox.Expect.FAIL)
   100             .setErrOutput(actualErrors);
   101         ToolBox.javac(args);
   103         if (!actualErrors.get(0).contains("- compiler.err.proc.bad.config.file: " +
   104             "javax.annotation.processing.Processor: Provider MissingAnnoProcessor not found")) {
   105             throw new AssertionError("Unexpected errors reported: " + actualErrors);
   106         }
   107     }
   109     public void testWrongClassFileVersion(Path base) throws Exception {
   110         Path apDir = base.resolve("annoprocessor");
   111         ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()),
   112                      "WrongClassFileVersion");
   114         ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs();
   115         args.setSources("class WrongClassFileVersion {}")
   116             .appendArgs("-d", apDir.toString())
   117             .set(ToolBox.Expect.SUCCESS);
   118         ToolBox.javac(args);
   120         increaseMajor(apDir.resolve("WrongClassFileVersion.class"), 1);
   122         Path classes = base.resolve("classes");
   123         Files.createDirectories(classes);
   125         List<String> actualErrors = new ArrayList<>();
   126         args = new ToolBox.JavaToolArgs();
   127         args.setSources("package test; public class Test {}")
   128             .appendArgs("-XDrawDiagnostics",
   129                         "-d", classes.toString(),
   130                         "-classpath", "",
   131                         "-processorpath", apDir.toString())
   132             .set(ToolBox.Expect.FAIL)
   133             .setErrOutput(actualErrors);
   134         ToolBox.javac(args);
   136         if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " +
   137             "WrongClassFileVersion has been compiled by a more recent version")) {
   138             throw new AssertionError("Unexpected errors reported: " + actualErrors);
   139         }
   140     }
   142     // Increase class file cfile's major version by delta
   143     // (note: based on test/langtools/tools/javac/6330997/T6330997.java)
   144     static void increaseMajor(Path cfile, int delta) {
   145         try (RandomAccessFile cls =
   146              new RandomAccessFile(cfile.toFile(), "rw");
   147              FileChannel fc = cls.getChannel()) {
   148             ByteBuffer rbuf = ByteBuffer.allocate(2);
   149             fc.read(rbuf, 6);
   150             ByteBuffer wbuf = ByteBuffer.allocate(2);
   151             wbuf.putShort(0, (short)(rbuf.getShort(0) + delta));
   152             fc.write(wbuf, 6);
   153             fc.force(false);
   154         } catch (Exception e){
   155             throw new RuntimeException("Failed: unexpected exception");
   156         }
   157      }
   158 }

mercurial