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 import java.io.*;
25 import java.util.*;
26 import javax.annotation.processing.*;
27 import javax.lang.model.element.*;
28 import javax.lang.model.SourceVersion;
29 import javax.tools.Diagnostic.Kind;
30
31 /*
32 * @test
33 * @bug 6499119
34 * @summary Created package-info class file modeled improperly
35 * @compile ClassProcessor.java package-info.java
36 * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object
37 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
38 */
39
40 @SupportedOptions({ "gen", "expect" })
41 @SupportedAnnotationTypes({"*"})
42 public class ClassProcessor extends AbstractProcessor {
43 int round = 1;
44
45 public SourceVersion getSupportedSourceVersion() {
46 return SourceVersion.latest();
47 }
48
49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
50 if (round == 1) {
51 System.out.println("-- Round 1 --");
52 createPackageFile();
53 } else if (round == 2) {
54 boolean found_foo_A = false;
55 System.out.println("-- Round 2 --");
56 for(Element e: roundEnv.getRootElements()) {
57 System.out.println("ElementKind: " + e.getKind());
58 System.out.println("Modifiers: " + e.getModifiers());
59 System.out.println("Annotations: " + e.getAnnotationMirrors());
60 if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
61 found_foo_A = true;
62 checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
63 checkEqual("Modifiers", e.getModifiers().toString(), "[]");
64 }
65 }
66 if (!found_foo_A)
67 error("did not find @foo.A");
68 }
69 round++;
70 return true;
71 }
72
73 private void createPackageFile() {
74 Filer filer = processingEnv.getFiler();
75
76 String kind = processingEnv.getOptions().get("kind");
77
78 File pkgInfo;
79 if (kind.equals("java"))
80 pkgInfo = new File(System.getProperty("test.src"), "package-info.java");
81 else
82 pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
83
84 byte[] bytes = new byte[(int) pkgInfo.length()];
85 DataInputStream in = null;
86 try {
87 in = new DataInputStream(new FileInputStream(pkgInfo));
88 in.readFully(bytes);
89 } catch (IOException ioe) {
90 error("Couldn't read package info file: " + ioe);
91 } finally {
92 if(in != null) {
93 try {
94 in.close();
95 } catch (IOException e) {
108 } catch (IOException ioe) {
109 error("Couldn't create package info file: " + ioe);
110 } finally {
111 if(out != null) {
112 try {
113 out.close();
114 } catch (IOException e) {
115 error("OutputStream closing failed: " + e);
116 }
117 }
118 }
119 }
120
121 private void checkEqual(String label, String actual, String expect) {
122 if (!actual.equals(expect)) {
123 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
124 }
125 }
126
127 private void error(String msg) {
128 Messager messager = processingEnv.getMessager();
129 messager.printMessage(Kind.ERROR, msg);
130 }
131 }
132
|
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 import java.io.*;
25 import java.util.*;
26 import javax.annotation.processing.*;
27 import javax.lang.model.element.*;
28 import javax.lang.model.SourceVersion;
29 import javax.tools.Diagnostic.Kind;
30
31 /*
32 * @test
33 * @bug 6499119
34 * @summary Created package-info class file modeled improperly
35 * @library ../../lib
36 * @build JavacTestingAbstractProcessor
37 * @compile ClassProcessor.java package-info.java
38 * @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object
39 * @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object
40 */
41
42 @SupportedOptions({ "gen", "expect" })
43 public class ClassProcessor extends JavacTestingAbstractProcessor {
44 int round = 1;
45
46 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
47 if (round == 1) {
48 System.out.println("-- Round 1 --");
49 createPackageFile();
50 } else if (round == 2) {
51 boolean found_foo_A = false;
52 System.out.println("-- Round 2 --");
53 for(Element e: roundEnv.getRootElements()) {
54 System.out.println("ElementKind: " + e.getKind());
55 System.out.println("Modifiers: " + e.getModifiers());
56 System.out.println("Annotations: " + e.getAnnotationMirrors());
57 if (e.getAnnotationMirrors().toString().equals("@foo.A")) {
58 found_foo_A = true;
59 checkEqual("ElementKind", e.getKind().toString(), "PACKAGE");
60 checkEqual("Modifiers", e.getModifiers().toString(), "[]");
61 }
62 }
63 if (!found_foo_A)
64 error("did not find @foo.A");
65 }
66 round++;
67 return true;
68 }
69
70 private void createPackageFile() {
71 String kind = processingEnv.getOptions().get("kind");
72
73 File pkgInfo;
74 if (kind.equals("java"))
75 pkgInfo = new File(System.getProperty("test.src"), "package-info.java");
76 else
77 pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class");
78
79 byte[] bytes = new byte[(int) pkgInfo.length()];
80 DataInputStream in = null;
81 try {
82 in = new DataInputStream(new FileInputStream(pkgInfo));
83 in.readFully(bytes);
84 } catch (IOException ioe) {
85 error("Couldn't read package info file: " + ioe);
86 } finally {
87 if(in != null) {
88 try {
89 in.close();
90 } catch (IOException e) {
103 } catch (IOException ioe) {
104 error("Couldn't create package info file: " + ioe);
105 } finally {
106 if(out != null) {
107 try {
108 out.close();
109 } catch (IOException e) {
110 error("OutputStream closing failed: " + e);
111 }
112 }
113 }
114 }
115
116 private void checkEqual(String label, String actual, String expect) {
117 if (!actual.equals(expect)) {
118 error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect);
119 }
120 }
121
122 private void error(String msg) {
123 messager.printMessage(Kind.ERROR, msg);
124 }
125 }
126
|