KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.reflect.visitor;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import spoon.reflect.reference.CtReference;
7
8 /**
9  * A simple visitor that takes a reference filter and returns all the references
10  * that match it.
11  */

12
13 public class ReferenceQueryVisitor<T extends CtReference> extends CtScanner {
14     ReferenceFilter<T> filter;
15
16     List JavaDoc<T> result = new ArrayList JavaDoc<T>();
17
18     /**
19      * Constructs a reference query visitor with a given reference filter.
20      */

21     public ReferenceQueryVisitor(ReferenceFilter<T> filter) {
22         super();
23         this.filter = filter;
24     }
25
26     /**
27      * Gets the result (references matching the filter).
28      */

29     public List JavaDoc<T> getResult() {
30         return result;
31     }
32
33     @SuppressWarnings JavaDoc("unchecked")
34     @Override JavaDoc
35     public void scan(CtReference reference) {
36         if (reference == null)
37             return;
38         if (filter.getType().isAssignableFrom(reference.getClass())) {
39             if (filter.matches((T) reference)) {
40                 result.add((T) reference);
41             }
42         }
43         super.scan(reference);
44     }
45 }
Popular Tags