1 /*
   2  * Copyright (c) 2006, 2015, 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 6993311
  27  * @summary Test the ability to create and process package-info.java files
  28  * @author  Joseph D. Darcy
  29  * @library /tools/javac/lib
  30  * @modules java.compiler
  31  *          jdk.compiler
  32  * @build   JavacTestingAbstractProcessor
  33  * @compile TestPackageInfo.java
  34  * @compile -processor TestPackageInfo -proc:only foo/bar/package-info.java TestPackageInfo.java
  35  */
  36 
  37 import java.util.Set;
  38 import java.util.HashSet;
  39 import java.util.Map;
  40 import javax.annotation.processing.*;
  41 import javax.lang.model.SourceVersion;
  42 import static javax.lang.model.SourceVersion.*;
  43 import javax.lang.model.element.*;
  44 import javax.lang.model.util.*;
  45 import static javax.lang.model.util.ElementFilter.*;
  46 import static javax.tools.Diagnostic.Kind.*;
  47 import static javax.tools.StandardLocation.*;
  48 
  49 import java.io.*;
  50 
  51 /**
  52  * Test the ability to process annotations on package-info.java files:
  53  * 1) Visibility of package-info files from the command line
  54  * 2) Visibility of generated package-info.java source files
  55  */
  56 public class TestPackageInfo extends JavacTestingAbstractProcessor {
  57     private int round = 0;
  58 
  59     public boolean process(Set<? extends TypeElement> annotations,
  60                            RoundEnvironment roundEnv) {
  61         round++;
  62 
  63         // Verify annotations are as expected
  64         Set<TypeElement> expectedAnnotations = new HashSet<TypeElement>();
  65         expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated"));
  66 
  67         if (!roundEnv.processingOver()) {
  68             System.out.println("\nRound " + round);
  69             int rootElementSize = roundEnv.getRootElements().size();
  70 
  71             for(Element elt : roundEnv.getRootElements()) {
  72                 System.out.printf("%nElement %s\tkind: %s%n", elt.getSimpleName(), elt.getKind());
  73                 eltUtils.printElements(new PrintWriter(System.out), elt);
  74             }
  75 
  76             switch (round) {
  77             case 1:
  78                 if (rootElementSize != 2)
  79                     throw new RuntimeException("Unexpected root element size " + rootElementSize);
  80 
  81                 // Note that foo.bar.FuBar, an element of package
  82                 // foo.bar, contains @Deprecated which should *not* be
  83                 // included in the set of annotations to process
  84                 if (!expectedAnnotations.equals(annotations)) {
  85                     throw new RuntimeException("Unexpected annotations: " + annotations);
  86                 }
  87 
  88                 try {
  89                     try {
  90                         filer.createClassFile("package-info");
  91                         throw new RuntimeException("Created class file for \"package-info\".");
  92                     } catch(FilerException fe) {}
  93 
  94                     PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
  95                     pw.println("@Deprecated");
  96                     pw.println("package foo;");
  97                     pw.close();
  98 
  99                 } catch(IOException ioe) {
 100                     throw new RuntimeException(ioe);
 101                 }
 102                 break;
 103 
 104             case 2:
 105                 // Expect foo.package-info
 106 
 107                 Set<Element> expectedElement = new HashSet<Element>();
 108                 expectedElement.add(eltUtils.getPackageElement("foo"));
 109                 if (!expectedElement.equals(roundEnv.getRootElements()))
 110                     throw new RuntimeException("Unexpected root element set " + roundEnv.getRootElements());
 111 
 112                 if (!expectedAnnotations.equals(annotations)) {
 113                     throw new RuntimeException("Unexpected annotations: " + annotations);
 114                 }
 115 
 116                 break;
 117 
 118             default:
 119                 throw new RuntimeException("Unexpected round number " + round);
 120             }
 121         }
 122         return false;
 123     }
 124 }