001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.reflect.visitor;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022:
023: import spoon.reflect.Factory;
024: import spoon.reflect.declaration.CtElement;
025: import spoon.reflect.declaration.CtPackage;
026: import spoon.reflect.reference.CtReference;
027:
028: /**
029: * This class provides some useful methods to retrieve program elements and
030: * reference through a {@link spoon.reflect.visitor.CtScanner}-based deep
031: * search. It uses the {@link spoon.reflect.visitor.Filter} and
032: * {@link spoon.reflect.visitor.ReferenceFilter} facitily to select the right
033: * elements or references.
034: */
035: public abstract class Query extends CtScanner {
036:
037: private Query() {
038: }
039:
040: /**
041: * Within a given factory, returns all the program elements that match the
042: * filter.
043: *
044: * @param <E>
045: * the type of the seeked program elements
046: * @param factory
047: * the factory that contains the elements where to recursive
048: * search on
049: * @param filter
050: * the filter which defines the matching criteria
051: */
052: public static <E extends CtElement> List<E> getElements(
053: Factory factory, Filter<E> filter) {
054: List<E> e = new ArrayList<E>();
055: for (CtPackage p : factory.Package().getAllRoots()) {
056: e.addAll(getElements(p, filter));
057: }
058: return e;
059: }
060:
061: /**
062: * Returns all the program elements that match the filter.
063: *
064: * @param <E>
065: * the type of the seeked program elements
066: * @param rootElement
067: * the element to start the recursive search on
068: * @param filter
069: * the filter which defines the matching criteria
070: */
071: public static <E extends CtElement> List<E> getElements(
072: CtElement rootElement, Filter<E> filter) {
073: QueryVisitor<E> visitor = new QueryVisitor<E>(filter);
074: visitor.scan(rootElement);
075: return visitor.getResult();
076: }
077:
078: /**
079: * Returns all the program element references that match the filter.
080: *
081: * @param <T>
082: * the type of the seeked program element references
083: * @param rootElement
084: * the element to start the recursive search on
085: * @param filter
086: * the filter which defines the matching criteria
087: */
088: public static <T extends CtReference> List<T> getReferences(
089: CtElement rootElement, ReferenceFilter<T> filter) {
090: ReferenceQueryVisitor<T> visitor = new ReferenceQueryVisitor<T>(
091: filter);
092: visitor.scan(rootElement);
093: return visitor.getResult();
094: }
095:
096: /**
097: * Within a given factory, returns all the program element references that
098: * match the filter.
099: *
100: * @param <R>
101: * the type of the seeked program element references
102: * @param factory
103: * the factory that contains the references where to recursive
104: * search on
105: * @param filter
106: * the filter which defines the matching criteria
107: */
108: public static <R extends CtReference> List<R> getReferences(
109: Factory factory, ReferenceFilter<R> filter) {
110: List<R> r = new ArrayList<R>();
111: for (CtPackage p : factory.Package().getAllRoots()) {
112: r.addAll(getReferences(p, filter));
113: }
114: return r;
115: }
116:
117: }
|