KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > visitor > Query


1 package spoon.reflect.visitor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import spoon.reflect.Factory;
7 import spoon.reflect.declaration.CtElement;
8 import spoon.reflect.declaration.CtPackage;
9 import spoon.reflect.reference.CtReference;
10
11 /**
12  * This class provides some useful methods to retrieve program elements and
13  * reference through a {@link spoon.reflect.visitor.CtScanner}-based deep
14  * search. It uses the {@link spoon.reflect.visitor.Filter} and
15  * {@link spoon.reflect.visitor.ReferenceFilter} facitily to select the right
16  * elements or references.
17  */

18 public abstract class Query extends CtScanner {
19
20     private Query() {
21     }
22
23     /**
24      * Within a given factory, returns all the program elements that match the
25      * filter.
26      *
27      * @param <E>
28      * the type of the seeked program elements
29      * @param factory
30      * the factory that contains the elements where to recursive
31      * search on
32      * @param filter
33      * the filter which defines the matching criteria
34      */

35     public static <E extends CtElement> List JavaDoc<E> getElements(Factory factory,
36             Filter<E> filter) {
37         List JavaDoc<E> e = new ArrayList JavaDoc<E>();
38         for (CtPackage p : factory.Package().getAllRoots()) {
39             e.addAll(getElements(p, filter));
40         }
41         return e;
42     }
43
44     /**
45      * Returns all the program elements that match the filter.
46      *
47      * @param <E>
48      * the type of the seeked program elements
49      * @param rootElement
50      * the element to start the recursive search on
51      * @param filter
52      * the filter which defines the matching criteria
53      */

54     public static <E extends CtElement> List JavaDoc<E> getElements(
55             CtElement rootElement, Filter<E> filter) {
56         QueryVisitor<E> visitor = new QueryVisitor<E>(filter);
57         visitor.scan(rootElement);
58         return visitor.getResult();
59     }
60
61     /**
62      * Returns all the program element references that match the filter.
63      *
64      * @param <T>
65      * the type of the seeked program element references
66      * @param rootElement
67      * the element to start the recursive search on
68      * @param filter
69      * the filter which defines the matching criteria
70      */

71     public static <T extends CtReference> List JavaDoc<T> getReferences(
72             CtElement rootElement, ReferenceFilter<T> filter) {
73         ReferenceQueryVisitor<T> visitor = new ReferenceQueryVisitor<T>(filter);
74         visitor.scan(rootElement);
75         return visitor.getResult();
76     }
77
78     /**
79      * Within a given factory, returns all the program element references that
80      * match the filter.
81      *
82      * @param <R>
83      * the type of the seeked program element references
84      * @param factory
85      * the factory that contains the references where to recursive
86      * search on
87      * @param filter
88      * the filter which defines the matching criteria
89      */

90     public static <R extends CtReference> List JavaDoc<R> getReferences(
91             Factory factory, ReferenceFilter<R> filter) {
92         List JavaDoc<R> r = new ArrayList JavaDoc<R>();
93         for (CtPackage p : factory.Package().getAllRoots()) {
94             r.addAll(getReferences(p, filter));
95         }
96         return r;
97     }
98
99 }
Popular Tags