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.*;
  35 
  36 import static java.util.Collections.*;
  37 
  38 /*
  39  * This class is used to test the the interaction of -XclassesAsDecls
  40  * with command line options handling.
  41  */
  42 public class ClassDeclApf2 implements AnnotationProcessorFactory {
  43     static int round = -1;
  44 
  45     // Process any set of annotations
  46     private static final Collection<String> supportedAnnotations
  47         = unmodifiableCollection(Arrays.asList("*"));
  48 
  49     // No supported options
  50     private static final Collection<String> supportedOptions = emptySet();
  51 
  52     public Collection<String> supportedAnnotationTypes() {
  53         return supportedAnnotations;
  54     }
  55 
  56     public Collection<String> supportedOptions() {
  57         return supportedOptions;
  58     }
  59 
  60     public AnnotationProcessor getProcessorFor(Set<AnnotationTypeDeclaration> atds,
  61                                                AnnotationProcessorEnvironment env) {
  62         return new ClassDeclAp(env);
  63     }
  64 
  65     private static class ClassDeclAp implements AnnotationProcessor {
  66         private final AnnotationProcessorEnvironment env;
  67         ClassDeclAp(AnnotationProcessorEnvironment env) {
  68             this.env = env;
  69         }
  70 
  71         // Simple inefficient drain
  72         void drain(InputStream is, OutputStream os) {
  73             try {
  74             while (is.available() > 0 )
  75                 os.write(is.read());
  76             } catch (java.io.IOException ioe) {
  77                 throw new RuntimeException(ioe);
  78             }
  79         }
  80 
  81         public void process() {
  82             int size = env.getSpecifiedTypeDeclarations().size();
  83             Filer f = env.getFiler();
  84 
  85             try {
  86                 round++;
  87                 switch (size) {
  88                 case 3:
  89                     if (round == 0) {
  90                         drain(new FileInputStream("./tmp/classes/Round1Class.class"),
  91                               f.createClassFile("Round1Class"));
  92                     } else
  93                         throw new RuntimeException("Got " + size + " decl's in round " + round);
  94                     break;
  95 
  96                 case 1:
  97                     if (round == 1) {
  98                         f.createSourceFile("AhOne").println("public class AhOne {}");
  99                         System.out.println("Before writing AndAhTwoClass");
 100                         drain(new FileInputStream("./tmp/classes/AndAhTwoClass.class"),
 101                               f.createClassFile("AndAhTwoClass"));
 102                         System.out.println("After writing AndAhTwoClass");
 103                     } else
 104                         throw new RuntimeException("Got " + size + " decl's in round " + round);
 105                     break;
 106 
 107                 case 2:
 108                     if (round != 2) {
 109                         throw new RuntimeException("Got " + size + " decl's in round " + round);
 110                     }
 111                     break;
 112                 default:
 113                     throw new RuntimeException("Unexpected number of declarations:" + size +
 114                                                "\n Specified:" + env.getSpecifiedTypeDeclarations() +
 115                                                "\n Included:" + env.getTypeDeclarations() );
 116                 }
 117 
 118             } catch (java.io.IOException ioe) {
 119                 throw new RuntimeException(ioe);
 120             }
 121         }
 122     }
 123 }