KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > factory > EnumFactory


1 package spoon.reflect.factory;
2
3 import spoon.reflect.Factory;
4 import spoon.reflect.declaration.CtEnum;
5 import spoon.reflect.declaration.CtPackage;
6
7 /**
8  * The {@link CtEnum} sub-factory.
9  */

10 public class EnumFactory extends TypeFactory {
11
12     private static final long serialVersionUID = 1L;
13
14     /**
15      * Creates a new enum sub-factory.
16      * @param factory the parent factory
17      */

18     public EnumFactory(Factory factory) {
19         super(factory);
20     }
21
22     /**
23      * Creates a new enumeration type
24      *
25      * @param owner
26      * package
27      * @param simpleName
28      * the simple name
29      */

30     public CtEnum create(CtPackage owner, String JavaDoc simpleName) {
31         CtEnum e = factory.Core().createEnum();
32         e.setSimpleName(simpleName);
33         owner.getTypes().add(e);
34         e.setParent(owner);
35         return e;
36     }
37
38     /**
39      * Creates an enum from its qualified name.
40      */

41     public CtEnum create(String JavaDoc qualifiedName) {
42         return create(factory.Package().getOrCreate(getPackageName(qualifiedName)),
43                 getSimpleName(qualifiedName));
44     }
45
46     /**
47      * Gets an already created enumeration from its qualified name.
48      *
49      * @return the enumeration or null if does not exist
50      */

51     @SuppressWarnings JavaDoc("unchecked")
52     public CtEnum get(String JavaDoc qualifiedName) {
53         try {
54             return (CtEnum) super.get(qualifiedName);
55         } catch (Exception JavaDoc e) {
56             return null;
57         }
58     }
59     
60     /**
61      * Gets a class from its runtime Java class.
62      *
63      * @param <T>
64      * type of created class
65      * @param cl
66      * java class
67      */

68     public <T extends Enum JavaDoc> CtEnum<T> get(Class JavaDoc<T> cl) {
69         try {
70             return (CtEnum<T>) super.get(cl);
71         } catch (Exception JavaDoc e) {
72             return null;
73         }
74     }
75
76 }
77
Popular Tags