Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javac/6341866/T6341866.java
38813 views
/*1* Copyright (c) 2006, 2010, 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* @build Anno T634186628* @run main T634186629*/3031import java.io.*;32import java.util.*;33import javax.annotation.processing.*;34import javax.tools.*;3536/**37* For each of a number of implicit compilation scenarios,38* and for each of a set of annotation processing scenarios,39* verify that a class file is generated, or not, for an40* implicitly compiled source file and that the correct41* warning message is given for implicitly compiled files42* when annotation processing.43*/44public class T6341866 {45static final String testSrc = System.getProperty("test.src", ".");46static final String testClasses = System.getProperty("test.classes", ".");47static final File a_java = new File(testSrc, "A.java");48static final File a_class = new File("A.class");49static final File b_java = new File(testSrc, "B.java");50static final File b_class = new File("B.class");51static final File processorServices = services(Processor.class);5253enum ImplicitType {54NONE(null), // don't use implicit compilation55OPT_UNSET(null), // implicit compilation, but no -implicit option56OPT_NONE("-implicit:none"), // implicit compilation wiith -implicit:none57OPT_CLASS("-implicit:class"); // implicit compilation wiith -implicit:class5859ImplicitType(String opt) {60this.opt = opt;61}62final String opt;63};6465enum AnnoType {66NONE, // no annotation processing67SERVICE, // implicit annotation processing, via ServiceLoader68SPECIFY // explicit annotation processing69};707172public static void main(String ... args) throws Exception {73boolean ok = true;7475// iterate over all combinations76for (ImplicitType implicitType: EnumSet.allOf(ImplicitType.class)) {77for (AnnoType annoType: EnumSet.allOf(AnnoType.class)) {78ok &= test(implicitType, annoType);79}80}8182if (!ok)83throw new AssertionError("test failed");84}8586/**87* Verify that a class file is generated, or not, for an implicitly compiled source file,88* and that the correct warning message is given for implicitly compiled files when annotation processing.89*/90static boolean test(ImplicitType implicitType, AnnoType annoType) throws IOException {91System.err.println("test implicit=" + implicitType + " anno=" + annoType);9293// ensure clean start94a_class.delete();95b_class.delete();96processorServices.delete();9798List<String> opts = new ArrayList<String>();99opts.addAll(Arrays.asList("-d", ".", "-sourcepath", testSrc, "-classpath", testClasses, "-source", "1.6", "-Xlint:-options"));100if (implicitType.opt != null)101opts.add(implicitType.opt);102103switch (annoType) {104case SERVICE:105createProcessorServices(Anno.class.getName());106break;107case SPECIFY:108opts.addAll(Arrays.asList("-processor", Anno.class.getName()));109break;110}111112113JavaCompiler javac = ToolProvider.getSystemJavaCompiler();114MyDiagListener dl = new MyDiagListener();115StandardJavaFileManager fm = javac.getStandardFileManager(dl, null, null);116117// Note: class A references class B, so compile A if we want implicit compilation118File file = (implicitType != ImplicitType.NONE) ? a_java : b_java;119Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(file);120121//System.err.println("compile: " + opts + " " + files);122123boolean ok = javac.getTask(null, fm, dl, opts, null, files).call();124if (!ok) {125error("compilation failed");126return false;127}128129// check implicit compilation results if necessary130if (implicitType != ImplicitType.NONE) {131boolean expectClass = (implicitType != ImplicitType.OPT_NONE);132if (b_class.exists() != expectClass) {133if (b_class.exists())134error("B implicitly compiled unexpectedly");135else136error("B not impliictly compiled");137return false;138}139}140141// check message key results142String expectKey = null;143if (implicitType == ImplicitType.OPT_UNSET) {144switch (annoType) {145case SERVICE:146expectKey = "compiler.warn.proc.use.proc.or.implicit";147break;148case SPECIFY:149expectKey = "compiler.warn.proc.use.implicit";150break;151}152}153154if (expectKey == null) {155if (dl.diagCodes.size() != 0) {156error("no diagnostics expected");157return false;158}159} else {160if (!(dl.diagCodes.size() == 1 && dl.diagCodes.get(0).equals(expectKey))) {161error("unexpected diagnostics generated");162return false;163}164}165166return true;167}168169static void createProcessorServices(String name) throws IOException {170processorServices.getParentFile().mkdirs();171172BufferedWriter out = new BufferedWriter(new FileWriter(processorServices));173out.write(name);174out.newLine();175out.close();176}177178static class MyDiagListener implements DiagnosticListener<JavaFileObject> {179public void report(Diagnostic d) {180diagCodes.add(d.getCode());181System.err.println(d);182}183184List<String> diagCodes = new ArrayList<String>();185}186187static void error(String msg) {188System.err.println("ERROR: " + msg);189}190191static File services(Class<?> service) {192String[] dirs = { testClasses, "META-INF", "services" };193File dir = null;194for (String d: dirs)195dir = (dir == null ? new File(d) : new File(dir, d));196197return new File(dir, service.getName());198}199}200201202