test/tools/javac/6341866/T6341866.java

Tue, 11 Mar 2008 13:14:55 -0700

author
jjg
date
Tue, 11 Mar 2008 13:14:55 -0700
changeset 11
b66d15dfd001
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
permissions
-rw-r--r--

6307187: clean up code for -Xlint:options
Summary: introduce common code for handling one-of and any-of options
Reviewed-by: mcimadamore

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

mercurial