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