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 static java.util.Collections.*;
  35 
  36 /*
  37  * This class is used to test the ability to store static state across
  38  * apt rounds.
  39  */
  40 public class StaticApf implements AnnotationProcessorFactory {
  41     static int round = -1;
  42 
  43     // Process any set of annotations
  44     private static final Collection<String> supportedAnnotations
  45         = unmodifiableCollection(Arrays.asList("*"));
  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     public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
  59                                                AnnotationProcessorEnvironment env) {
  60         return new StaticAp(env);
  61     }
  62 
  63     private static class StaticAp implements AnnotationProcessor {
  64         private final AnnotationProcessorEnvironment env;
  65         StaticAp(AnnotationProcessorEnvironment env) {
  66             this.env = env;
  67         }
  68 
  69         public void process() {
  70             int size = env.getSpecifiedTypeDeclarations().size();
  71 
  72             try {
  73                 round++;
  74                 switch (size) {
  75                 case 0:
  76                     if (round == 0) {
  77                         env.getFiler().createSourceFile("Round1").print("class Round1 {}");
  78                     } else
  79                         throw new RuntimeException("Got " + size + " decl's in round " + round);
  80                     break;
  81 
  82                 case 1:
  83                     if (round == 1) {
  84                         env.getFiler().createSourceFile("AhOne").print("class AhOne {}");
  85                         env.getFiler().createSourceFile("AndAhTwo").print("class AndAhTwo {}");
  86                         env.getFiler().createClassFile("Foo");
  87                     } else
  88                         throw new RuntimeException("Got " + size + " decl's in round " + round);
  89                     break;
  90                 case 2:
  91                     if (round != 2) {
  92                         throw new RuntimeException("Got " + size + " decl's in round " + round);
  93                     }
  94                     break;
  95                 }
  96 
  97             } catch (java.io.IOException ioe) {
  98                     throw new RuntimeException();
  99                 }
 100 
 101             }
 102 
 103     }
 104 }