|
1 /* |
|
2 * Copyright 2010 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any 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) { |
|
96 error("InputStream closing failed: " + e); |
|
97 } |
|
98 } |
|
99 } |
|
100 |
|
101 OutputStream out = null; |
|
102 try { |
|
103 if (kind.equals("java")) |
|
104 out = filer.createSourceFile("foo.package-info").openOutputStream(); |
|
105 else |
|
106 out = filer.createClassFile("foo.package-info").openOutputStream(); |
|
107 out.write(bytes, 0, bytes.length); |
|
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 |