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

Mon, 15 Dec 2008 16:55:33 -0800

author
xdono
date
Mon, 15 Dec 2008 16:55:33 -0800
changeset 174
fdfed22db054
parent 143
4feda9f0dbe7
child 554
9d9f26857129
permissions
-rw-r--r--

6785258: Update copyright year
Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008
Reviewed-by: katleman, ohair, tbell

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

mercurial