test/tools/javac/processing/filer/TestPackageInfo.java

Print this page


   1 /*
   2  * Copyright (c) 2006, 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 6380018 6392177
  27  * @summary Test the ability to create and process package-info.java files
  28  * @author  Joseph D. Darcy


  29  * @compile TestPackageInfo.java
  30  * @compile -processor TestPackageInfo -proc:only foo/bar/package-info.java TestPackageInfo.java
  31  */
  32 
  33 import java.util.Set;
  34 import java.util.HashSet;
  35 import java.util.Map;
  36 import javax.annotation.processing.*;
  37 import javax.lang.model.SourceVersion;
  38 import static javax.lang.model.SourceVersion.*;
  39 import javax.lang.model.element.*;
  40 import javax.lang.model.util.*;
  41 import static javax.lang.model.util.ElementFilter.*;
  42 import static javax.tools.Diagnostic.Kind.*;
  43 import static javax.tools.StandardLocation.*;
  44 
  45 import java.io.*;
  46 
  47 /**
  48  * Test the ability to process annotations on package-info.java files:
  49  * 1) Visibility of package-info files from the command line
  50  * 2) Visibility of generated package-info.java source files
  51  */
  52 @SupportedAnnotationTypes("*")
  53 public class TestPackageInfo extends AbstractProcessor {
  54     private Elements eltUtils;
  55     private Messager messager;
  56     private Filer filer;
  57     private Map<String,String> options;
  58 
  59     private int round = 0;
  60 
  61     public boolean process(Set<? extends TypeElement> annotations,
  62                            RoundEnvironment roundEnv) {
  63         round++;
  64 
  65         // Verify annotations are as expected
  66         Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
  67         if (round == 1)
  68             expectedAnnotations.add(eltUtils.
  69                                     getTypeElement("javax.annotation.processing.SupportedAnnotationTypes"));
  70         expectedAnnotations.add(eltUtils.
  71                                 getTypeElement("java.lang.SuppressWarnings"));
  72 
  73         if (!roundEnv.processingOver()) {
  74             System.out.println("\nRound " + round);
  75             int rootElementSize = roundEnv.getRootElements().size();
  76 
  77             for(Element elt : roundEnv.getRootElements()) {
  78                 System.out.printf("%nElement %s\tkind: %s%n", elt.getSimpleName(), elt.getKind());
  79                 eltUtils.printElements(new PrintWriter(System.out), elt);
  80             }
  81 
  82             switch (round) {
  83             case 1:
  84                 if (rootElementSize != 2)
  85                     throw new RuntimeException("Unexpected root element size " + rootElementSize);
  86 
  87                 // Note that foo.bar.FuBar, an element of package
  88                 // foo.bar, contains @Deprecated which should *not* be
  89                 // included in the set of annotations to process
  90                 if (!expectedAnnotations.equals(annotations)) {
  91                     throw new RuntimeException("Unexpected annotations: " + annotations);


 110             case 2:
 111                 // Expect foo.package-info
 112 
 113                 Set<Element> expectedElement = new HashSet<Element>();
 114                 expectedElement.add(eltUtils.getPackageElement("foo"));
 115                 if (!expectedElement.equals(roundEnv.getRootElements()))
 116                     throw new RuntimeException("Unexpected root element set " + roundEnv.getRootElements());
 117 
 118                 if (!expectedAnnotations.equals(annotations)) {
 119                     throw new RuntimeException("Unexpected annotations: " + annotations);
 120                 }
 121 
 122                 break;
 123 
 124             default:
 125                 throw new RuntimeException("Unexpected round number " + round);
 126             }
 127         }
 128         return false;
 129     }
 130 
 131     public SourceVersion getSupportedSourceVersion() {
 132         return SourceVersion.latest();
 133     }
 134 
 135     public void init(ProcessingEnvironment processingEnv) {
 136         super.init(processingEnv);
 137         eltUtils = processingEnv.getElementUtils();
 138         messager = processingEnv.getMessager();
 139         filer    = processingEnv.getFiler();
 140         options  = processingEnv.getOptions();
 141     }
 142 }
   1 /*
   2  * Copyright (c) 2006, 2010, 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 6380018 6392177
  27  * @summary Test the ability to create and process package-info.java files
  28  * @author  Joseph D. Darcy
  29  * @library ../../lib
  30  * @build   JavacTestingAbstractProcessor
  31  * @compile TestPackageInfo.java
  32  * @compile -processor TestPackageInfo -proc:only foo/bar/package-info.java TestPackageInfo.java
  33  */
  34 
  35 import java.util.Set;
  36 import java.util.HashSet;
  37 import java.util.Map;
  38 import javax.annotation.processing.*;
  39 import javax.lang.model.SourceVersion;
  40 import static javax.lang.model.SourceVersion.*;
  41 import javax.lang.model.element.*;
  42 import javax.lang.model.util.*;
  43 import static javax.lang.model.util.ElementFilter.*;
  44 import static javax.tools.Diagnostic.Kind.*;
  45 import static javax.tools.StandardLocation.*;
  46 
  47 import java.io.*;
  48 
  49 /**
  50  * Test the ability to process annotations on package-info.java files:
  51  * 1) Visibility of package-info files from the command line
  52  * 2) Visibility of generated package-info.java source files
  53  */
  54 public class TestPackageInfo extends JavacTestingAbstractProcessor {






  55     private int round = 0;
  56 
  57     public boolean process(Set<? extends TypeElement> annotations,
  58                            RoundEnvironment roundEnv) {
  59         round++;
  60 
  61         // Verify annotations are as expected
  62         Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
  63         expectedAnnotations.add(eltUtils.getTypeElement("java.lang.SuppressWarnings"));




  64 
  65         if (!roundEnv.processingOver()) {
  66             System.out.println("\nRound " + round);
  67             int rootElementSize = roundEnv.getRootElements().size();
  68 
  69             for(Element elt : roundEnv.getRootElements()) {
  70                 System.out.printf("%nElement %s\tkind: %s%n", elt.getSimpleName(), elt.getKind());
  71                 eltUtils.printElements(new PrintWriter(System.out), elt);
  72             }
  73 
  74             switch (round) {
  75             case 1:
  76                 if (rootElementSize != 2)
  77                     throw new RuntimeException("Unexpected root element size " + rootElementSize);
  78 
  79                 // Note that foo.bar.FuBar, an element of package
  80                 // foo.bar, contains @Deprecated which should *not* be
  81                 // included in the set of annotations to process
  82                 if (!expectedAnnotations.equals(annotations)) {
  83                     throw new RuntimeException("Unexpected annotations: " + annotations);


 102             case 2:
 103                 // Expect foo.package-info
 104 
 105                 Set<Element> expectedElement = new HashSet<Element>();
 106                 expectedElement.add(eltUtils.getPackageElement("foo"));
 107                 if (!expectedElement.equals(roundEnv.getRootElements()))
 108                     throw new RuntimeException("Unexpected root element set " + roundEnv.getRootElements());
 109 
 110                 if (!expectedAnnotations.equals(annotations)) {
 111                     throw new RuntimeException("Unexpected annotations: " + annotations);
 112                 }
 113 
 114                 break;
 115 
 116             default:
 117                 throw new RuntimeException("Unexpected round number " + round);
 118             }
 119         }
 120         return false;
 121     }












 122 }