author | ohair |
Tue, 25 May 2010 15:54:51 -0700 | |
changeset 5520 | 86e4b9a9da40 |
parent 4871 | 655bba719625 |
child 6720 | f16f91662ad8 |
permissions | -rw-r--r-- |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
1 |
/* |
5520 | 2 |
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. |
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
4 |
* |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
8 |
* |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
13 |
* accompanied this code). |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
14 |
* |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
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. |
|
4871
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
22 |
*/ |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
23 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
24 |
import java.io.*; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
25 |
import java.util.*; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
26 |
import javax.annotation.processing.*; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
27 |
import javax.lang.model.element.*; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
28 |
import javax.lang.model.SourceVersion; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
29 |
import javax.tools.Diagnostic.Kind; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
30 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
31 |
/* |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
32 |
* @test |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
33 |
* @bug 6499119 |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
34 |
* @summary Created package-info class file modeled improperly |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
35 |
* @compile ClassProcessor.java package-info.java |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
36 |
* @compile/process -cp . -processor ClassProcessor -Akind=java java.lang.Object |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
37 |
* @compile/process -cp . -processor ClassProcessor -Akind=class java.lang.Object |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
38 |
*/ |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
39 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
40 |
@SupportedOptions({ "gen", "expect" }) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
41 |
@SupportedAnnotationTypes({"*"}) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
42 |
public class ClassProcessor extends AbstractProcessor { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
43 |
int round = 1; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
44 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
45 |
public SourceVersion getSupportedSourceVersion() { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
46 |
return SourceVersion.latest(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
47 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
48 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
49 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
50 |
if (round == 1) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
51 |
System.out.println("-- Round 1 --"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
52 |
createPackageFile(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
53 |
} else if (round == 2) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
54 |
boolean found_foo_A = false; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
55 |
System.out.println("-- Round 2 --"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
56 |
for(Element e: roundEnv.getRootElements()) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
57 |
System.out.println("ElementKind: " + e.getKind()); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
58 |
System.out.println("Modifiers: " + e.getModifiers()); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
59 |
System.out.println("Annotations: " + e.getAnnotationMirrors()); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
60 |
if (e.getAnnotationMirrors().toString().equals("@foo.A")) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
61 |
found_foo_A = true; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
62 |
checkEqual("ElementKind", e.getKind().toString(), "PACKAGE"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
63 |
checkEqual("Modifiers", e.getModifiers().toString(), "[]"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
64 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
65 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
66 |
if (!found_foo_A) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
67 |
error("did not find @foo.A"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
68 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
69 |
round++; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
70 |
return true; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
71 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
72 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
73 |
private void createPackageFile() { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
74 |
Filer filer = processingEnv.getFiler(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
75 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
76 |
String kind = processingEnv.getOptions().get("kind"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
77 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
78 |
File pkgInfo; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
79 |
if (kind.equals("java")) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
80 |
pkgInfo = new File(System.getProperty("test.src"), "package-info.java"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
81 |
else |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
82 |
pkgInfo = new File(System.getProperty("test.classes"), "foo/package-info.class"); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
83 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
84 |
byte[] bytes = new byte[(int) pkgInfo.length()]; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
85 |
DataInputStream in = null; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
86 |
try { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
87 |
in = new DataInputStream(new FileInputStream(pkgInfo)); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
88 |
in.readFully(bytes); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
89 |
} catch (IOException ioe) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
90 |
error("Couldn't read package info file: " + ioe); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
91 |
} finally { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
92 |
if(in != null) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
93 |
try { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
94 |
in.close(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
95 |
} catch (IOException e) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
96 |
error("InputStream closing failed: " + e); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
97 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
98 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
99 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
100 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
101 |
OutputStream out = null; |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
102 |
try { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
103 |
if (kind.equals("java")) |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
104 |
out = filer.createSourceFile("foo.package-info").openOutputStream(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
105 |
else |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
106 |
out = filer.createClassFile("foo.package-info").openOutputStream(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
107 |
out.write(bytes, 0, bytes.length); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
108 |
} catch (IOException ioe) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
109 |
error("Couldn't create package info file: " + ioe); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
110 |
} finally { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
111 |
if(out != null) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
112 |
try { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
113 |
out.close(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
114 |
} catch (IOException e) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
115 |
error("OutputStream closing failed: " + e); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
116 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
117 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
118 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
119 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
120 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
121 |
private void checkEqual(String label, String actual, String expect) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
122 |
if (!actual.equals(expect)) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
123 |
error("Unexpected value for " + label + "; actual=" + actual + ", expected=" + expect); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
124 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
125 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
126 |
|
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
127 |
private void error(String msg) { |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
128 |
Messager messager = processingEnv.getMessager(); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
129 |
messager.printMessage(Kind.ERROR, msg); |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
130 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
131 |
} |
655bba719625
6499119: Created package-info class file modeled improperly
jjg
parents:
diff
changeset
|
132 |