All Downloads are FREE. Search and download functionalities are using the official Maven repository.

spoon.reflect.visitor.Query Maven / Gradle / Ivy

Go to download

Spoon is a tool for meta-programming, analysis and transformation of Java programs.

There is a newer version: 11.1.1-beta-14
Show newest version
/*
 * SPDX-License-Identifier: (MIT OR CECILL-C)
 *
 * Copyright (C) 2006-2023 INRIA and contributors
 *
 * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
 */
package spoon.reflect.visitor;

import spoon.reflect.declaration.CtElement;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtReference;
import spoon.reflect.visitor.chain.CtFunction;
import spoon.reflect.visitor.filter.TypeFilter;

import java.util.List;

/**
 * This class provides some useful methods to retrieve program elements and
 * reference through a {@link spoon.reflect.visitor.CtScanner}-based deep
 * search. It uses the {@link spoon.reflect.visitor.Filter} facility to select the right
 * elements or references.
 */
public abstract class Query {

	private Query() {
	}

	/**
	 * Within a given factory, returns all the program elements that match the
	 * filter.
	 *
	 * @param 
	 * 		the type of the sought program elements
	 * @param factory
	 * 		the factory that contains the elements where to recursive
	 * 		search on
	 * @param filter
	 * 		the filter which defines the matching criteria
	 */
	public static  List getElements(Factory factory,
															Filter filter) {
		return getElements(factory.Package().getRootPackage(), filter);
	}

	/**
	 * Returns all the program elements that match the filter starting from the given rootElement.
	 * Use {@link spoon.reflect.visitor.chain.CtQueryable#map(CtFunction)} if you need more control on the scanning context of the Filter.
	 *
	 * @param 
	 * 		the type of the sought program elements
	 * @param rootElement
	 * 		the element to start the recursive search on
	 * @param filter
	 * 		the filter which defines the matching criteria
	 */
	public static  List getElements(
			CtElement rootElement, Filter filter) {
		return rootElement.filterChildren(filter).list();
	}

	/**
	 * Returns all the program element references that match the filter.
	 *
	 * @param 
	 * 		the type of the sought program element references
	 * @param rootElement
	 * 		the element to start the recursive search on
	 * @param filter
	 * 		the filter which defines the matching criteria
	 *
	 */
	public static  List getReferences(
			CtElement rootElement, Filter filter) {
		// note that the new TypeFilter<>(CtReference.class) should not be necessary
		// thanks to using 
		// however, playing safe to satisfy contract in case of type erasure
		return rootElement.filterChildren(new TypeFilter<>(CtReference.class)).filterChildren(filter).list();
	}

	/**
	 * Within a given factory, returns all the program element references that
	 * match the filter.
	 *
	 * @param 
	 * 		the type of the sought program element references
	 * @param factory
	 * 		the factory that contains the references where to recursive
	 * 		search on
	 * @param filter
	 * 		the filter which defines the matching criteria
	 */
	public static  List getReferences(
			Factory factory, Filter filter) {
		return getReferences(factory.Package().getRootPackage(), filter);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy