1 /*
   2  * Copyright (c) 2004, 2007, 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 import com.sun.mirror.apt.*;
  26 import com.sun.mirror.declaration.*;
  27 import com.sun.mirror.type.*;
  28 import com.sun.mirror.util.*;
  29 
  30 import java.util.Collection;
  31 import java.util.Set;
  32 import java.util.Arrays;
  33 
  34 import java.io.File;
  35 
  36 import static java.util.Collections.*;
  37 import static com.sun.mirror.util.DeclarationVisitors.*;
  38 
  39 /*
  40  * Factory to help test updated discovery policy.
  41  */
  42 public class Round1Apf implements AnnotationProcessorFactory {
  43     // Process @Round1
  44     private static final Collection<String> supportedAnnotations
  45         = unmodifiableCollection(Arrays.asList("Round1"));
  46 
  47     // No supported options
  48     private static final Collection<String> supportedOptions = emptySet();
  49 
  50     public Collection<String> supportedAnnotationTypes() {
  51         return supportedAnnotations;
  52     }
  53 
  54     public Collection<String> supportedOptions() {
  55         return supportedOptions;
  56     }
  57 
  58     private static int round = 0;
  59 
  60     public AnnotationProcessor getProcessorFor(
  61             Set<AnnotationTypeDeclaration> atds,
  62             AnnotationProcessorEnvironment env) {
  63         return new Round1Ap(env, atds.size() == 0);
  64     }
  65 
  66     private static class Round1Ap implements AnnotationProcessor, RoundCompleteListener {
  67         private final AnnotationProcessorEnvironment env;
  68         private final boolean empty;
  69 
  70         Round1Ap(AnnotationProcessorEnvironment env, boolean empty) {
  71             this.env = env;
  72             this.empty = empty;
  73         }
  74 
  75         public void process() {
  76             Round1Apf.round++;
  77             try {
  78                 if (!empty) {
  79                     Filer f = env.getFiler();
  80                     f.createSourceFile("Dummy2").println("@Round2 class Dummy2{}");
  81                     f.createTextFile(Filer.Location.SOURCE_TREE,
  82                                      "",
  83                                      new File("foo.txt"),
  84                                      null).println("xxyzzy");
  85                     f.createClassFile("Vacant");
  86                     f.createBinaryFile(Filer.Location.CLASS_TREE,
  87                                        "",
  88                                        new File("onezero"));
  89                 }
  90             } catch (java.io.IOException ioe) {
  91                 throw new RuntimeException(ioe);
  92             }
  93 
  94             System.out.println("Round1Apf: " + round);
  95             env.addListener(this);
  96         }
  97 
  98         public void roundComplete(RoundCompleteEvent event) {
  99             RoundState rs = event.getRoundState();
 100 
 101             if (event.getSource() != this.env)
 102                 throw new RuntimeException("Wrong source!");
 103 
 104             Filer f = env.getFiler();
 105             try {
 106                 f.createSourceFile("AfterTheBell").println("@Round2 class AfterTheBell{}");
 107                 throw new RuntimeException("Inappropriate source file creation.");
 108             } catch (java.io.IOException ioe) {}
 109 
 110 
 111             System.out.printf("\t[final round: %b, error raised: %b, "+
 112                               "source files created: %b, class files created: %b]%n",
 113                               rs.finalRound(),
 114                               rs.errorRaised(),
 115                               rs.sourceFilesCreated(),
 116                               rs.classFilesCreated());
 117 
 118             System.out.println("Round1Apf: " + round + " complete");
 119         }
 120     }
 121 }