KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

19     public InterfaceFactory(Factory factory) {
20         super(factory);
21     }
22
23     /**
24      * Creates an interface.
25      */

26     public <T> CtInterface<T> create(CtPackage owner, String JavaDoc simpleName) {
27         CtInterface<T> i = factory.Core().createInterface();
28         i.setSimpleName(simpleName);
29         owner.getTypes().add(i);
30         i.setParent(owner);
31         return i;
32     }
33
34     /**
35      * Creates an inner interface
36      */

37     public <T> CtInterface<T> create(CtType<T> owner, String JavaDoc simpleName) {
38         CtInterface<T> i = factory.Core().createInterface();
39         i.setSimpleName(simpleName);
40         owner.getNestedTypes().add(i);
41         i.setParent(owner);
42         return i;
43     }
44
45     /**
46      * Creates an interface.
47      */

48     @SuppressWarnings JavaDoc("unchecked")
49     public <T> CtInterface<T> create(String JavaDoc qualifiedName) {
50         if (hasInnerType(qualifiedName) > 0) {
51             return create(
52                     (CtInterface<T>) create(getDeclaringTypeName(qualifiedName)),
53                     getSimpleName(qualifiedName));
54         } else {
55             return create(factory.Package().getOrCreate(getPackageName(qualifiedName)),
56                     getSimpleName(qualifiedName));
57         }
58     }
59
60     /**
61      * Gets a created interface
62      *
63      * @return the interface or null if does not exist
64      */

65     @SuppressWarnings JavaDoc("unchecked")
66     public <T> CtInterface<T> get(String JavaDoc qualifiedName) {
67         try {
68             return (CtInterface<T>) super.get(qualifiedName);
69         } catch (Exception JavaDoc e) {
70             return null;
71         }
72     }
73
74     /**
75      * Gets a interface from its runtime Java class.
76      *
77      * @param <T>
78      * type of created class
79      * @param cl
80      * java class
81      */

82     public <T> CtInterface<T> get(Class JavaDoc<T> cl) {
83         try {
84             return (CtInterface<T>) super.get(cl);
85         } catch (Exception JavaDoc e) {
86             return null;
87         }
88     }
89     
90     
91 }
Popular Tags