01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.reflect.visitor;
19:
20: import java.util.ArrayList;
21: import java.util.List;
22:
23: import spoon.reflect.declaration.CtElement;
24:
25: /**
26: * A simple visitor that takes a filter and returns all the elements that match
27: * it.
28: */
29: public class QueryVisitor<T extends CtElement> extends CtScanner {
30: Filter<T> filter;
31:
32: List<T> result = new ArrayList<T>();
33:
34: /**
35: * Constructs a query visitor with a given filter.
36: */
37: public QueryVisitor(Filter<T> filter) {
38: super ();
39: this .filter = filter;
40: }
41:
42: /**
43: * Gets the result (elements matching the filter).
44: */
45: public List<T> getResult() {
46: return result;
47: }
48:
49: @SuppressWarnings("unchecked")
50: @Override
51: public void scan(CtElement element) {
52: if (element == null)
53: return;
54: if (filter.getType().isAssignableFrom(element.getClass())) {
55: if (filter.matches((T) element)) {
56: result.add((T) element);
57: }
58: }
59: super.scan(element);
60: }
61: }
|