Fri, 17 May 2019 18:41:21 +0100
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.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Fri May 17 07:45:51 2019 +0100 1.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacProcessingEnvironment.java Fri May 17 18:41:21 2019 +0100 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -350,6 +350,7 @@ 1.11 log.error("proc.bad.config.file", sce.getLocalizedMessage()); 1.12 throw new Abort(sce); 1.13 } catch (Throwable t) { 1.14 + log.error("proc.bad.config.file", t.getLocalizedMessage()); 1.15 throw new Abort(t); 1.16 } 1.17 } 1.18 @@ -360,7 +361,14 @@ 1.19 } catch (ServiceConfigurationError sce) { 1.20 log.error("proc.bad.config.file", sce.getLocalizedMessage()); 1.21 throw new Abort(sce); 1.22 + } catch (UnsupportedClassVersionError ucve) { 1.23 + log.error("proc.cant.load.class", ucve.getLocalizedMessage()); 1.24 + throw new Abort(ucve); 1.25 + } catch (ClassFormatError cfe) { 1.26 + log.error("proc.cant.load.class", cfe.getLocalizedMessage()); 1.27 + throw new Abort(cfe); 1.28 } catch (Throwable t) { 1.29 + log.error("proc.bad.config.file", t.getLocalizedMessage()); 1.30 throw new Abort(t); 1.31 } 1.32 }
2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri May 17 07:45:51 2019 +0100 2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Fri May 17 18:41:21 2019 +0100 2.3 @@ -1,5 +1,5 @@ 2.4 # 2.5 -# Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved. 2.6 +# Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved. 2.7 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.8 # 2.9 # This code is free software; you can redistribute it and/or modify it 2.10 @@ -869,6 +869,9 @@ 2.11 compiler.err.proc.bad.config.file=\ 2.12 Bad service configuration file, or exception thrown while constructing Processor object: {0} 2.13 2.14 +compiler.err.proc.cant.load.class=\ 2.15 + Could not load processor class file due to ''{0}''. 2.16 + 2.17 compiler.err.proc.cant.create.loader=\ 2.18 Could not create class loader for annotation processors: {0} 2.19
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/annotations/8218152/MalformedAnnotationProcessorTests.java Fri May 17 18:41:21 2019 +0100 3.3 @@ -0,0 +1,158 @@ 3.4 +/* 3.5 + * Copyright (c) 2019, 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 8218152 3.30 + * @summary A bad annotation processor class file should fail with an error 3.31 + * @author Steven Groeger 3.32 + * 3.33 + * @library /tools/javac/lib 3.34 + * @build ToolBox 3.35 + * @run main MalformedAnnotationProcessorTests 3.36 + */ 3.37 + 3.38 +import java.io.*; 3.39 +import java.util.*; 3.40 +import java.io.RandomAccessFile; 3.41 +import java.nio.ByteBuffer; 3.42 +import java.nio.channels.FileChannel; 3.43 +import java.nio.file.Files; 3.44 +import java.nio.file.Path; 3.45 +import java.nio.file.Paths; 3.46 +import java.util.List; 3.47 +import javax.annotation.processing.Processor; 3.48 + 3.49 +public class MalformedAnnotationProcessorTests { 3.50 + public static void main(String... args) throws Exception { 3.51 + new MalformedAnnotationProcessorTests().run(); 3.52 + } 3.53 + 3.54 + void run() throws Exception { 3.55 + testBadAnnotationProcessor(Paths.get(".")); 3.56 + testMissingAnnotationProcessor(Paths.get(".")); 3.57 + testWrongClassFileVersion(Paths.get(".")); 3.58 + } 3.59 + 3.60 + public void testBadAnnotationProcessor(Path base) throws Exception { 3.61 + Path apDir = base.resolve("annoprocessor"); 3.62 + ToolBox.writeFile(apDir.resolve("META-INF").resolve("services") 3.63 + .resolve(Processor.class.getCanonicalName()), "BadAnnoProcessor"); 3.64 + ToolBox.writeFile(apDir.resolve("BadAnnoProcessor.class"), "badannoprocessor"); 3.65 + 3.66 + Path classes = base.resolve("classes"); 3.67 + Files.createDirectories(classes); 3.68 + 3.69 + List<String> actualErrors = new ArrayList<>(); 3.70 + ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs(); 3.71 + args.setSources("package test; public class Test {}") 3.72 + .appendArgs("-XDrawDiagnostics", 3.73 + "-d", classes.toString(), 3.74 + "-classpath", "", 3.75 + "-processorpath", apDir.toString()) 3.76 + .set(ToolBox.Expect.FAIL) 3.77 + .setErrOutput(actualErrors); 3.78 + ToolBox.javac(args); 3.79 + 3.80 + System.out.println(actualErrors.get(0)); 3.81 + if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " + 3.82 + "Incompatible magic value")) { 3.83 + throw new AssertionError("Unexpected errors reported: " + actualErrors); 3.84 + } 3.85 + } 3.86 + 3.87 + public void testMissingAnnotationProcessor(Path base) throws Exception { 3.88 + Path apDir = base.resolve("annoprocessor"); 3.89 + ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()), 3.90 + "MissingAnnoProcessor"); 3.91 + 3.92 + Path classes = base.resolve("classes"); 3.93 + Files.createDirectories(classes); 3.94 + 3.95 + List<String> actualErrors = new ArrayList<>(); 3.96 + ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs(); 3.97 + args.setSources("package test; public class Test {}") 3.98 + .appendArgs("-XDrawDiagnostics", 3.99 + "-d", classes.toString(), 3.100 + "-classpath", "", 3.101 + "-processorpath", apDir.toString()) 3.102 + .set(ToolBox.Expect.FAIL) 3.103 + .setErrOutput(actualErrors); 3.104 + ToolBox.javac(args); 3.105 + 3.106 + if (!actualErrors.get(0).contains("- compiler.err.proc.bad.config.file: " + 3.107 + "javax.annotation.processing.Processor: Provider MissingAnnoProcessor not found")) { 3.108 + throw new AssertionError("Unexpected errors reported: " + actualErrors); 3.109 + } 3.110 + } 3.111 + 3.112 + public void testWrongClassFileVersion(Path base) throws Exception { 3.113 + Path apDir = base.resolve("annoprocessor"); 3.114 + ToolBox.writeFile(apDir.resolve("META-INF").resolve("services").resolve(Processor.class.getCanonicalName()), 3.115 + "WrongClassFileVersion"); 3.116 + 3.117 + ToolBox.JavaToolArgs args = new ToolBox.JavaToolArgs(); 3.118 + args.setSources("class WrongClassFileVersion {}") 3.119 + .appendArgs("-d", apDir.toString()) 3.120 + .set(ToolBox.Expect.SUCCESS); 3.121 + ToolBox.javac(args); 3.122 + 3.123 + increaseMajor(apDir.resolve("WrongClassFileVersion.class"), 1); 3.124 + 3.125 + Path classes = base.resolve("classes"); 3.126 + Files.createDirectories(classes); 3.127 + 3.128 + List<String> actualErrors = new ArrayList<>(); 3.129 + args = new ToolBox.JavaToolArgs(); 3.130 + args.setSources("package test; public class Test {}") 3.131 + .appendArgs("-XDrawDiagnostics", 3.132 + "-d", classes.toString(), 3.133 + "-classpath", "", 3.134 + "-processorpath", apDir.toString()) 3.135 + .set(ToolBox.Expect.FAIL) 3.136 + .setErrOutput(actualErrors); 3.137 + ToolBox.javac(args); 3.138 + 3.139 + if (!actualErrors.get(0).contains("- compiler.err.proc.cant.load.class: " + 3.140 + "WrongClassFileVersion has been compiled by a more recent version")) { 3.141 + throw new AssertionError("Unexpected errors reported: " + actualErrors); 3.142 + } 3.143 + } 3.144 + 3.145 + // Increase class file cfile's major version by delta 3.146 + // (note: based on test/langtools/tools/javac/6330997/T6330997.java) 3.147 + static void increaseMajor(Path cfile, int delta) { 3.148 + try (RandomAccessFile cls = 3.149 + new RandomAccessFile(cfile.toFile(), "rw"); 3.150 + FileChannel fc = cls.getChannel()) { 3.151 + ByteBuffer rbuf = ByteBuffer.allocate(2); 3.152 + fc.read(rbuf, 6); 3.153 + ByteBuffer wbuf = ByteBuffer.allocate(2); 3.154 + wbuf.putShort(0, (short)(rbuf.getShort(0) + delta)); 3.155 + fc.write(wbuf, 6); 3.156 + fc.force(false); 3.157 + } catch (Exception e){ 3.158 + throw new RuntimeException("Failed: unexpected exception"); 3.159 + } 3.160 + } 3.161 +}
4.1 --- a/test/tools/javac/diags/examples.not-yet.txt Fri May 17 07:45:51 2019 +0100 4.2 +++ b/test/tools/javac/diags/examples.not-yet.txt Fri May 17 18:41:21 2019 +0100 4.3 @@ -32,6 +32,7 @@ 4.4 compiler.err.proc.no.service # JavacProcessingEnvironment: no service loader available 4.5 compiler.err.proc.processor.bad.option.name # cannot happen? masked by javac.err.invalid.A.key 4.6 compiler.err.proc.service.problem # JavacProcessingEnvironment: catch Throwable from service loader 4.7 +compiler.err.proc.cant.load.class # JavacProcessingEnvironment: cant load the class/jar file 4.8 compiler.err.signature.doesnt.match.intf # UNUSED 4.9 compiler.err.signature.doesnt.match.supertype # UNUSED 4.10 compiler.err.source.cant.overwrite.input.file