KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > ElementHandleTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.java.source;
21
22 import com.sun.tools.javac.model.JavacElements;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.PrintWriter JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import javax.lang.model.type.DeclaredType;
30 import javax.lang.model.type.TypeKind;
31 import junit.framework.*;
32 import java.util.List JavaDoc;
33 import javax.lang.model.element.Element;
34 import javax.lang.model.element.ElementKind;
35 import javax.lang.model.element.ExecutableElement;
36 import javax.lang.model.element.TypeElement;
37 import javax.lang.model.element.TypeParameterElement;
38 import javax.lang.model.element.VariableElement;
39 import javax.lang.model.type.TypeMirror;
40 import org.netbeans.api.java.classpath.ClassPath;
41 import org.netbeans.junit.NbTestCase;
42 import org.netbeans.spi.java.classpath.ClassPathProvider;
43 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
44 import org.openide.filesystems.FileLock;
45 import org.openide.filesystems.FileObject;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.util.Lookup;
48 import org.openide.util.lookup.Lookups;
49 import org.openide.util.lookup.ProxyLookup;
50
51 /**
52  *
53  * @author Tomas Zezula
54  */

55 public class ElementHandleTest extends NbTestCase {
56     
57     private FileObject src;
58     private FileObject data;
59     
60     
61     static {
62         ElementHandleTest.class.getClassLoader().setDefaultAssertionStatus(true);
63         System.setProperty("org.openide.util.Lookup", ElementHandleTest.Lkp.class.getName());
64         Assert.assertEquals(ElementHandleTest.Lkp.class, Lookup.getDefault().getClass());
65     }
66     
67     public static class Lkp extends ProxyLookup {
68         
69         private static Lkp DEFAULT;
70         
71         public Lkp () {
72             Assert.assertNull(DEFAULT);
73             DEFAULT = this;
74             ClassLoader JavaDoc l = Lkp.class.getClassLoader();
75             this.setLookups(
76                  new Lookup [] {
77                     Lookups.metaInfServices(l),
78                     Lookups.singleton(l),
79                     Lookups.singleton(ClassPathProviderImpl.getDefault()),
80             });
81         }
82         
83     }
84     
85     
86     public ElementHandleTest(String JavaDoc testName) {
87         super(testName);
88     }
89
90     protected void setUp() throws Exception JavaDoc {
91         clearWorkDir();
92         FileObject wd = FileUtil.toFileObject(this.getWorkDir());
93         assertNotNull(wd);
94         this.src = wd.createFolder("src");
95         this.data = src.createData("Test","java");
96         FileLock lock = data.lock();
97         try {
98             PrintWriter JavaDoc out = new PrintWriter JavaDoc ( new OutputStreamWriter JavaDoc (data.getOutputStream(lock)));
99             try {
100                 out.println ("public class Test {}");
101             } finally {
102                 out.close ();
103             }
104         } finally {
105             lock.releaseLock();
106         }
107         ClassPathProviderImpl.getDefault().setClassPaths(createBootPath(),ClassPathSupport.createClassPath(new URL JavaDoc[0]),ClassPathSupport.createClassPath(new FileObject[]{this.src}));
108     }
109
110     protected void tearDown() throws Exception JavaDoc {
111     }
112
113
114     public void testElementHandle() throws Exception JavaDoc {
115         final JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathProviderImpl.getDefault().findClassPath(data,ClassPath.BOOT), ClassPathProviderImpl.getDefault().findClassPath(data, ClassPath.COMPILE), null));
116         assertNotNull(js);
117         final ElementHandle[] utilElementHandle = new ElementHandle[1];
118         final ElementHandle[] stringElementHandle = new ElementHandle[1];
119         final ElementHandle[] stringLengthElementHandle = new ElementHandle[1];
120         final ElementHandle[] stringConstructorElementHandle = new ElementHandle[1];
121         final ElementHandle[] stringCountElementHandle = new ElementHandle[1];
122         final ElementHandle[] retentionPolicyHandle = new ElementHandle[1];
123         final ElementHandle[] retentionPolicyClassHandle = new ElementHandle[1];
124         final ElementHandle[] innerClassHandle = new ElementHandle[1];
125         final ElementHandle[] collectionAddHandle = new ElementHandle[1];
126         final ElementHandle[] annonClassHandle = new ElementHandle[1];
127         final ElementHandle[] genParList = new ElementHandle[1];
128         final ElementHandle[] genParColsMin = new ElementHandle[1];
129         
130         js.runUserActionTask(new CancellableTask<CompilationController>() {
131             public void run(CompilationController parameter) {
132                 JavacElements elements = (JavacElements) parameter.getElements();
133                 Element utilElement = elements.getPackageElement("java.util");
134                 assertNotNull (utilElement);
135                 utilElementHandle[0] = ElementHandle.create(utilElement);
136                 assertNotNull (utilElementHandle[0]);
137                 Element stringElement = elements.getTypeElement (String JavaDoc.class.getName());
138                 assertNotNull (stringElement);
139                 stringElementHandle[0] = ElementHandle.create(stringElement);
140                 assertNotNull (stringElementHandle[0]);
141                 Element[] stringMembers = getStringElements(stringElement);
142                 assertEquals(3,stringMembers.length);
143                 assertNotNull (stringMembers[0]);
144                 assertNotNull (stringMembers[1]);
145                 assertNotNull (stringMembers[2]);
146                 stringLengthElementHandle[0] = ElementHandle.create(stringMembers[0]);
147                 assertNotNull (stringLengthElementHandle[0]);
148                 stringConstructorElementHandle[0] = ElementHandle.create(stringMembers[1]);
149                 assertNotNull (stringConstructorElementHandle[0]);
150                 stringCountElementHandle[0] = ElementHandle.create(stringMembers[2]);
151                 assertNotNull (stringCountElementHandle[0]);
152                 Element retentionPolicy = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
153                 assertNotNull (retentionPolicy);
154                 retentionPolicyHandle[0] = ElementHandle.create(retentionPolicy);
155                 assertNotNull (retentionPolicyHandle[0]);
156                 List JavaDoc<? extends Element> members = ((TypeElement)retentionPolicy).getEnclosedElements();
157                 for (Element member : members) {
158                     if (member.getKind() == ElementKind.ENUM_CONSTANT && "CLASS".contentEquals(((VariableElement)member).getSimpleName())) {
159                         retentionPolicyClassHandle[0] = ElementHandle.create(member);
160                         break;
161                     }
162                 }
163                 assertNotNull (retentionPolicyClassHandle[0]);
164                 Element csl = elements.getTypeElementByBinaryName("java.util.Collections$SingletonList");
165                 assertNotNull (csl);
166                 innerClassHandle[0] = ElementHandle.create(csl);
167                 assertNotNull (innerClassHandle[0]);
168                 Element cadd = getCollectionAdd (elements.getTypeElement(java.util.Collection JavaDoc.class.getName()));
169                 assertNotNull (cadd);
170                 collectionAddHandle[0] = ElementHandle.create(cadd);
171                 assertNotNull (collectionAddHandle[0]);
172                 TypeElement annonClass = elements.getTypeElementByBinaryName("java.lang.String$1"); //NOI18N
173
assertNotNull (annonClass);
174                 annonClassHandle[0] = ElementHandle.create(annonClass);
175                 assertNotNull (annonClassHandle[0]);
176                 TypeElement listClass = elements.getTypeElementByBinaryName("java.util.List"); //NOI18N
177
assertNotNull(listClass);
178                 List JavaDoc<? extends TypeParameterElement> tpes = listClass.getTypeParameters();
179                 assertEquals(tpes.size(), 1);
180                 genParList[0] = ElementHandle.create(tpes.get(0));
181                 TypeElement collsClass = elements.getTypeElementByBinaryName("java.util.Collections"); //NOI18N
182
assertNotNull(collsClass);
183                 for (Element member : collsClass.getEnclosedElements()) {
184                     if (member.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("min")) {
185                         ExecutableElement ee = (ExecutableElement) member;
186                         if (ee.getParameters().size() == 1) {
187                             genParColsMin[0] = ElementHandle.create(ee.getTypeParameters().get(0));
188                         }
189                     }
190                 }
191                 assertNotNull(genParColsMin[0]);
192             }
193
194             public void cancel() {
195             }
196             
197         },true);
198         
199         js.runUserActionTask(new CancellableTask<CompilationController>() {
200             public void run(CompilationController parameter) {
201                 JavacElements elements = (JavacElements) parameter.getElements();
202                 Element resolved = utilElementHandle[0].resolve(parameter);
203                 assertNotNull (resolved);
204                 Element utilElement = elements.getPackageElement("java.util");
205                 assertNotNull (utilElement);
206                 assertEquals(resolved,utilElement);
207                 resolved = stringElementHandle[0].resolve(parameter);
208                 assertNotNull (resolved);
209                 Element stringElement = elements.getTypeElement(String JavaDoc.class.getName());
210                 assertNotNull (stringElement);
211                 assertEquals(resolved,stringElement);
212                 Element[] stringMembers = getStringElements(stringElement);
213                 assertEquals(3,stringMembers.length);
214                 assertNotNull (stringMembers[0]);
215                 assertNotNull (stringMembers[1]);
216                 assertNotNull (stringMembers[2]);
217                 resolved = stringLengthElementHandle[0].resolve(parameter);
218                 assertNotNull (resolved);
219                 assertEquals(resolved,stringMembers[0]);
220                 resolved = stringConstructorElementHandle[0].resolve(parameter);
221                 assertNotNull (resolved);
222                 assertEquals(resolved,stringMembers[1]);
223                 resolved = stringCountElementHandle[0].resolve(parameter);
224                 assertNotNull (resolved);
225                 assertEquals(resolved,stringMembers[2]);
226                 resolved = retentionPolicyHandle[0].resolve(parameter);
227                 assertNotNull (resolved);
228                 Element retentionPolicy = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
229                 assertNotNull (retentionPolicy);
230                 assertEquals(resolved, retentionPolicy);
231                 List JavaDoc<? extends Element> members = ((TypeElement)retentionPolicy).getEnclosedElements();
232                 Element retentionClassElement = null;
233                 for (Element member : members) {
234                     if (member.getKind() == ElementKind.ENUM_CONSTANT && "CLASS".contentEquals(((VariableElement)member).getSimpleName())) {
235                         retentionClassElement = member;
236                         break;
237                     }
238                 }
239                 assertNotNull (retentionClassElement);
240                 resolved = retentionPolicyClassHandle[0].resolve(parameter);
241                 assertNotNull (resolved);
242                 assertEquals(resolved,retentionClassElement);
243                 resolved = innerClassHandle[0].resolve(parameter);
244                 assertNotNull (resolved);
245                 Element csl = elements.getTypeElementByBinaryName("java.util.Collections$SingletonList");
246                 assertNotNull (csl);
247                 assertEquals(resolved,csl);
248                 resolved = collectionAddHandle[0].resolve(parameter);
249                 assertNotNull(resolved);
250                 Element cadd = getCollectionAdd(elements.getTypeElement(java.util.Collection JavaDoc.class.getName()));
251                 assertNotNull(cadd);
252                 assertEquals (resolved, cadd);
253                 resolved = annonClassHandle[0].resolve(parameter);
254                 assertNotNull (resolved);
255                 TypeElement annonClass = elements.getTypeElementByBinaryName("java.lang.String$1"); //NOI18N
256
assertNotNull (annonClass);
257                 assertEquals(resolved,annonClass);
258                 
259                 TypeElement listClass = elements.getTypeElementByBinaryName("java.util.List"); //NOI18N
260
assertNotNull(listClass);
261                 TypeParameterElement tpe = listClass.getTypeParameters().get(0);
262                 resolved = genParList[0].resolve(parameter);
263                 assertEquals(tpe, resolved);
264                 
265                 tpe = null;
266                 TypeElement collsClass = elements.getTypeElementByBinaryName("java.util.Collections"); //NOI18N
267
assertNotNull(collsClass);
268                 for (Element member : collsClass.getEnclosedElements()) {
269                     if (member.getKind() == ElementKind.METHOD && member.getSimpleName().contentEquals("min")) {
270                         ExecutableElement ee = (ExecutableElement) member;
271                         if (ee.getParameters().size() == 1) {
272                             tpe = ee.getTypeParameters().get(0);
273                         }
274                     }
275                 }
276                 assertNotNull(tpe);
277                 resolved = genParColsMin[0].resolve(parameter);
278                 assertEquals(tpe, resolved);
279             }
280
281             public void cancel() {
282             }
283             
284         },true);
285         
286     }
287     
288     public void testSignatureEquals() throws Exception JavaDoc {
289         final JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathProviderImpl.getDefault().findClassPath(data,ClassPath.BOOT), ClassPathProviderImpl.getDefault().findClassPath(data, ClassPath.COMPILE), null));
290         assertNotNull(js);
291         final ElementHandle[] utilElementHandle = new ElementHandle[1];
292         final ElementHandle[] stringElementHandle = new ElementHandle[1];
293         final ElementHandle[] stringLengthElementHandle = new ElementHandle[1];
294         final ElementHandle[] stringConstructorElementHandle = new ElementHandle[1];
295         final ElementHandle[] stringCountElementHandle = new ElementHandle[1];
296         final ElementHandle[] retentionPolicyHandle = new ElementHandle[1];
297         final ElementHandle[] retentionPolicyClassHandle = new ElementHandle[1];
298         final ElementHandle[] innerClassHandle = new ElementHandle[1];
299         final ElementHandle[] collectionAddHandle = new ElementHandle[1];
300         final ElementHandle[] annonClassHandle = new ElementHandle[1];
301         
302         js.runUserActionTask(new CancellableTask<CompilationController>() {
303             public void run(CompilationController parameter) {
304                 JavacElements elements = (JavacElements) parameter.getElements();
305                 Element utilElement = elements.getPackageElement("java.util");
306                 assertNotNull (utilElement);
307                 utilElementHandle[0] = ElementHandle.create(utilElement);
308                 assertNotNull (utilElementHandle[0]);
309                 Element stringElement = elements.getTypeElement (String JavaDoc.class.getName());
310                 assertNotNull (stringElement);
311                 stringElementHandle[0] = ElementHandle.create(stringElement);
312                 assertNotNull (stringElementHandle[0]);
313                 Element[] stringMembers = getStringElements(stringElement);
314                 assertEquals(3,stringMembers.length);
315                 assertNotNull (stringMembers[0]);
316                 assertNotNull (stringMembers[1]);
317                 assertNotNull (stringMembers[2]);
318                 stringLengthElementHandle[0] = ElementHandle.create(stringMembers[0]);
319                 assertNotNull (stringLengthElementHandle[0]);
320                 stringConstructorElementHandle[0] = ElementHandle.create(stringMembers[1]);
321                 assertNotNull (stringConstructorElementHandle[0]);
322                 stringCountElementHandle[0] = ElementHandle.create(stringMembers[2]);
323                 assertNotNull (stringCountElementHandle[0]);
324                 Element retentionPolicy = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
325                 assertNotNull (retentionPolicy);
326                 retentionPolicyHandle[0] = ElementHandle.create(retentionPolicy);
327                 assertNotNull (retentionPolicyHandle[0]);
328                 List JavaDoc<? extends Element> members = ((TypeElement)retentionPolicy).getEnclosedElements();
329                 for (Element member : members) {
330                     if (member.getKind() == ElementKind.ENUM_CONSTANT && "CLASS".contentEquals(((VariableElement)member).getSimpleName())) {
331                         retentionPolicyClassHandle[0] = ElementHandle.create(member);
332                         break;
333                     }
334                 }
335                 assertNotNull (retentionPolicyClassHandle[0]);
336                 Element csl = elements.getTypeElement("java.util.Collections.SingletonList");
337                 assertNotNull (csl);
338                 innerClassHandle[0] = ElementHandle.create(csl);
339                 assertNotNull (innerClassHandle[0]);
340                 Element cadd = getCollectionAdd (elements.getTypeElement(java.util.Collection JavaDoc.class.getName()));
341                 assertNotNull (cadd);
342                 collectionAddHandle[0] = ElementHandle.create(cadd);
343                 assertNotNull (collectionAddHandle[0]);
344                 TypeElement annonClass = elements.getTypeElementByBinaryName("java.lang.String$1"); //NOI18N
345
assertNotNull (annonClass);
346                 annonClassHandle[0] = ElementHandle.create(annonClass);
347                 assertNotNull (annonClassHandle[0]);
348             }
349
350             public void cancel() {
351             }
352             
353         },true);
354         
355         js.runUserActionTask(new CancellableTask<CompilationController>() {
356             public void run(CompilationController parameter) {
357                 JavacElements elements = (JavacElements)parameter.getElements();
358                 Element utilElement = elements.getPackageElement("java.util");
359                 assertNotNull (utilElement);
360                 assertTrue (utilElementHandle[0].signatureEquals(utilElement));
361                 Element stringElement = elements.getTypeElement(String JavaDoc.class.getName());
362                 assertNotNull (stringElement);
363                 assertTrue(stringElementHandle[0].signatureEquals(stringElement));
364                 Element[] stringMembers = getStringElements(stringElement);
365                 assertEquals(3,stringMembers.length);
366                 assertNotNull (stringMembers[0]);
367                 assertNotNull (stringMembers[1]);
368                 assertNotNull (stringMembers[2]);
369                 assertTrue(stringLengthElementHandle[0].signatureEquals(stringMembers[0]));
370                 assertTrue(stringConstructorElementHandle[0].signatureEquals(stringMembers[1]));
371                 assertTrue(stringCountElementHandle[0].signatureEquals(stringMembers[2]));
372                 Element retentionPolicy = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
373                 assertNotNull (retentionPolicy);
374                 assertTrue(retentionPolicyHandle[0].signatureEquals(retentionPolicy));
375                 List JavaDoc<? extends Element> members = ((TypeElement)retentionPolicy).getEnclosedElements();
376                 Element retentionClassElement = null;
377                 for (Element member : members) {
378                     if (member.getKind() == ElementKind.ENUM_CONSTANT && "CLASS".contentEquals(((VariableElement)member).getSimpleName())) {
379                         retentionClassElement = member;
380                         break;
381                     }
382                 }
383                 assertNotNull (retentionClassElement);
384                 assertTrue(retentionPolicyClassHandle[0].signatureEquals(retentionClassElement));
385                 Element csl = elements.getTypeElementByBinaryName("java.util.Collections$SingletonList");
386                 assertNotNull (csl);
387                 assertTrue(innerClassHandle[0].signatureEquals(csl));
388                 Element cadd = getCollectionAdd(elements.getTypeElement(java.util.Collection JavaDoc.class.getName()));
389                 assertNotNull(cadd);
390                 assertTrue (collectionAddHandle[0].signatureEquals(cadd));
391                 TypeElement annonClass = elements.getTypeElementByBinaryName("java.lang.String$1"); //NOI18N
392
assertNotNull (annonClass);
393                 assertTrue(annonClassHandle[0].signatureEquals(annonClass));
394             }
395
396             public void cancel() {
397             }
398             
399         },true);
400         
401     }
402     
403     
404     public void testEquals() throws Exception JavaDoc {
405         final JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathProviderImpl.getDefault().findClassPath(data,ClassPath.BOOT), ClassPathProviderImpl.getDefault().findClassPath(data, ClassPath.COMPILE), null));
406         assertNotNull(js);
407         final ElementHandle[] utilElementHandle = new ElementHandle[1];
408         final ElementHandle[] stringElementHandle = new ElementHandle[1];
409         final ElementHandle[] stringLengthElementHandle = new ElementHandle[1];
410         final ElementHandle[] stringConstructorElementHandle = new ElementHandle[1];
411         final ElementHandle[] stringCountElementHandle = new ElementHandle[1];
412         final ElementHandle[] retentionPolicyHandle = new ElementHandle[1];
413         final ElementHandle[] retentionPolicyClassHandle = new ElementHandle[1];
414         final ElementHandle[] innerClassHandle = new ElementHandle[1];
415         final ElementHandle[] collectionAddHandle = new ElementHandle[1];
416         final ElementHandle[] annonClassHandle = new ElementHandle[1];
417         
418         js.runUserActionTask(new CancellableTask<CompilationController>() {
419             public void run(CompilationController parameter) {
420                 JavacElements elements = (JavacElements) parameter.getElements();
421                 Element utilElement = elements.getPackageElement("java.util");
422                 assertNotNull (utilElement);
423                 utilElementHandle[0] = ElementHandle.create(utilElement);
424                 assertNotNull (utilElementHandle[0]);
425                 Element stringElement = elements.getTypeElement (String JavaDoc.class.getName());
426                 assertNotNull (stringElement);
427                 stringElementHandle[0] = ElementHandle.create(stringElement);
428                 assertNotNull (stringElementHandle[0]);
429                 Element[] stringMembers = getStringElements(stringElement);
430                 assertEquals(3,stringMembers.length);
431                 assertNotNull (stringMembers[0]);
432                 assertNotNull (stringMembers[1]);
433                 assertNotNull (stringMembers[2]);
434                 stringLengthElementHandle[0] = ElementHandle.create(stringMembers[0]);
435                 assertNotNull (stringLengthElementHandle[0]);
436                 stringConstructorElementHandle[0] = ElementHandle.create(stringMembers[1]);
437                 assertNotNull (stringConstructorElementHandle[0]);
438                 stringCountElementHandle[0] = ElementHandle.create(stringMembers[2]);
439                 assertNotNull (stringCountElementHandle[0]);
440                 Element retentionPolicy = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
441                 assertNotNull (retentionPolicy);
442                 retentionPolicyHandle[0] = ElementHandle.create(retentionPolicy);
443                 assertNotNull (retentionPolicyHandle[0]);
444                 List JavaDoc<? extends Element> members = ((TypeElement)retentionPolicy).getEnclosedElements();
445                 for (Element member : members) {
446                     if (member.getKind() == ElementKind.ENUM_CONSTANT && "CLASS".contentEquals(((VariableElement)member).getSimpleName())) {
447                         retentionPolicyClassHandle[0] = ElementHandle.create(member);
448                         break;
449                     }
450                 }
451                 assertNotNull (retentionPolicyClassHandle[0]);
452                 Element csl = elements.getTypeElement("java.util.Collections.SingletonList");
453                 assertNotNull (csl);
454                 innerClassHandle[0] = ElementHandle.create(csl);
455                 assertNotNull (innerClassHandle[0]);
456                 Element cadd = getCollectionAdd (elements.getTypeElement(java.util.Collection JavaDoc.class.getName()));
457                 assertNotNull (cadd);
458                 collectionAddHandle[0] = ElementHandle.create(cadd);
459                 assertNotNull (collectionAddHandle[0]);
460                 TypeElement annonClass = elements.getTypeElementByBinaryName("java.lang.String$1"); //NOI18N
461
assertNotNull (annonClass);
462                 annonClassHandle[0] = ElementHandle.create(annonClass);
463                 assertNotNull (annonClassHandle[0]);
464             }
465
466             public void cancel() {
467             }
468             
469         },true);
470         
471         
472         assertTrue (utilElementHandle[0].equals(utilElementHandle[0]));
473         assertTrue (stringElementHandle[0].equals(stringElementHandle[0]));
474         assertTrue (stringLengthElementHandle[0].equals(stringLengthElementHandle[0]));
475         assertTrue (stringConstructorElementHandle[0].equals(stringConstructorElementHandle[0]));
476         assertTrue (stringCountElementHandle[0].equals(stringCountElementHandle[0]));
477         assertTrue (retentionPolicyHandle[0].equals(retentionPolicyHandle[0]));
478         assertTrue (retentionPolicyClassHandle[0].equals(retentionPolicyClassHandle[0]));
479         assertTrue (innerClassHandle[0].equals(innerClassHandle[0]));
480         assertTrue (collectionAddHandle[0].equals(collectionAddHandle[0]));
481         assertTrue (annonClassHandle[0].equals(annonClassHandle[0]));
482         
483         assertFalse (utilElementHandle[0].equals(stringElementHandle[0]));
484         assertFalse (utilElementHandle[0].equals(stringLengthElementHandle[0]));
485         assertFalse (utilElementHandle[0].equals(stringConstructorElementHandle[0]));
486         assertFalse (utilElementHandle[0].equals(stringCountElementHandle[0]));
487         assertFalse (utilElementHandle[0].equals(retentionPolicyHandle[0]));
488         assertFalse(utilElementHandle[0].equals(retentionPolicyClassHandle[0]));
489         assertFalse (utilElementHandle[0].equals(innerClassHandle[0]));
490         assertFalse (utilElementHandle[0].equals(collectionAddHandle[0]));
491         assertFalse (utilElementHandle[0].equals(annonClassHandle[0]));
492         
493         assertTrue (utilElementHandle[0].hashCode() == utilElementHandle[0].hashCode());
494         assertTrue (stringElementHandle[0].hashCode() == stringElementHandle[0].hashCode());
495         assertTrue (stringLengthElementHandle[0].hashCode() == stringLengthElementHandle[0].hashCode());
496         assertTrue (stringConstructorElementHandle[0].hashCode() == stringConstructorElementHandle[0].hashCode());
497         assertTrue (stringCountElementHandle[0].hashCode() == stringCountElementHandle[0].hashCode());
498         assertTrue (retentionPolicyHandle[0].hashCode() == retentionPolicyHandle[0].hashCode());
499         assertTrue (retentionPolicyClassHandle[0].hashCode() == retentionPolicyClassHandle[0].hashCode());
500         assertTrue (innerClassHandle[0].hashCode() == innerClassHandle[0].hashCode());
501         assertTrue (collectionAddHandle[0].hashCode() == collectionAddHandle[0].hashCode());
502         assertTrue (annonClassHandle[0].hashCode() == annonClassHandle[0].hashCode());
503         
504         assertFalse (stringElementHandle[0].hashCode() == utilElementHandle[0].hashCode());
505         assertTrue (stringLengthElementHandle[0].hashCode() == stringCountElementHandle[0].hashCode());
506         assertFalse (stringLengthElementHandle[0].hashCode() == retentionPolicyHandle[0].hashCode());
507         assertFalse (stringLengthElementHandle[0].hashCode() == retentionPolicyClassHandle[0].hashCode());
508         assertFalse (stringLengthElementHandle[0].hashCode() == innerClassHandle[0].hashCode());
509         assertFalse (stringLengthElementHandle[0].hashCode() == collectionAddHandle[0].hashCode());
510         assertFalse (stringLengthElementHandle[0].hashCode() == annonClassHandle[0].hashCode());
511         
512     }
513     
514     
515     public void testNames () throws Exception JavaDoc {
516         final JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathProviderImpl.getDefault().findClassPath(data,ClassPath.BOOT), ClassPathProviderImpl.getDefault().findClassPath(data, ClassPath.COMPILE), null));
517         assertNotNull(js);
518         js.runUserActionTask(new CancellableTask<CompilationController>() {
519             public void run(CompilationController parameter) {
520                 final JavacElements elements = (JavacElements) parameter.getElements();
521                 Element element = elements.getTypeElement (String JavaDoc.class.getName());
522                 assertNotNull (element);
523                 ElementHandle handle = ElementHandle.create(element);
524                 assertEquals("java.lang.String", handle.getBinaryName());
525                 assertEquals("java.lang.String", handle.getQualifiedName());
526                 
527                 element = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
528                 assertNotNull (element);
529                 handle = ElementHandle.create(element);
530                 assertEquals("java.lang.annotation.RetentionPolicy", handle.getBinaryName());
531                 assertEquals("java.lang.annotation.RetentionPolicy", handle.getQualifiedName());
532                 
533                 element = elements.getTypeElement(java.io.Externalizable JavaDoc.class.getName());
534                 assertNotNull(element);
535                 handle = ElementHandle.create(element);
536                 assertEquals("java.io.Externalizable", handle.getBinaryName());
537                 assertEquals("java.io.Externalizable", handle.getQualifiedName());
538                 
539                 element = elements.getTypeElement(java.lang.annotation.RetentionPolicy JavaDoc.class.getName());
540                 assertNotNull(element);
541                 handle = ElementHandle.create(element);
542                 assertEquals("java.lang.annotation.RetentionPolicy", handle.getBinaryName());
543                 assertEquals("java.lang.annotation.RetentionPolicy", handle.getQualifiedName());
544                 
545                 element = elements.getTypeElementByBinaryName("java.util.Collections$SingletonList");
546                 assertNotNull (element);
547                 handle = ElementHandle.create(element);
548                 assertEquals("java.util.Collections$SingletonList", handle.getBinaryName());
549                 assertEquals("java.util.Collections.SingletonList", handle.getQualifiedName());
550                 
551                 element = elements.getPackageElement("java.lang");
552                 assertNotNull (element);
553                 handle = ElementHandle.create(element);
554                 try {
555                     handle.getBinaryName();
556                     assertFalse(true);
557                 } catch (IllegalStateException JavaDoc is) {
558                 }
559                 try {
560                     handle.getQualifiedName();
561                     assertFalse (true);
562                 } catch (IllegalStateException JavaDoc is) {
563                     
564                 }
565             }
566             
567             public void cancel () {
568             }
569         },true);
570     }
571     
572     public void testTypesRankNPE () throws Exception JavaDoc {
573         final JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathProviderImpl.getDefault().findClassPath(data,ClassPath.BOOT), ClassPathProviderImpl.getDefault().findClassPath(data, ClassPath.COMPILE), null));
574         assertNotNull(js);
575         js.runUserActionTask(new CancellableTask<CompilationController>() {
576
577             public void cancel() {
578             }
579
580             public void run(CompilationController parameter) throws Exception JavaDoc {
581                 JavacElements elements = (JavacElements) parameter.getElements();
582                 TypeElement te = elements.getTypeElementByBinaryName("java.lang.String$1");
583                 List JavaDoc<? extends Element> content = elements.getAllMembers(te);
584             }
585         }, true);
586     }
587     
588     private Element[] getStringElements (Element stringElement) {
589         List JavaDoc<? extends Element> members = ((TypeElement)stringElement).getEnclosedElements();
590         Element[] result = new Element[3];
591         for (Element member : members) {
592             if (member.getKind() == ElementKind.METHOD && "length".contentEquals(member.getSimpleName())) {
593                 result[0] = member;
594             }
595             if (member.getKind() == ElementKind.CONSTRUCTOR) {
596                 List JavaDoc<? extends VariableElement> params = ((ExecutableElement)member).getParameters();
597                 if (params.size() == 1) {
598                     TypeMirror type = params.get(0).asType();
599                     if (type.getKind() == TypeKind.DECLARED) {
600                         TypeElement element = (TypeElement)((DeclaredType)type).asElement();
601                         if (java.lang.StringBuilder JavaDoc.class.getName().contentEquals(element.getQualifiedName())) {
602                             result[1] = element;
603                         }
604                     }
605                 }
606             }
607             if (member.getKind() == ElementKind.FIELD) {
608                 VariableElement ve = (VariableElement) member;
609                 if ("count".contentEquals(ve.getSimpleName())) {
610                     result[2] = member;
611                 }
612             }
613         }
614         return result;
615     }
616     
617     
618     
619     private Element getCollectionAdd (Element collectionElement) {
620         List JavaDoc<? extends Element> members = ((TypeElement)collectionElement).getEnclosedElements();
621         for (Element member : members) {
622             if (member.getKind() == ElementKind.METHOD && "add".contentEquals(member.getSimpleName())) {
623                 return member;
624             }
625         }
626         return null;
627     }
628     
629     
630     private static class ClassPathProviderImpl implements ClassPathProvider {
631         
632         private static ClassPathProviderImpl instance;
633         
634         private ClassPath compile;
635         private ClassPath boot;
636         private ClassPath src;
637         
638         private ClassPathProviderImpl () {
639             
640         }
641         
642         public synchronized ClassPath findClassPath(FileObject file, String JavaDoc type) {
643             if (ClassPath.COMPILE.equals(type)) {
644                 return compile;
645             }
646             else if (ClassPath.BOOT.equals(type)) {
647                 return boot;
648             }
649             else if (ClassPath.SOURCE.equals(type)) {
650                 return src;
651             }
652             else {
653                 return null;
654             }
655         }
656         
657         public synchronized void setClassPaths (ClassPath boot, ClassPath compile, ClassPath src) {
658             this.boot = boot;
659             this.compile = compile;
660             this.src = src;
661         }
662         
663         public static synchronized ClassPathProviderImpl getDefault () {
664             if (instance == null) {
665                 instance = new ClassPathProviderImpl ();
666             }
667             return instance;
668         }
669     }
670     
671     private static ClassPath createBootPath () throws IOException JavaDoc {
672         String JavaDoc bootPath = System.getProperty ("sun.boot.class.path"); //NOI18N
673
String JavaDoc[] paths = bootPath.split(File.pathSeparator);
674         List JavaDoc<URL JavaDoc>roots = new ArrayList JavaDoc<URL JavaDoc> (paths.length);
675         for (String JavaDoc path : paths) {
676             File JavaDoc f = new File JavaDoc (path);
677             if (!f.exists()) {
678                 continue;
679             }
680             URL JavaDoc url = f.toURI().toURL();
681             if (FileUtil.isArchiveFile(url)) {
682                 url = FileUtil.getArchiveRoot(url);
683             }
684             roots.add (url);
685         }
686         return ClassPathSupport.createClassPath(roots.toArray(new URL JavaDoc[roots.size()]));
687     }
688     
689 }
690
Popular Tags