aoqi@0: /* aoqi@0: * Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6348193 aoqi@0: * @summary AS8.1 UR2 BAT test failure with "javac" aoqi@0: * @compile -proc:none T6348193.java aoqi@0: * @run main/othervm T6348193 aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.net.*; aoqi@0: import java.util.*; aoqi@0: import javax.annotation.processing.*; aoqi@0: import javax.lang.model.element.*; aoqi@0: import javax.tools.*; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: aoqi@0: @SupportedAnnotationTypes({"*"}) aoqi@0: public class T6348193 extends AbstractProcessor aoqi@0: { aoqi@0: private static final boolean verbose = true; aoqi@0: aoqi@0: enum NoYes { NO, YES }; aoqi@0: enum NoGoodBad { NO, GOOD, BAD}; aoqi@0: aoqi@0: public static final String myName = T6348193.class.getName(); aoqi@0: aoqi@0: public static void main(String... args) throws IOException { aoqi@0: if (System.getSecurityManager() != null) aoqi@0: throw new AssertionError("unexpected security manager"); aoqi@0: aoqi@0: for (NoYes secMgr: EnumSet.allOf(NoYes.class)) aoqi@0: for (NoGoodBad config: EnumSet.allOf(NoGoodBad.class)) aoqi@0: for (NoYes proc: EnumSet.allOf(NoYes.class)) aoqi@0: test(secMgr, config, proc); aoqi@0: } aoqi@0: aoqi@0: private static File processed = new File("processed"); aoqi@0: aoqi@0: public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException { aoqi@0: if (verbose) aoqi@0: System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc); aoqi@0: aoqi@0: if (secMgr == NoYes.YES && System.getSecurityManager() == null) aoqi@0: System.setSecurityManager(new NoLoaderSecurityManager()); aoqi@0: aoqi@0: installConfigFile(config); aoqi@0: aoqi@0: processed.delete(); aoqi@0: aoqi@0: List args = new ArrayList(); aoqi@0: //args.add("-XprintRounds"); aoqi@0: if (proc == NoYes.YES) { aoqi@0: args.add("-processor"); aoqi@0: args.add(myName); aoqi@0: } aoqi@0: args.add("-processorpath"); aoqi@0: args.add(System.getProperty("java.class.path")); aoqi@0: args.add("-d"); aoqi@0: args.add("."); aoqi@0: aoqi@0: JavacTool t = JavacTool.create(); // avoid using class loader aoqi@0: aoqi@0: MyDiagListener dl = new MyDiagListener(); aoqi@0: PrintWriter out = new PrintWriter(System.err, true); aoqi@0: StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null); aoqi@0: File file = new File(System.getProperty("test.src"), myName+".java"); aoqi@0: Iterable files = aoqi@0: fm.getJavaFileObjectsFromFiles(Arrays.asList(file)); aoqi@0: boolean ok = t.getTask(out, null, dl, args, null, files).call(); aoqi@0: aoqi@0: if (config == NoGoodBad.GOOD || proc == NoYes.YES) { aoqi@0: if (secMgr == NoYes.YES) { aoqi@0: if (dl.last == null) aoqi@0: throw new AssertionError("Security manager installed, and processors present, " aoqi@0: + " but no diagnostic received"); aoqi@0: } aoqi@0: else { aoqi@0: if (!processed.exists()) aoqi@0: throw new AssertionError("No security manager installed, and processors present, " aoqi@0: + " but no processing occurred"); aoqi@0: } aoqi@0: } aoqi@0: else if (config == NoGoodBad.BAD) { aoqi@0: // TODO: should verify that no compiler crash occurred aoqi@0: // needs revised JSR199 spec aoqi@0: } aoqi@0: else { aoqi@0: if (processed.exists()) aoqi@0: throw new AssertionError("No processors present, but processing occurred!"); aoqi@0: } aoqi@0: aoqi@0: if (verbose) aoqi@0: System.err.println("OK"); aoqi@0: } aoqi@0: aoqi@0: // set up or remove a service configuration file aoqi@0: static void installConfigFile(NoGoodBad type) throws IOException { aoqi@0: File f = new File(System.getProperty("test.classes", ".")); aoqi@0: for (String s: new String[] { "META-INF", "services", Processor.class.getName() }) aoqi@0: f = new File(f, s); aoqi@0: BufferedWriter out; aoqi@0: switch (type) { aoqi@0: case GOOD: aoqi@0: f.getParentFile().mkdirs(); aoqi@0: out = new BufferedWriter(new FileWriter(f)); aoqi@0: out.write(myName); aoqi@0: out.newLine(); aoqi@0: out.close(); aoqi@0: break; aoqi@0: case BAD: aoqi@0: f.getParentFile().mkdirs(); aoqi@0: out = new BufferedWriter(new FileWriter(f)); aoqi@0: out.write("This is not a valid line"); aoqi@0: out.newLine(); aoqi@0: out.close(); aoqi@0: break; aoqi@0: case NO: aoqi@0: f.delete(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: } aoqi@0: aoqi@0: // annotation processor method aoqi@0: public boolean process(Set tes, RoundEnvironment renv ) aoqi@0: { aoqi@0: try { aoqi@0: // touch a file to indicate we have run aoqi@0: new FileWriter(processed).close(); aoqi@0: } catch (IOException e) { aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: static class MyDiagListener implements DiagnosticListener aoqi@0: { aoqi@0: public void report(Diagnostic message) { aoqi@0: if (verbose) aoqi@0: System.err.println(message); aoqi@0: last = message; aoqi@0: } aoqi@0: aoqi@0: Diagnostic last; aoqi@0: } aoqi@0: aoqi@0: static class NoLoaderSecurityManager extends SecurityManager aoqi@0: { aoqi@0: public void checkCreateClassLoader() { aoqi@0: throw new SecurityException("Not today, thanks you!"); aoqi@0: } aoqi@0: aoqi@0: public void checkPropertyAccess(String key) { /*OK*/ } aoqi@0: aoqi@0: public void checkDelete(String file) { /*OK*/ } aoqi@0: public void checkRead(FileDescriptor fd) { /*OK*/ } aoqi@0: public void checkRead(String file) { /*OK*/ } aoqi@0: public void checkRead(String file, Object context) { /*OK*/ } aoqi@0: public void checkWrite(String file) { /*OK*/ } aoqi@0: aoqi@0: } aoqi@0: }