TestPackageInfo.java revision 1465:b52a38d4536c
1/*
2 * Copyright (c) 2006, 2011, 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 * @build   JavacTestingAbstractProcessor
31 * @compile TestPackageInfo.java
32 * @compile -processor TestPackageInfo -proc:only foo/bar/package-info.java TestPackageInfo.java
33 */
34
35import java.util.Set;
36import java.util.HashSet;
37import java.util.Map;
38import javax.annotation.processing.*;
39import javax.lang.model.SourceVersion;
40import static javax.lang.model.SourceVersion.*;
41import javax.lang.model.element.*;
42import javax.lang.model.util.*;
43import static javax.lang.model.util.ElementFilter.*;
44import static javax.tools.Diagnostic.Kind.*;
45import static javax.tools.StandardLocation.*;
46
47import 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 */
54public 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.Deprecated"));
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);
84                }
85
86                try {
87                    try {
88                        filer.createClassFile("package-info");
89                        throw new RuntimeException("Created class file for \"package-info\".");
90                    } catch(FilerException fe) {}
91
92                    PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
93                    pw.println("@Deprecated");
94                    pw.println("package foo;");
95                    pw.close();
96
97                } catch(IOException ioe) {
98                    throw new RuntimeException(ioe);
99                }
100                break;
101
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}
123