Path: blob/master/test/langtools/tools/javac/6341866/T6341866.java
41152 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 634186626* @summary Source files loaded from source path are not subject to annotation processing27* @modules java.compiler28* jdk.compiler29* @build Anno T634186630* @run main T634186631*/3233import java.io.*;34import java.util.*;35import javax.annotation.processing.*;36import javax.tools.*;3738/**39* For each of a number of implicit compilation scenarios,40* and for each of a set of annotation processing scenarios,41* verify that a class file is generated, or not, for an42* implicitly compiled source file and that the correct43* warning message is given for implicitly compiled files44* when annotation processing.45*/46public class T6341866 {47static final String testSrc = System.getProperty("test.src", ".");48static final String testClasses = System.getProperty("test.classes", ".");49static final File a_java = new File(testSrc, "A.java");50static final File a_class = new File("A.class");51static final File b_java = new File(testSrc, "B.java");52static final File b_class = new File("B.class");53static final File processorServices = services(Processor.class);5455enum ImplicitType {56NONE(null), // don't use implicit compilation57OPT_UNSET(null), // implicit compilation, but no -implicit option58OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none59OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class6061ImplicitType(String opt) {62this.opt = opt;63}64final String opt;65};6667enum AnnoType {68NONE, // no annotation processing69SERVICE, // implicit annotation processing, via ServiceLoader70SPECIFY // explicit annotation processing71};727374public static void main(String ... args) throws Exception {75boolean ok = true;7677// iterate over all combinations78for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) {79for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) {80ok &= test(implicitType, annoType);81}82}8384if (!ok)85throw new AssertionError("test failed");86}8788/**89* Verify that a class file is generated, or not, for an implicitly compiled source file,90* and that the correct warning message is given for implicitly compiled files when annotation processing.91*/92static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException {93System.err.println("test implicit=" + implicitType + " anno=" + annoType);9495// ensure clean start96a_class.delete();97b_class.delete();98processorServices.delete();99100List<String> opts = new ArrayList<String>();101opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-Xlint:-options"));102if (implicitType.opt != null)103opts.add(implicitType.opt);104105switch (annoType) {106case SERVICE:107createProcessorServices(Anno.class.getName());108break;109case SPECIFY:110opts.addAll(Arrays.asList("-processor", Anno.class.getName()));111break;112}113114115JavaCompiler javac = ToolProvider.getSystemJavaCompiler();116MyDiagListener dl = new MyDiagListener();117try (StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null)) {118119// Note: class A references class B, so compile A if we want implicit compilation120File file = (implicitType != ImplicitType.NONE) ? a_java : b_java;121Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);122123//System.err.println("compile: " + opts + " " + files);124125boolean ok = javac.getTask(null, fm, dl, opts, null, files).call();126if (!ok) {127error("compilation failed");128return false;129}130131// check implicit compilation results if necessary132if (implicitType != ImplicitType.NONE) {133boolean expectClass = (implicitType != ImplicitType.OPT_NONE);134if (b_class.exists() != expectClass) {135if (b_class.exists())136error("B implicitly compiled unexpectedly");137else138error("B not impliictly compiled");139return false;140}141}142143// check message key results144String expectKey = null;145if (implicitType == ImplicitType.OPT_UNSET) {146switch (annoType) {147case SERVICE:148expectKey = "compiler.warn.proc.use.proc.or.implicit";149break;150case SPECIFY:151expectKey = "compiler.warn.proc.use.implicit";152break;153}154}155156if (expectKey == null) {157if (dl.diagCodes.size() != 0) {158error("no diagnostics expected");159return false;160}161} else {162if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) {163error("unexpected diagnostics generated");164return false;165}166}167168return true;169}170}171172static void createProcessorServices(String name) throws IOException {173processorServices.getParentFile().mkdirs();174175BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));176out.write(name);177out.newLine();178out.close();179}180181static class MyDiagListener implements DiagnosticListener<JavaFileObject> {182public void report(Diagnostic d) {183diagCodes.add(d.getCode());184System.err.println(d);185}186187List<String> diagCodes = new ArrayList<String>();188}189190static void error(String msg) {191System.err.println("ERROR: " + msg);192}193194static File services(Class<?> service) {195String[] dirs = { testClasses, "META-INF", "services" };196File dir = null;197for (String d: dirs)198dir = (dir == null ? new File(d) : new File(dir, d));199200return new File(dir, service.getName());201}202}203204205