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

spoon.reflect.CtModelImpl 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;

import spoon.processing.Processor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtModule;
import spoon.reflect.declaration.CtNamedElement;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.factory.ModuleFactory;
import spoon.reflect.visitor.Filter;
import spoon.reflect.visitor.chain.CtConsumableFunction;
import spoon.reflect.visitor.chain.CtFunction;
import spoon.reflect.visitor.chain.CtQuery;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.support.QueueProcessingManager;
import spoon.support.reflect.declaration.CtPackageImpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class CtModelImpl implements CtModel {

	private static final long serialVersionUID = 1L;

	private boolean buildModelFinished = false;

	@Override
	public  CtQuery filterChildren(Filter filter) {
		return getUnnamedModule().getFactory().Query().createQuery(this.getAllModules().toArray()).filterChildren(filter);
	}

	@Override
	public  CtQuery map(CtFunction function) {
		return getUnnamedModule().getFactory().Query().createQuery(this.getAllModules().toArray()).map(function);
	}

	@Override
	public  CtQuery map(CtConsumableFunction queryStep) {
		return getUnnamedModule().getFactory().Query().createQuery(this.getAllModules().toArray()).map(queryStep);
	}

	public static class CtRootPackage extends CtPackageImpl {
		{
			this.setSimpleName(CtPackage.TOP_LEVEL_PACKAGE_NAME);
		}

		@Override
		public  T setSimpleName(String name) {
			if (name == null) {
				return (T) this;
			}

			if (name.equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
				return super.setSimpleName(name);
			}

			return (T) this;
		}

		@Override
		public String getQualifiedName() {
			return "";
		}

		@Override
		public String toString() {
			return TOP_LEVEL_PACKAGE_NAME;
		}
	}

	private final CtModule unnamedModule;

	public CtModelImpl(Factory f) {
		this.unnamedModule = new ModuleFactory.CtUnnamedModule();
		this.unnamedModule.setFactory(f);
		this.unnamedModule.setRootPackage(new CtModelImpl.CtRootPackage());
		getRootPackage().setFactory(f);
	}

	@Override
	public CtPackage getRootPackage() {
		return getUnnamedModule().getRootPackage();
	}


	@Override
	public Collection> getAllTypes() {
		final List> result = new ArrayList<>();
		getAllPackages().forEach(ctPackage -> {
			result.addAll(ctPackage.getTypes());
		});
		return result;
	}


	@Override
	public Collection getAllPackages() {
		return Collections.unmodifiableCollection(getElements(new TypeFilter<>(CtPackage.class)));
	}

	@Override
	public CtModule getUnnamedModule() {
		return this.unnamedModule;
	}

	@Override
	public Collection getAllModules() {
		return ((ModuleFactory.CtUnnamedModule) this.unnamedModule).getAllModules();
	}


	@Override
	public void processWith(Processor processor) {
		QueueProcessingManager processingManager = new QueueProcessingManager(getUnnamedModule().getFactory());
		processingManager.addProcessor(processor);
		processingManager.process(getAllModules());
	}

	@Override
	public  List getElements(Filter filter) {
		return filterChildren(filter).list();
	}


	@Override
	public boolean isBuildModelFinished() {
		return this.buildModelFinished;
	}

	@Override
	public  T setBuildModelIsFinished(boolean buildModelFinished) {
		this.buildModelFinished = buildModelFinished;
		return (T) this;
	}

}