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.factory;
019:
020: import spoon.reflect.Factory;
021: import spoon.reflect.declaration.CtInterface;
022: import spoon.reflect.declaration.CtPackage;
023: import spoon.reflect.declaration.CtType;
024:
025: /**
026: * The {@link CtInterface} sub-factory.
027: */
028: public class InterfaceFactory extends TypeFactory {
029:
030: private static final long serialVersionUID = 1L;
031:
032: /**
033: * Creates a new interface sub-factory.
034: *
035: * @param factory
036: * the parent factory
037: */
038: public InterfaceFactory(Factory factory) {
039: super (factory);
040: }
041:
042: /**
043: * Creates an interface.
044: */
045: public <T> CtInterface<T> create(CtPackage owner, String simpleName) {
046: CtInterface<T> i = factory.Core().createInterface();
047: i.setSimpleName(simpleName);
048: owner.getTypes().add(i);
049: i.setParent(owner);
050: return i;
051: }
052:
053: /**
054: * Creates an inner interface
055: */
056: public <T> CtInterface<T> create(CtType<T> owner, String simpleName) {
057: CtInterface<T> i = factory.Core().createInterface();
058: i.setSimpleName(simpleName);
059: owner.getNestedTypes().add(i);
060: i.setParent(owner);
061: return i;
062: }
063:
064: /**
065: * Creates an interface.
066: */
067: @SuppressWarnings("unchecked")
068: public <T> CtInterface<T> create(String qualifiedName) {
069: if (hasInnerType(qualifiedName) > 0) {
070: return create(
071: (CtInterface<T>) create(getDeclaringTypeName(qualifiedName)),
072: getSimpleName(qualifiedName));
073: }
074: return create(factory.Package().getOrCreate(
075: getPackageName(qualifiedName)),
076: getSimpleName(qualifiedName));
077: }
078:
079: /**
080: * Gets a created interface
081: *
082: * @return the interface or null if does not exist
083: */
084: @Override
085: @SuppressWarnings("unchecked")
086: public <T> CtInterface<T> get(String qualifiedName) {
087: try {
088: return (CtInterface<T>) super .get(qualifiedName);
089: } catch (Exception e) {
090: return null;
091: }
092: }
093:
094: /**
095: * Gets a interface from its runtime Java class.
096: *
097: * @param <T>
098: * type of created class
099: * @param cl
100: * the java class: note that this class should be Class<T> but
101: * it then poses problem when T is a generic type itself
102: */
103: @Override
104: @SuppressWarnings("unchecked")
105: public <T> CtInterface<T> get(Class<?> cl) {
106: try {
107: return (CtInterface<T>) super .get(cl);
108: } catch (Exception e) {
109: return null;
110: }
111: }
112:
113: }
|