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.CtClass;
022: import spoon.reflect.declaration.CtPackage;
023:
024: /**
025: * The {@link CtClass} sub-factory.
026: */
027: public class ClassFactory extends TypeFactory {
028:
029: private static final long serialVersionUID = 1L;
030:
031: /**
032: * Creates a class sub-factory.
033: *
034: * @param factory
035: * the parent factory
036: */
037: public ClassFactory(Factory factory) {
038: super (factory);
039: }
040:
041: /**
042: * Creates an inner class.
043: *
044: * @param declaringClass
045: * declaring class
046: * @param simpleName
047: * simple name of inner class (without . or $)
048: */
049: public <T> CtClass<T> create(CtClass<?> declaringClass,
050: String simpleName) {
051: CtClass<T> c = factory.Core().createClass();
052: c.setSimpleName(simpleName);
053: c.setParent(declaringClass);
054: return c;
055: }
056:
057: /**
058: * Creates a top-level class.
059: *
060: * @param owner
061: * the declaring package
062: * @param simpleName
063: * the simple name
064: */
065: public <T> CtClass<T> create(CtPackage owner, String simpleName) {
066: CtClass<T> c = factory.Core().createClass();
067: c.setSimpleName(simpleName);
068: if (owner.getTypes().contains(c)) {
069: owner.getTypes().remove(c);
070: }
071: owner.getTypes().add(c);
072: c.setParent(owner);
073: return c;
074: }
075:
076: /**
077: * Creates a class from its qualified name.
078: *
079: * @param <T>
080: * type of created class
081: * @param qualifiedName
082: * full name of class to create. Name can contain . or $ for
083: * inner types
084: */
085: public <T> CtClass<T> create(String qualifiedName) {
086: if (hasInnerType(qualifiedName) > 0) {
087: CtClass<?> declaringClass = create(getDeclaringTypeName(qualifiedName));
088: return create(declaringClass, getSimpleName(qualifiedName));
089: }
090: return create(factory.Package().getOrCreate(
091: getPackageName(qualifiedName)),
092: getSimpleName(qualifiedName));
093: }
094:
095: /**
096: * Gets a class from its runtime Java class.
097: *
098: * @param <T>
099: * type of created class
100: * @param cl
101: * the java class: note that this class should be Class<T> but
102: * it then poses problem when T is a generic type itself
103: */
104: @Override
105: @SuppressWarnings("unchecked")
106: public <T> CtClass<T> get(Class<?> cl) {
107: try {
108: return (CtClass<T>) super .get(cl);
109: } catch (Exception e) {
110: return null;
111: }
112: }
113:
114: /**
115: * Searches for a class from his qualified name.
116: *
117: * @param <T>
118: * the type of the class
119: * @param qualifiedName
120: * to search
121: * @return found class or null
122: */
123: @Override
124: @SuppressWarnings("unchecked")
125: public <T> CtClass<T> get(String qualifiedName) {
126: try {
127: return (CtClass<T>) super .get(qualifiedName);
128: } catch (Exception e) {
129: return null;
130: }
131: }
132:
133: }
|