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());


 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  * @compile TestPackageInfo.java
  31  * @compile -processor TestPackageInfo -proc:only foo/bar/package-info.java TestPackageInfo.java
  32  */
  33 
  34 import java.util.Set;
  35 import java.util.HashSet;
  36 import java.util.Map;
  37 import javax.annotation.processing.*;
  38 import javax.lang.model.SourceVersion;
  39 import static javax.lang.model.SourceVersion.*;
  40 import javax.lang.model.element.*;
  41 import javax.lang.model.util.*;
  42 import static javax.lang.model.util.ElementFilter.*;
  43 import static javax.tools.Diagnostic.Kind.*;
  44 import static javax.tools.StandardLocation.*;
  45 
  46 import java.io.*;
  47 
  48 /**
  49  * Test the ability to process annotations on package-info.java files:
  50  * 1) Visibility of package-info files from the command line
  51  * 2) Visibility of generated package-info.java source files
  52  */
  53 public class TestPackageInfo extends JavacTestingAbstractProcessor {






  54     private int round = 0;
  55 
  56     public boolean process(Set<? extends TypeElement> annotations,
  57                            RoundEnvironment roundEnv) {
  58         round++;
  59 
  60         // Verify annotations are as expected
  61         Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
  62         if (round == 1)
  63             expectedAnnotations.add(eltUtils.
  64                                     getTypeElement("javax.annotation.processing.SupportedAnnotationTypes"));
  65         expectedAnnotations.add(eltUtils.
  66                                 getTypeElement("java.lang.SuppressWarnings"));
  67 
  68         if (!roundEnv.processingOver()) {
  69             System.out.println("\nRound " + round);
  70             int rootElementSize = roundEnv.getRootElements().size();
  71 
  72             for(Element elt : roundEnv.getRootElements()) {
  73                 System.out.printf("%nElement %s\tkind: %s%n", elt.getSimpleName(), elt.getKind());


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












 125 }