KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.reflect.factory;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.TreeMap JavaDoc;
6
7 import spoon.reflect.Factory;
8 import spoon.reflect.declaration.CtClass;
9 import spoon.reflect.declaration.CtElement;
10 import spoon.reflect.declaration.CtSimpleType;
11 import spoon.reflect.declaration.CtType;
12 import spoon.reflect.reference.CtReference;
13 import spoon.reflect.reference.CtTypeReference;
14 import spoon.reflect.visitor.CtScanner;
15 import spoon.template.Template;
16 import spoon.template.TemplateException;
17
18 /**
19  * A factory for storing, accessing and creating templates and associated types.
20  */

21 public class TemplateFactory extends SubFactory implements Serializable JavaDoc {
22
23     private static final long serialVersionUID = 1L;
24
25     /**
26      * Creates a new template factory.
27      *
28      * @param factory
29      * the parent factory
30      */

31     public TemplateFactory(Factory factory) {
32         super(factory);
33     }
34
35     private Map JavaDoc<String JavaDoc, CtClass<?>> templates = new TreeMap JavaDoc<String JavaDoc, CtClass<?>>();
36
37     /**
38      * Adds a template to this factory.
39      *
40      * @param template
41      * the template class
42      */

43     public void add(CtClass<?> template) {
44         if (templates.containsKey(template.getQualifiedName()))
45             templates.remove(template.getQualifiedName());
46         templates.put(template.getQualifiedName(), template);
47         new CtScanner() {
48             public void enter(CtElement e) {
49                 e.setFactory(factory);
50                 super.enter(e);
51             }
52
53             @Override JavaDoc
54             protected void enterReference(CtReference e) {
55                 e.setFactory(factory);
56                 super.enterReference(e);
57             }
58         }.scan(template);
59     }
60
61     /**
62      * Gets all the templates in this factory.
63      *
64      * @return the stored templates as a map
65      */

66     public Map JavaDoc<String JavaDoc, CtClass<?>> getAll() {
67         return templates;
68     }
69
70     /**
71      * Gets a template CT class from its actual class.
72      *
73      * @param templateClass
74      * the runtime class
75      * @return the compile-time class
76      */

77     public <T> CtClass<T> get(Class JavaDoc<T> templateClass) {
78         return get(templateClass.getName());
79     }
80
81     /**
82      * Gets a template class from its qualified name.
83      *
84      * @param templateName
85      * the template's fully qualified name
86      * @return the template class
87      */

88     @SuppressWarnings JavaDoc("unchecked")
89     public <T> CtClass<T> get(String JavaDoc templateName) {
90         if (!templates.containsKey(templateName))
91             throw new TemplateException("Unable to load template \""
92                     + templateName + "\". Check your template source path");
93         return (CtClass<T>) templates.get(templateName);
94     }
95
96     /**
97      * Look for template classes in the parent factory and add them to this
98      * factory.
99      */

100     @SuppressWarnings JavaDoc("unchecked")
101     public void parseTypes() {
102         for (CtSimpleType<?> t : factory.Type().getAll()) {
103             if (t instanceof CtClass) {
104                 for (CtSimpleType nested : ((CtType<?>) t).getNestedTypes()) {
105                     if (nested instanceof CtClass)
106                         scanType((CtClass<? extends Template>) nested);
107                 }
108                 scanType((CtClass<? extends Template>) t);
109             }
110         }
111     }
112
113     private void scanType(CtClass<? extends Template> t) {
114         if (factory.Type().createReference(Template.class).isAssignableFrom(
115                 t.getReference())) {
116             add(t);
117         }
118     }
119
120     /**
121      * Helper method to know if a given type reference points to a Template or
122      * not
123      *
124      * @param candidate
125      * the reference to check
126      *
127      * @return <code> true </code> if the reference points to a template type
128      */

129     public boolean isTemplate(CtTypeReference<?> candidate) {
130         return templates.containsKey(candidate.getQualifiedName());
131     }
132 }
Popular Tags