KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

20     public ClassFactory(Factory factory) {
21         super(factory);
22     }
23
24     /**
25      * Creates an inner class.
26      *
27      * @param declaringClass
28      * declaring class
29      * @param simpleName
30      * simple name of inner class (without . or $)
31      */

32     public <T> CtClass<T> create(CtClass<?> declaringClass, String JavaDoc simpleName) {
33         CtClass<T> c = factory.Core().createClass();
34         c.setSimpleName(simpleName);
35         c.setParent(declaringClass);
36         return c;
37     }
38
39     /**
40      * Creates a top-level class.
41      *
42      * @param owner
43      * the declaring package
44      * @param simpleName
45      * the simple name
46      */

47     public <T> CtClass<T> create(CtPackage owner, String JavaDoc simpleName) {
48         CtClass<T> c = factory.Core().createClass();
49         c.setSimpleName(simpleName);
50         if (owner.getTypes().contains(c))
51             owner.getTypes().remove(c);
52         owner.getTypes().add(c);
53         c.setParent(owner);
54         return c;
55     }
56
57     /**
58      * Creates a class from its qualified name.
59      *
60      * @param <T>
61      * type of created class
62      * @param qualifiedName
63      * full name of class to create. Name can contain . or $ for
64      * inner types
65      */

66     public <T> CtClass<T> create(String JavaDoc qualifiedName) {
67         if (hasInnerType(qualifiedName) > 0) {
68             CtClass<?> declaringClass = create(getDeclaringTypeName(qualifiedName));
69             return create(declaringClass, getSimpleName(qualifiedName));
70         } else {
71             return create(factory.Package().getOrCreate(getPackageName(qualifiedName)),
72                     getSimpleName(qualifiedName));
73         }
74     }
75
76     /**
77      * Gets a class from its runtime Java class.
78      *
79      * @param <T>
80      * type of created class
81      * @param cl
82      * java class
83      */

84     public <T> CtClass<T> get(Class JavaDoc<T> cl) {
85         try {
86             return (CtClass<T>) super.get(cl);
87         } catch (Exception JavaDoc e) {
88             return null;
89         }
90     }
91
92     /**
93      * Searches for a class from his qualified name.
94      *
95      * @param <T>
96      * the type of the class
97      * @param qualifiedName
98      * to search
99      * @return found class or null
100      */

101     @SuppressWarnings JavaDoc("unchecked")
102     public <T> CtClass<T> get(String JavaDoc qualifiedName) {
103         try {
104             return (CtClass<T>) super.get(qualifiedName);
105         } catch (Exception JavaDoc e) {
106             return null;
107         }
108     }
109
110 }
Popular Tags