10
|
1 |
/*
|
5520
|
2 |
* Copyright (c) 2006, 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
|
|
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);
|
|
92 |
}
|
|
93 |
|
|
94 |
try {
|
|
95 |
try {
|
|
96 |
filer.createClassFile("package-info");
|
|
97 |
throw new RuntimeException("Created class file for \"package-info\".");
|
|
98 |
} catch(FilerException fe) {}
|
|
99 |
|
|
100 |
PrintWriter pw = new PrintWriter(filer.createSourceFile("foo.package-info").openWriter());
|
|
101 |
pw.println("@SuppressWarnings(\"\")");
|
|
102 |
pw.println("package foo;");
|
|
103 |
pw.close();
|
|
104 |
|
|
105 |
} catch(IOException ioe) {
|
|
106 |
throw new RuntimeException(ioe);
|
|
107 |
}
|
|
108 |
break;
|
|
109 |
|
|
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 |
}
|