1 /* |
|
2 * Copyright (c) 2004, 2007, Oracle and/or its affiliates. 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 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 |
|
25 /* |
|
26 * A utility used to invoke and test the apt tool. |
|
27 * Tests should subclass Tester, and invoke run(). |
|
28 * |
|
29 * @author Scott Seligman |
|
30 */ |
|
31 |
|
32 import java.io.*; |
|
33 import java.util.*; |
|
34 import com.sun.mirror.apt.*; |
|
35 import com.sun.mirror.declaration.*; |
|
36 |
|
37 |
|
38 public abstract class Tester { |
|
39 |
|
40 /** |
|
41 * The declaration corresponding to this tester's class. Set by |
|
42 * TestProcessorFactory after the constructor completes, and |
|
43 * before init() is invoked. |
|
44 */ |
|
45 ClassDeclaration thisClassDecl; |
|
46 |
|
47 /** |
|
48 * The environment for this apt run. Set by TestProcessorFactory |
|
49 * after the constructor completes, and before init() is invoked. |
|
50 */ |
|
51 AnnotationProcessorEnvironment env; |
|
52 |
|
53 |
|
54 // TestProcessorFactory looks here to find the tester that's running |
|
55 // when it's invoked. |
|
56 static Tester activeTester; |
|
57 |
|
58 private static final String[] DEFAULT_ARGS = { |
|
59 "-nocompile", |
|
60 "-XPrintAptRounds", |
|
61 "-XListDeclarations", |
|
62 }; |
|
63 private static final String[] NO_STRINGS = {}; |
|
64 |
|
65 // Force processor and factory to be compiled |
|
66 private static Class dummy = TestProcessorFactory.class; |
|
67 |
|
68 private final String testSrc = System.getProperty("test.src", "."); |
|
69 private final String testClasses = System.getProperty("test.classes", "."); |
|
70 |
|
71 // apt command-line args |
|
72 private String[] args; |
|
73 |
|
74 |
|
75 static { |
|
76 // Enable assertions in the unnamed package. |
|
77 ClassLoader loader = Tester.class.getClassLoader(); |
|
78 if (loader != null) { |
|
79 loader.setPackageAssertionStatus(null, true); |
|
80 } |
|
81 } |
|
82 |
|
83 |
|
84 protected Tester(String... additionalArgs) { |
|
85 String sourceFile = testSrc + File.separator + |
|
86 getClass().getName() + ".java"; |
|
87 |
|
88 ArrayList<String> as = new ArrayList<String>(); |
|
89 Collections.addAll(as, DEFAULT_ARGS); |
|
90 as.add("-sourcepath"); as.add(testSrc); |
|
91 as.add("-factory"); as.add(TestProcessorFactory.class.getName()); |
|
92 Collections.addAll(as, additionalArgs); |
|
93 as.add(sourceFile); |
|
94 args = as.toArray(NO_STRINGS); |
|
95 } |
|
96 |
|
97 /** |
|
98 * Run apt. |
|
99 */ |
|
100 protected void run() { |
|
101 activeTester = this; |
|
102 if (com.sun.tools.apt.Main.process(args) != 0) { |
|
103 throw new Error("apt errors encountered."); |
|
104 } |
|
105 } |
|
106 |
|
107 /** |
|
108 * Called after thisClassDecl and env have been set, but before any |
|
109 * tests are run, to allow the tester subclass to perform any |
|
110 * needed initialization. |
|
111 */ |
|
112 protected void init() { |
|
113 } |
|
114 |
|
115 /** |
|
116 * Returns the declaration of a named method in this class. If this |
|
117 * method name is overloaded, one method is chosen arbitrarily. |
|
118 * Returns null if no method is found. |
|
119 */ |
|
120 protected MethodDeclaration getMethod(String methodName) { |
|
121 for (MethodDeclaration m : thisClassDecl.getMethods()) { |
|
122 if (methodName.equals(m.getSimpleName())) { |
|
123 return m; |
|
124 } |
|
125 } |
|
126 return null; |
|
127 } |
|
128 |
|
129 /** |
|
130 * Returns the declaration of a named field in this class. |
|
131 * Returns null if no field is found. |
|
132 */ |
|
133 protected FieldDeclaration getField(String fieldName) { |
|
134 for (FieldDeclaration f : thisClassDecl.getFields()) { |
|
135 if (fieldName.equals(f.getSimpleName())) { |
|
136 return f; |
|
137 } |
|
138 } |
|
139 return null; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Returns the annotation mirror of a given type on a named method |
|
144 * in this class. If this method name is overloaded, one method is |
|
145 * chosen arbitrarily. Returns null if no appropriate annotation |
|
146 * is found. |
|
147 */ |
|
148 protected AnnotationMirror getAnno(String methodName, String annoType) { |
|
149 MethodDeclaration m = getMethod(methodName); |
|
150 if (m != null) { |
|
151 TypeDeclaration at = env.getTypeDeclaration(annoType); |
|
152 for (AnnotationMirror a : m.getAnnotationMirrors()) { |
|
153 if (at == a.getAnnotationType().getDeclaration()) { |
|
154 return a; |
|
155 } |
|
156 } |
|
157 } |
|
158 return null; |
|
159 } |
|
160 } |
|