KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > reflect > Factory


1 package spoon.reflect;
2
3 import java.io.Serializable JavaDoc;
4
5 import spoon.processing.Builder;
6 import spoon.processing.Environment;
7 import spoon.reflect.declaration.CtAnnotationType;
8 import spoon.reflect.declaration.CtClass;
9 import spoon.reflect.declaration.CtConstructor;
10 import spoon.reflect.declaration.CtEnum;
11 import spoon.reflect.declaration.CtExecutable;
12 import spoon.reflect.declaration.CtField;
13 import spoon.reflect.declaration.CtInterface;
14 import spoon.reflect.declaration.CtMethod;
15 import spoon.reflect.declaration.CtPackage;
16 import spoon.reflect.declaration.CtType;
17 import spoon.reflect.factory.AnnotationFactory;
18 import spoon.reflect.factory.ClassFactory;
19 import spoon.reflect.factory.CodeFactory;
20 import spoon.reflect.factory.ConstructorFactory;
21 import spoon.reflect.factory.EnumFactory;
22 import spoon.reflect.factory.EvalFactory;
23 import spoon.reflect.factory.ExecutableFactory;
24 import spoon.reflect.factory.FieldFactory;
25 import spoon.reflect.factory.InterfaceFactory;
26 import spoon.reflect.factory.MethodFactory;
27 import spoon.reflect.factory.PackageFactory;
28 import spoon.reflect.factory.TemplateFactory;
29 import spoon.reflect.factory.TypeFactory;
30 import spoon.support.DefaultCoreFactory;
31 import spoon.support.StandardEnvironment;
32
33 /**
34  * This class defines the entry point API to create and access the program's
35  * model. Element-specific methods are defined in the sub-factories. An instance
36  * of the current factory can be retrieved at any place where a
37  * {@link spoon.processing.FactoryAccessor} instance is available (that is to
38  * say, any processor or model element). Factory methods ensure the model's
39  * consistency and should be always used when creating model elements. The
40  * {@link spoon.reflect.CoreFactory} is used to create raw elements, but model
41  * consistency has to be maintained manually.
42  *
43  * @see spoon.processing.Processor
44  * @see spoon.reflect.declaration.CtElement
45  * @see spoon.reflect.CoreFactory
46  * @see #Core()
47  */

48 public class Factory implements Serializable JavaDoc {
49
50     private static final long serialVersionUID = 1L;
51
52     private transient AnnotationFactory Annotation;
53
54     /**
55      * The {@link CtAnnotationType} sub-factory.
56      */

57     public AnnotationFactory Annotation() {
58         if (Annotation == null) {
59             Annotation = new AnnotationFactory(this);
60         }
61         return Annotation;
62     }
63
64     private transient ClassFactory Class;
65
66     /**
67      * The {@link CtClass} sub-factory.
68      */

69     public ClassFactory Class() {
70         if (Class == null) {
71             Class = new ClassFactory(this);
72         }
73         return Class;
74     }
75
76     private transient CodeFactory Code;
77
78     /**
79      * The {@link spoon.reflect.code.CtCodeElement} sub-factory.
80      */

81     public CodeFactory Code() {
82         if (Code == null) {
83             Code = new CodeFactory(this);
84         }
85         return Code;
86     }
87
88     private transient ConstructorFactory Constructor;
89
90     /**
91      * The {@link CtConstructor} sub-factory.
92      */

93     public ConstructorFactory Constructor() {
94         if (Constructor == null) {
95             Constructor = new ConstructorFactory(this);
96         }
97         return Constructor;
98     }
99
100     private CoreFactory Core;
101
102     /**
103      * The core factory.
104      */

105     public CoreFactory Core() {
106         if (Core == null) {
107             Core = new DefaultCoreFactory();
108         }
109         return Core;
110     }
111
112     private transient EnumFactory Enum;
113
114     /**
115      * The {@link CtEnum} sub-factory.
116      */

117     public EnumFactory Enum() {
118         if (Enum == null) {
119             Enum = new EnumFactory(this);
120         }
121         return Enum;
122     }
123
124     private Environment Environment;
125
126     /**
127      * Gets the Spoon environment that encloses this factory.
128      */

129     public Environment getEnvironment() {
130         if (Environment == null) {
131             Environment = new StandardEnvironment();
132         }
133         return Environment;
134     }
135
136     private transient ExecutableFactory Executable;
137
138     /**
139      * The {@link CtExecutable} sub-factory.
140      */

141     public ExecutableFactory Executable() {
142         if (Executable == null) {
143             Executable = new ExecutableFactory(this);
144         }
145         return Executable;
146     }
147
148     private transient EvalFactory Eval;
149
150     /**
151      * The evaluators sub-factory.
152      */

153     public EvalFactory Eval() {
154         if (Eval == null) {
155             Eval = new EvalFactory(this);
156         }
157         return Eval;
158     }
159
160     private transient FieldFactory Field;
161
162     /**
163      * The {@link CtField} sub-factory.
164      */

165     public FieldFactory Field() {
166         if (Field == null) {
167             Field = new FieldFactory(this);
168         }
169         return Field;
170     }
171
172     /**
173      * The {@link CtInterface} sub-factory.
174      */

175     private transient InterfaceFactory Interface;
176
177     /**
178      * The {@link CtInterface} sub-factory.
179      */

180     public InterfaceFactory Interface() {
181         if (Interface == null) {
182             Interface = new InterfaceFactory(this);
183         }
184         return Interface;
185     }
186
187     private transient MethodFactory Method;
188
189     /**
190      * The {@link CtMethod} sub-factory.
191      */

192     public MethodFactory Method() {
193         if (Method == null) {
194             Method = new MethodFactory(this);
195         }
196         return Method;
197     }
198
199     private PackageFactory Package;
200
201     /**
202      * The {@link CtPackage} sub-factory.
203      */

204     public PackageFactory Package() {
205         if (Package == null) {
206             Package = new PackageFactory(this);
207         }
208         return Package;
209     }
210
211     private TemplateFactory Template;
212
213     /**
214      * The {@link CtPackage} sub-factory.
215      */

216     public TemplateFactory Template() {
217         if (Template == null) {
218             Template = new TemplateFactory(this);
219         }
220         return Template;
221     }
222
223     private transient TypeFactory Type;
224
225     /**
226      * The {@link CtType} sub-factory.
227      */

228     public TypeFactory Type() {
229         if (Type == null) {
230             Type = new TypeFactory(this);
231         }
232         return Type;
233     }
234
235     private Factory() {
236         super();
237         if (launchingFactory == null)
238             launchingFactory = this;
239     }
240
241     static Factory launchingFactory;
242
243     /**
244      * Gets the factory that was created at launching time (the firstly created
245      * factory). This factory is automatically initialized when a factory is
246      * contructed for the first time. Any subsequent constructions will not
247      * affect the launching factory and references to other factories have to be
248      * handled manually, if ever needed.
249      */

250     public static Factory getLauchingFactory() {
251         return launchingFactory;
252     }
253
254     /**
255      * The constructor.
256      */

257     public Factory(CoreFactory coreFactory, Environment environment) {
258         this();
259         this.Environment = environment;
260         this.Core = coreFactory;
261         this.Core.setMainFactory(this);
262     }
263
264     private Builder builder;
265
266     /**
267      * Returns the builder that was used to build the model of this factory ({@link Builder#build(Factory)}).
268      */

269     public Builder getBuilder() {
270         return builder;
271     }
272
273     /**
274      * Sets the builder (used in {@link Builder#build(Factory)}).
275      */

276     public void setBuilder(Builder builder) {
277         this.builder = builder;
278     }
279
280 }
281
Popular Tags