1/* 2 * Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 6348193 27 * @summary AS8.1 UR2 BAT test failure with "javac" 28 * @modules jdk.compiler/com.sun.tools.javac.api 29 * jdk.compiler/com.sun.tools.javac.file 30 * @compile -proc:none T6348193.java 31 * @run main/othervm T6348193 32 */ 33 34import java.io.*; 35import java.net.*; 36import java.security.*; 37import java.util.*; 38import javax.annotation.processing.*; 39import javax.lang.model.element.*; 40import javax.tools.*; 41import com.sun.tools.javac.api.JavacTool; 42 43@SupportedAnnotationTypes({"*"}) 44public class T6348193 extends AbstractProcessor 45{ 46 private static final boolean verbose = true; 47 48 enum NoYes { NO, YES }; 49 enum NoGoodBad { NO, GOOD, BAD}; 50 51 public static final String myName = T6348193.class.getName(); 52 53 public static void main(String... args) throws IOException { 54 if (System.getSecurityManager() != null) 55 throw new AssertionError("unexpected security manager"); 56 57 for (NoYes secMgr: EnumSet.allOf(NoYes.class)) 58 for (NoGoodBad config: EnumSet.allOf(NoGoodBad.class)) 59 for (NoYes proc: EnumSet.allOf(NoYes.class)) 60 test(secMgr, config, proc); 61 } 62 63 private static File processed = new File("processed"); 64 65 public static void test(NoYes secMgr, NoGoodBad config, NoYes proc) throws IOException { 66 if (verbose) 67 System.err.println("secMgr:" + secMgr + " config:" + config + " proc:" + proc); 68 69 if (secMgr == NoYes.YES && System.getSecurityManager() == null) 70 System.setSecurityManager(new NoLoaderSecurityManager()); 71 72 installConfigFile(config); 73 74 processed.delete(); 75 76 List<String> args = new ArrayList<String>(); 77 //args.add("-XprintRounds"); 78 if (proc == NoYes.YES) { 79 args.add("-processor"); 80 args.add(myName); 81 } 82 args.add("-processorpath"); 83 args.add(System.getProperty("java.class.path")); 84 args.add("-d"); 85 args.add("."); 86 87 JavacTool t = JavacTool.create(); // avoid using class loader 88 89 MyDiagListener dl = new MyDiagListener(); 90 PrintWriter out = new PrintWriter(System.err, true); 91 try (StandardJavaFileManager fm = t.getStandardFileManager(dl, null, null)) { 92 File file = new File(System.getProperty("test.src"), myName+".java"); 93 Iterable<? extends JavaFileObject> files = 94 fm.getJavaFileObjectsFromFiles(Arrays.asList(file)); 95 boolean ok = t.getTask(out, null, dl, args, null, files).call(); 96 97 if (config == NoGoodBad.GOOD || proc == NoYes.YES) { 98 if (secMgr == NoYes.YES) { 99 if (dl.last == null) 100 throw new AssertionError("Security manager installed, and processors present, " 101 + " but no diagnostic received"); 102 } 103 else { 104 if (!processed.exists()) 105 throw new AssertionError("No security manager installed, and processors present, " 106 + " but no processing occurred"); 107 } 108 } 109 else if (config == NoGoodBad.BAD) { 110 // TODO: should verify that no compiler crash occurred 111 // needs revised JSR199 spec 112 } 113 else { 114 if (processed.exists()) 115 throw new AssertionError("No processors present, but processing occurred!"); 116 } 117 118 if (verbose) 119 System.err.println("OK"); 120 } 121 } 122 123 // set up or remove a service configuration file 124 static void installConfigFile(NoGoodBad type) throws IOException { 125 File f = new File(System.getProperty("test.classes", ".")); 126 for (String s: new String[] { "META-INF", "services", Processor.class.getName() }) 127 f = new File(f, s); 128 BufferedWriter out; 129 switch (type) { 130 case GOOD: 131 f.getParentFile().mkdirs(); 132 out = new BufferedWriter(new FileWriter(f)); 133 out.write(myName); 134 out.newLine(); 135 out.close(); 136 break; 137 case BAD: 138 f.getParentFile().mkdirs(); 139 out = new BufferedWriter(new FileWriter(f)); 140 out.write("This is not a valid line"); 141 out.newLine(); 142 out.close(); 143 break; 144 case NO: 145 f.delete(); 146 } 147 148 149 } 150 151 // annotation processor method 152 public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv ) 153 { 154 try { 155 // touch a file to indicate we have run 156 new FileWriter(processed).close(); 157 } catch (IOException e) { 158 } 159 return true; 160 } 161 162 static class MyDiagListener implements DiagnosticListener<JavaFileObject> 163 { 164 public void report(Diagnostic<? extends JavaFileObject> message) { 165 if (verbose) 166 System.err.println(message); 167 last = message; 168 } 169 170 Diagnostic<? extends JavaFileObject> last; 171 } 172 173 static class NoLoaderSecurityManager extends SecurityManager 174 { 175 public void checkCreateClassLoader() { 176 throw new SecurityException("Not today, thanks you!"); 177 } 178 179 public void checkPropertyAccess(String key) { /*OK*/ } 180 181 public void checkDelete(String file) { /*OK*/ } 182 public void checkPermission(Permission perm) { /*OK*/ } 183 public void checkRead(FileDescriptor fd) { /*OK*/ } 184 public void checkRead(String file) { /*OK*/ } 185 public void checkRead(String file, Object context) { /*OK*/ } 186 public void checkWrite(String file) { /*OK*/ } 187 188 } 189} 190