author | darcy |
Fri, 21 Dec 2012 08:45:43 -0800 | |
changeset 14963 | 974d4423c999 |
parent 8845 | 42fb82dfbf52 |
child 30730 | d3ce7619db2c |
permissions | -rw-r--r-- |
10 | 1 |
/* |
8845 | 2 |
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
8845 | 26 |
* @bug 6380018 6392177 6993311 |
10 | 27 |
* @summary Test the ability to create and process package-info.java files |
28 |
* @author Joseph D. Darcy |
|
14963
974d4423c999
8005282: Use @library tag with non-relative path for javac tests
darcy
parents:
8845
diff
changeset
|
29 |
* @library /tools/javac/lib |
6720 | 30 |
* @build JavacTestingAbstractProcessor |
10 | 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 |
*/ |
|
6720 | 54 |
public class TestPackageInfo extends JavacTestingAbstractProcessor { |
10 | 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>(); |
|
8845 | 63 |
expectedAnnotations.add(eltUtils.getTypeElement("java.lang.Deprecated")); |
10 | 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()); |
|
8845 | 93 |
pw.println("@Deprecated"); |
10 | 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 |
} |