test/tools/javac/6341866/T6341866.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 798
4868a36f6fd8
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * @test
aoqi@0 26 * @bug 6341866
aoqi@0 27 * @summary Source files loaded from source path are not subject to annotation processing
aoqi@0 28 * @build Anno T6341866
aoqi@0 29 * @run main T6341866
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.*;
aoqi@0 33 import java.util.*;
aoqi@0 34 import javax.annotation.processing.*;
aoqi@0 35 import javax.tools.*;
aoqi@0 36
aoqi@0 37 /**
aoqi@0 38 * For each of a number of implicit compilation scenarios,
aoqi@0 39 * and for each of a set of annotation processing scenarios,
aoqi@0 40 * verify that a class file is generated, or not, for an
aoqi@0 41 * implicitly compiled source file and that the correct
aoqi@0 42 * warning message is given for implicitly compiled files
aoqi@0 43 * when annotation processing.
aoqi@0 44 */
aoqi@0 45 public class T6341866 {
aoqi@0 46 static final String testSrc = System.getProperty("test.src", ".");
aoqi@0 47 static final String testClasses = System.getProperty("test.classes", ".");
aoqi@0 48 static final File a_java = new File(testSrc, "A.java");
aoqi@0 49 static final File a_class = new File("A.class");
aoqi@0 50 static final File b_java = new File(testSrc, "B.java");
aoqi@0 51 static final File b_class = new File("B.class");
aoqi@0 52 static final File processorServices = services(Processor.class);
aoqi@0 53
aoqi@0 54 enum ImplicitType {
aoqi@0 55 NONE(null), // don't use implicit compilation
aoqi@0 56 OPT_UNSET(null), // implicit compilation, but no -implicit option
aoqi@0 57 OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none
aoqi@0 58 OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class
aoqi@0 59
aoqi@0 60 ImplicitType(String opt) {
aoqi@0 61 this.opt = opt;
aoqi@0 62 }
aoqi@0 63 final String opt;
aoqi@0 64 };
aoqi@0 65
aoqi@0 66 enum AnnoType {
aoqi@0 67 NONE, // no annotation processing
aoqi@0 68 SERVICE, // implicit annotation processing, via ServiceLoader
aoqi@0 69 SPECIFY // explicit annotation processing
aoqi@0 70 };
aoqi@0 71
aoqi@0 72
aoqi@0 73 public static void main(String ... args) throws Exception {
aoqi@0 74 boolean ok = true;
aoqi@0 75
aoqi@0 76 // iterate over all combinations
aoqi@0 77 for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) {
aoqi@0 78 for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) {
aoqi@0 79 ok &= test(implicitType, annoType);
aoqi@0 80 }
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 if (!ok)
aoqi@0 84 throw new AssertionError("test failed");
aoqi@0 85 }
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * Verify that a class file is generated, or not, for an implicitly compiled source file,
aoqi@0 89 * and that the correct warning message is given for implicitly compiled files when annotation processing.
aoqi@0 90 */
aoqi@0 91 static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException {
aoqi@0 92 System.err.println("test implicit=" + implicitType + " anno=" + annoType);
aoqi@0 93
aoqi@0 94 // ensure clean start
aoqi@0 95 a_class.delete();
aoqi@0 96 b_class.delete();
aoqi@0 97 processorServices.delete();
aoqi@0 98
aoqi@0 99 List<String> opts = new ArrayList<String>();
aoqi@0 100 opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-source", "1.6", "-Xlint:-options"));
aoqi@0 101 if (implicitType.opt != null)
aoqi@0 102 opts.add(implicitType.opt);
aoqi@0 103
aoqi@0 104 switch (annoType) {
aoqi@0 105 case SERVICE:
aoqi@0 106 createProcessorServices(Anno.class.getName());
aoqi@0 107 break;
aoqi@0 108 case SPECIFY:
aoqi@0 109 opts.addAll(Arrays.asList("-processor", Anno.class.getName()));
aoqi@0 110 break;
aoqi@0 111 }
aoqi@0 112
aoqi@0 113
aoqi@0 114 JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
aoqi@0 115 MyDiagListener dl = new MyDiagListener();
aoqi@0 116 StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null);
aoqi@0 117
aoqi@0 118 // Note: class A references class B, so compile A if we want implicit compilation
aoqi@0 119 File file = (implicitType != ImplicitType.NONE) ? a_java : b_java;
aoqi@0 120 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);
aoqi@0 121
aoqi@0 122 //System.err.println("compile: " + opts + " " + files);
aoqi@0 123
aoqi@0 124 boolean ok = javac.getTask(null, fm, dl, opts, null, files).call();
aoqi@0 125 if (!ok) {
aoqi@0 126 error("compilation failed");
aoqi@0 127 return false;
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 // check implicit compilation results if necessary
aoqi@0 131 if (implicitType != ImplicitType.NONE) {
aoqi@0 132 boolean expectClass = (implicitType != ImplicitType.OPT_NONE);
aoqi@0 133 if (b_class.exists() != expectClass) {
aoqi@0 134 if (b_class.exists())
aoqi@0 135 error("B implicitly compiled unexpectedly");
aoqi@0 136 else
aoqi@0 137 error("B not impliictly compiled");
aoqi@0 138 return false;
aoqi@0 139 }
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 // check message key results
aoqi@0 143 String expectKey = null;
aoqi@0 144 if (implicitType == ImplicitType.OPT_UNSET) {
aoqi@0 145 switch (annoType) {
aoqi@0 146 case SERVICE:
aoqi@0 147 expectKey = "compiler.warn.proc.use.proc.or.implicit";
aoqi@0 148 break;
aoqi@0 149 case SPECIFY:
aoqi@0 150 expectKey = "compiler.warn.proc.use.implicit";
aoqi@0 151 break;
aoqi@0 152 }
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 if (expectKey == null) {
aoqi@0 156 if (dl.diagCodes.size() != 0) {
aoqi@0 157 error("no diagnostics expected");
aoqi@0 158 return false;
aoqi@0 159 }
aoqi@0 160 } else {
aoqi@0 161 if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) {
aoqi@0 162 error("unexpected diagnostics generated");
aoqi@0 163 return false;
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 return true;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 static void createProcessorServices(String name) throws IOException {
aoqi@0 171 processorServices.getParentFile().mkdirs();
aoqi@0 172
aoqi@0 173 BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));
aoqi@0 174 out.write(name);
aoqi@0 175 out.newLine();
aoqi@0 176 out.close();
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 static class MyDiagListener implements DiagnosticListener<JavaFileObject> {
aoqi@0 180 public void report(Diagnostic d) {
aoqi@0 181 diagCodes.add(d.getCode());
aoqi@0 182 System.err.println(d);
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 List<String> diagCodes = new ArrayList<String>();
aoqi@0 186 }
aoqi@0 187
aoqi@0 188 static void error(String msg) {
aoqi@0 189 System.err.println("ERROR: " + msg);
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 static File services(Class<?> service) {
aoqi@0 193 String[] dirs = { testClasses, "META-INF", "services" };
aoqi@0 194 File dir = null;
aoqi@0 195 for (String d: dirs)
aoqi@0 196 dir = (dir == null ? new File(d) : new File(dir, d));
aoqi@0 197
aoqi@0 198 return new File(dir, service.getName());
aoqi@0 199 }
aoqi@0 200 }

mercurial