test/tools/javac/processing/6348193/T6348193.java

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

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

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2006, 2008, 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 6348193
aoqi@0 27 * @summary AS8.1 UR2 BAT test failure with "javac"
aoqi@0 28 * @compile -proc:none T6348193.java
aoqi@0 29 * @run main/othervm T6348193
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 import java.io.*;
aoqi@0 33 import java.net.*;
aoqi@0 34 import java.util.*;
aoqi@0 35 import javax.annotation.processing.*;
aoqi@0 36 import javax.lang.model.element.*;
aoqi@0 37 import javax.tools.*;
aoqi@0 38 import com.sun.tools.javac.api.JavacTool;
aoqi@0 39
aoqi@0 40 @SupportedAnnotationTypes({"*"})
aoqi@0 41 public class T6348193 extends AbstractProcessor
aoqi@0 42 {
aoqi@0 43 private static final boolean verbose = true;
aoqi@0 44
aoqi@0 45 enum NoYes { NO, YES };
aoqi@0 46 enum NoGoodBad { NO, GOOD, BAD};
aoqi@0 47
aoqi@0 48 public static final String myName = T6348193.class.getName();
aoqi@0 49
aoqi@0 50 public static void main(String... args) throws IOException {
aoqi@0 51 if (System.getSecurityManager() != null)
aoqi@0 52 throw new AssertionError("unexpected security manager");
aoqi@0 53
aoqi@0 54 for (NoYes secMgr: EnumSet.allOf(NoYes.class))
aoqi@0 55 for (NoGoodBad config: EnumSet.allOf(NoGoodBad.class))
aoqi@0 56 for (NoYes proc: EnumSet.allOf(NoYes.class))
aoqi@0 57 test(secMgr, config, proc);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 private static File processed = new File("processed");
aoqi@0 61
aoqi@0 62 public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException {
aoqi@0 63 if (verbose)
aoqi@0 64 System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc);
aoqi@0 65
aoqi@0 66 if (secMgr == NoYes.YES && System.getSecurityManager() == null)
aoqi@0 67 System.setSecurityManager(new NoLoaderSecurityManager());
aoqi@0 68
aoqi@0 69 installConfigFile(config);
aoqi@0 70
aoqi@0 71 processed.delete();
aoqi@0 72
aoqi@0 73 List<String> args = new ArrayList<String>();
aoqi@0 74 //args.add("-XprintRounds");
aoqi@0 75 if (proc == NoYes.YES) {
aoqi@0 76 args.add("-processor");
aoqi@0 77 args.add(myName);
aoqi@0 78 }
aoqi@0 79 args.add("-processorpath");
aoqi@0 80 args.add(System.getProperty("java.class.path"));
aoqi@0 81 args.add("-d");
aoqi@0 82 args.add(".");
aoqi@0 83
aoqi@0 84 JavacTool t = JavacTool.create(); // avoid using class loader
aoqi@0 85
aoqi@0 86 MyDiagListener dl = new MyDiagListener();
aoqi@0 87 PrintWriter out = new PrintWriter(System.err, true);
aoqi@0 88 StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null);
aoqi@0 89 File file = new File(System.getProperty("test.src"), myName+".java");
aoqi@0 90 Iterable<? extends JavaFileObject> files =
aoqi@0 91 fm.getJavaFileObjectsFromFiles(Arrays.asList(file));
aoqi@0 92 boolean ok = t.getTask(out, null, dl, args, null, files).call();
aoqi@0 93
aoqi@0 94 if (config == NoGoodBad.GOOD || proc == NoYes.YES) {
aoqi@0 95 if (secMgr == NoYes.YES) {
aoqi@0 96 if (dl.last == null)
aoqi@0 97 throw new AssertionError("Security manager installed, and processors present, "
aoqi@0 98 + " but no diagnostic received");
aoqi@0 99 }
aoqi@0 100 else {
aoqi@0 101 if (!processed.exists())
aoqi@0 102 throw new AssertionError("No security manager installed, and processors present, "
aoqi@0 103 + " but no processing occurred");
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106 else if (config == NoGoodBad.BAD) {
aoqi@0 107 // TODO: should verify that no compiler crash occurred
aoqi@0 108 // needs revised JSR199 spec
aoqi@0 109 }
aoqi@0 110 else {
aoqi@0 111 if (processed.exists())
aoqi@0 112 throw new AssertionError("No processors present, but processing occurred!");
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 if (verbose)
aoqi@0 116 System.err.println("OK");
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 // set up or remove a service configuration file
aoqi@0 120 static void installConfigFile(NoGoodBad type) throws IOException {
aoqi@0 121 File f = new File(System.getProperty("test.classes", "."));
aoqi@0 122 for (String s: new String[] { "META-INF", "services", Processor.class.getName() })
aoqi@0 123 f = new File(f, s);
aoqi@0 124 BufferedWriter out;
aoqi@0 125 switch (type) {
aoqi@0 126 case GOOD:
aoqi@0 127 f.getParentFile().mkdirs();
aoqi@0 128 out = new BufferedWriter(new FileWriter(f));
aoqi@0 129 out.write(myName);
aoqi@0 130 out.newLine();
aoqi@0 131 out.close();
aoqi@0 132 break;
aoqi@0 133 case BAD:
aoqi@0 134 f.getParentFile().mkdirs();
aoqi@0 135 out = new BufferedWriter(new FileWriter(f));
aoqi@0 136 out.write("This is not a valid line");
aoqi@0 137 out.newLine();
aoqi@0 138 out.close();
aoqi@0 139 break;
aoqi@0 140 case NO:
aoqi@0 141 f.delete();
aoqi@0 142 }
aoqi@0 143
aoqi@0 144
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 // annotation processor method
aoqi@0 148 public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv )
aoqi@0 149 {
aoqi@0 150 try {
aoqi@0 151 // touch a file to indicate we have run
aoqi@0 152 new FileWriter(processed).close();
aoqi@0 153 } catch (IOException e) {
aoqi@0 154 }
aoqi@0 155 return true;
aoqi@0 156 }
aoqi@0 157
aoqi@0 158 static class MyDiagListener implements DiagnosticListener<JavaFileObject>
aoqi@0 159 {
aoqi@0 160 public void report(Diagnostic<? extends JavaFileObject> message) {
aoqi@0 161 if (verbose)
aoqi@0 162 System.err.println(message);
aoqi@0 163 last = message;
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 Diagnostic<? extends JavaFileObject> last;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 static class NoLoaderSecurityManager extends SecurityManager
aoqi@0 170 {
aoqi@0 171 public void checkCreateClassLoader() {
aoqi@0 172 throw new SecurityException("Not today, thanks you!");
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 public void checkPropertyAccess(String key) { /*OK*/ }
aoqi@0 176
aoqi@0 177 public void checkDelete(String file) { /*OK*/ }
aoqi@0 178 public void checkRead(FileDescriptor fd) { /*OK*/ }
aoqi@0 179 public void checkRead(String file) { /*OK*/ }
aoqi@0 180 public void checkRead(String file, Object context) { /*OK*/ }
aoqi@0 181 public void checkWrite(String file) { /*OK*/ }
aoqi@0 182
aoqi@0 183 }
aoqi@0 184 }

mercurial