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

spoon.reflect.factory.InterfaceFactory Maven / Gradle / Ivy

Go to download

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

The newest version!
/*
 * Spoon - http://spoon.gforge.inria.fr/
 * Copyright (C) 2006 INRIA Futurs 
 *
 * This software is governed by the CeCILL-C License under French law and
 * abiding by the rules of distribution of free software. You can use, modify
 * and/or redistribute the software under the terms of the CeCILL-C license as
 * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL-C license and that you accept its terms.
 */

package spoon.reflect.factory;

import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtType;

/**
 * The {@link CtInterface} sub-factory.
 */
public class InterfaceFactory extends TypeFactory {

	/**
	 * Creates a new interface sub-factory.
	 *
	 * @param factory
	 *            the parent factory
	 */
	public InterfaceFactory(Factory factory) {
		super(factory);
	}

	/**
	 * Creates an interface.
	 */
	public  CtInterface create(CtPackage owner, String simpleName) {
		CtInterface i = factory.Core().createInterface();
		i.setSimpleName(simpleName);
		owner.getTypes().add(i);
		return i;
	}

	/**
	 * Creates an inner interface
	 */
	public  CtInterface create(CtType owner, String simpleName) {
		CtInterface i = factory.Core().createInterface();
		i.setSimpleName(simpleName);
		owner.getNestedTypes().add(i);
		return i;
	}

	/**
	 * Creates an interface.
	 */
	@SuppressWarnings("unchecked")
	public  CtInterface create(String qualifiedName) {
		if (hasInnerType(qualifiedName) > 0) {
			return create(
					(CtInterface) create(getDeclaringTypeName(qualifiedName)),
					getSimpleName(qualifiedName));
		}
		return create(factory.Package().getOrCreate(
				getPackageName(qualifiedName)), getSimpleName(qualifiedName));
	}

	/**
	 * Gets a created interface
	 *
	 * @return the interface or null if does not exist
	 */
	@Override
	@SuppressWarnings("unchecked")
	public  CtInterface get(String qualifiedName) {
		try {
			return (CtInterface) super.get(qualifiedName);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * Gets a interface from its runtime Java class.
	 *
	 * @param 
	 *            type of created class
	 * @param cl
	 *            the java class: note that this class should be Class<T> but
	 *            it then poses problem when T is a generic type itself
	 */
	@Override
	@SuppressWarnings("unchecked")
	public  CtInterface get(Class cl) {
		try {
			return (CtInterface) super.get(cl);
		} catch (Exception e) {
			return null;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy