1 /***
2 *
3 * Copyright 2004 James Strachan
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.groovy.syntax;
19
20 import org.codehaus.groovy.ast.ModuleNode;
21 import org.codehaus.groovy.control.SourceUnit;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 /***
29 * A common base class of AST helper methods which can be shared across the classic and new parsers
30 *
31 * @author James Strachan
32 * @author Bob McWhirter
33 * @author Sam Pullara
34 * @author Chris Poirier
35 * @version $Revision: 1.9 $
36 */
37 public class ASTHelper {
38
39 private static final String[] EMPTY_STRING_ARRAY = new String[0];
40
41 /*** The SourceUnit controlling us */
42 private SourceUnit controller;
43
44 /*** Our ClassLoader, which provides information on external types */
45 private ClassLoader classLoader;
46
47 /*** Our imports, simple name => fully qualified name */
48 private Map imports;
49 protected ModuleNode output;
50
51 /**</package-summary/html">The package name in which the module sits *//package-summary.html">em>* The package name in which the module sits */
52 private String packageName/package-summary.html">ong> String packageName;
53
54
55 protected static HashMap resolutions = new HashMap();
56
57 private static String NOT_RESOLVED = new String();
58
59 /*** temporarily store the class names that the current modulenode contains */
60 private List newClasses = new ArrayList();
61
62 public ASTHelper(SourceUnit controller, ClassLoader classLoader) {
63 this();
64 this.controller = controller;
65 this.classLoader = classLoader;
66 }
67
68 public ASTHelper() {
69 imports = new HashMap();
70 }
71
72 public String getPackageName() {
73 return</strong> packageName;
74 }
75
76 public void setPackageName(String packageName) {/package-summary.html">ong> void setPackageName(String packageName) {
77 this.packageName = packageName;
78 if (packageName!=null && packageName.length()>0){
79 packageName+='.';
80 }
81 output.setPackageName(packageName);
82 }
83
84
85 /***
86 * Returns our class loader (as supplied on construction).
87 */
88 public ClassLoader getClassLoader() {
89 return classLoader;
90 }
91
92 public void setClassLoader(ClassLoader classLoader) {
93 this.classLoader = classLoader;
94 }
95
96 public SourceUnit getController() {
97 return controller;
98 }
99
100 public void setController(SourceUnit controller) {
101 this.controller = controller;
102 }
103
104 /***
105 * Returns a fully qualified name for any given potential type
106 * name. Returns null if no qualified name could be determined.
107 */
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 /***
256 * Returns two names joined by a dot. If the base name is
257 * empty, returns the name unchanged.
258 */
259 public static String dot(String base, String name) {
260 if (base != null && base.length() > 0) {
261 return base + "." + name;
262 }
263
264 return name;
265 }
266
267 protected void makeModule() {
268 this.newClasses.clear();
269 this.output = new ModuleNode(controller);
270 resolutions.clear();
271 }
272
273 /***
274 * A synonym for <code>dot( base, "" )</code>.
275 */
276 protected String dot(String base) {
277 return dot(base, "");
278 }
279
280
281
282
283
284
285
286
287
288
289 protected void addNewClassName(String name) {
290 this.newClasses.add(name);
291 }
292
293 protected void importClass(String importPackage, String name, String as) {
294 if (as==null) as=name;
295
296 name = dot( importPackage, name );
297
298 output.addImport( as, name );
299 imports.put( as, name );
300 }
301
302 protected void importPackageWithStar(String importPackage) {
303 String[] classes = output.addImportPackage( dot(importPackage) );
304 for( int i = 0; i < classes.length; i++ )
305 {
306 imports.put( classes[i], dot(importPackage, classes[i]) );
307 }
308 }
309 }