KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > support > DefaultCoreFactory


1 package spoon.support;
2
3 import java.io.File JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.lang.annotation.Annotation JavaDoc;
6 import java.lang.reflect.Field JavaDoc;
7 import java.lang.reflect.Modifier JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Stack JavaDoc;
11 import java.util.Map.Entry;
12
13 import spoon.reflect.CoreFactory;
14 import spoon.reflect.Factory;
15 import spoon.reflect.code.CtArrayAccess;
16 import spoon.reflect.code.CtAssert;
17 import spoon.reflect.code.CtAssignment;
18 import spoon.reflect.code.CtBinaryOperator;
19 import spoon.reflect.code.CtBlock;
20 import spoon.reflect.code.CtBreak;
21 import spoon.reflect.code.CtCase;
22 import spoon.reflect.code.CtCatch;
23 import spoon.reflect.code.CtConditional;
24 import spoon.reflect.code.CtContinue;
25 import spoon.reflect.code.CtDo;
26 import spoon.reflect.code.CtExpression;
27 import spoon.reflect.code.CtFieldAccess;
28 import spoon.reflect.code.CtFor;
29 import spoon.reflect.code.CtForEach;
30 import spoon.reflect.code.CtIf;
31 import spoon.reflect.code.CtInvocation;
32 import spoon.reflect.code.CtLiteral;
33 import spoon.reflect.code.CtLocalVariable;
34 import spoon.reflect.code.CtNewArray;
35 import spoon.reflect.code.CtNewClass;
36 import spoon.reflect.code.CtOperatorAssignment;
37 import spoon.reflect.code.CtReturn;
38 import spoon.reflect.code.CtStatementList;
39 import spoon.reflect.code.CtSwitch;
40 import spoon.reflect.code.CtSynchronized;
41 import spoon.reflect.code.CtThrow;
42 import spoon.reflect.code.CtTry;
43 import spoon.reflect.code.CtUnaryOperator;
44 import spoon.reflect.code.CtVariableAccess;
45 import spoon.reflect.code.CtWhile;
46 import spoon.reflect.declaration.CtAnnotation;
47 import spoon.reflect.declaration.CtAnnotationType;
48 import spoon.reflect.declaration.CtAnonymousExecutable;
49 import spoon.reflect.declaration.CtClass;
50 import spoon.reflect.declaration.CtConstructor;
51 import spoon.reflect.declaration.CtElement;
52 import spoon.reflect.declaration.CtEnum;
53 import spoon.reflect.declaration.CtField;
54 import spoon.reflect.declaration.CtInterface;
55 import spoon.reflect.declaration.CtMethod;
56 import spoon.reflect.declaration.CtPackage;
57 import spoon.reflect.declaration.CtParameter;
58 import spoon.reflect.declaration.CtTypeParameter;
59 import spoon.reflect.declaration.SourcePosition;
60 import spoon.reflect.reference.CtArrayTypeReference;
61 import spoon.reflect.reference.CtExecutableReference;
62 import spoon.reflect.reference.CtFieldReference;
63 import spoon.reflect.reference.CtLocalVariableReference;
64 import spoon.reflect.reference.CtPackageReference;
65 import spoon.reflect.reference.CtParameterReference;
66 import spoon.reflect.reference.CtReference;
67 import spoon.reflect.reference.CtTypeParameterReference;
68 import spoon.reflect.reference.CtTypeReference;
69 import spoon.support.reflect.code.CtArrayAccessImpl;
70 import spoon.support.reflect.code.CtAssertImpl;
71 import spoon.support.reflect.code.CtAssignmentImpl;
72 import spoon.support.reflect.code.CtBinaryOperatorImpl;
73 import spoon.support.reflect.code.CtBlockImpl;
74 import spoon.support.reflect.code.CtBreakImpl;
75 import spoon.support.reflect.code.CtCaseImpl;
76 import spoon.support.reflect.code.CtCatchImpl;
77 import spoon.support.reflect.code.CtConditionalImpl;
78 import spoon.support.reflect.code.CtContinueImpl;
79 import spoon.support.reflect.code.CtDoImpl;
80 import spoon.support.reflect.code.CtFieldAccessImpl;
81 import spoon.support.reflect.code.CtForEachImpl;
82 import spoon.support.reflect.code.CtForImpl;
83 import spoon.support.reflect.code.CtIfImpl;
84 import spoon.support.reflect.code.CtInvocationImpl;
85 import spoon.support.reflect.code.CtLiteralImpl;
86 import spoon.support.reflect.code.CtLocalVariableImpl;
87 import spoon.support.reflect.code.CtNewArrayImpl;
88 import spoon.support.reflect.code.CtNewClassImpl;
89 import spoon.support.reflect.code.CtOperatorAssignmentImpl;
90 import spoon.support.reflect.code.CtReturnImpl;
91 import spoon.support.reflect.code.CtStatementListImpl;
92 import spoon.support.reflect.code.CtSwitchImpl;
93 import spoon.support.reflect.code.CtSynchronizedImpl;
94 import spoon.support.reflect.code.CtThrowImpl;
95 import spoon.support.reflect.code.CtTryImpl;
96 import spoon.support.reflect.code.CtUnaryOperatorImpl;
97 import spoon.support.reflect.code.CtVariableAccessImpl;
98 import spoon.support.reflect.code.CtWhileImpl;
99 import spoon.support.reflect.declaration.CtAnnotationImpl;
100 import spoon.support.reflect.declaration.CtAnnotationTypeImpl;
101 import spoon.support.reflect.declaration.CtAnonymousExecutableImpl;
102 import spoon.support.reflect.declaration.CtClassImpl;
103 import spoon.support.reflect.declaration.CtConstructorImpl;
104 import spoon.support.reflect.declaration.CtEnumImpl;
105 import spoon.support.reflect.declaration.CtFieldImpl;
106 import spoon.support.reflect.declaration.CtInterfaceImpl;
107 import spoon.support.reflect.declaration.CtMethodImpl;
108 import spoon.support.reflect.declaration.CtPackageImpl;
109 import spoon.support.reflect.declaration.CtParameterImpl;
110 import spoon.support.reflect.declaration.CtTypeParameterImpl;
111 import spoon.support.reflect.declaration.SourcePositionImpl;
112 import spoon.support.reflect.reference.CtArrayTypeReferenceImpl;
113 import spoon.support.reflect.reference.CtExecutableReferenceImpl;
114 import spoon.support.reflect.reference.CtFieldReferenceImpl;
115 import spoon.support.reflect.reference.CtLocalVariableReferenceImpl;
116 import spoon.support.reflect.reference.CtPackageReferenceImpl;
117 import spoon.support.reflect.reference.CtParameterReferenceImpl;
118 import spoon.support.reflect.reference.CtTypeParameterReferenceImpl;
119 import spoon.support.reflect.reference.CtTypeReferenceImpl;
120 import spoon.support.util.RtHelper;
121
122 /**
123  * This class implements a default core factory for Spoon's meta-model. This
124  * implementation is done with regular Java classes (POJOs).
125  */

126 public class DefaultCoreFactory implements CoreFactory, Serializable JavaDoc {
127
128     private static final long serialVersionUID = 1L;
129
130     Stack JavaDoc<Field JavaDoc> clonedFields = new Stack JavaDoc<Field JavaDoc>();
131
132     Stack JavaDoc<CtElement> cloningContext = new Stack JavaDoc<CtElement>();
133
134     Factory mainFactory;
135
136     /**
137      * Default constructor.
138      */

139     public DefaultCoreFactory() {
140     }
141
142     @SuppressWarnings JavaDoc("unchecked")
143     public <T> T clone(T object) {
144         if (object == null)
145             return null;
146         // System.err.println(" * cloning object " +
147
// object.getClass().getSimpleName());
148
T result = null;
149         try {
150             if (!(object instanceof CtElement || object instanceof CtReference)) {
151                 return object;
152             }
153             // if
154
// ((!object.getClass().getName().startsWith("spoon.reflect.support.code")))
155
// {
156
// return object;
157
// }
158
if (object instanceof Cloneable JavaDoc) {
159                 return (T) object.getClass().getMethod("clone").invoke(object);
160             }
161             if (object.getClass().isEnum()) {
162                 return object;
163             }
164             // System.err.println("cloning " + object + "["
165
// + object.getClass().getSimpleName() + "]");
166
result = (T) object.getClass().newInstance();
167             if (result instanceof CtElement) {
168                 if (cloningContext.isEmpty()) {
169                     cloningContext.push(((CtElement) result).getParent());
170                 }
171                 cloningContext.push((CtElement) result);
172             }
173             for (Field JavaDoc f : RtHelper.getAllFields(object.getClass())) {
174                 // if (!clonedFields.contains(f)) {
175
// clonedFields.push(f);
176
f.setAccessible(true);
177                 if (f.getName().equals("parent")) {
178                     // if (!cloningContext.isEmpty()) {
179
((CtElement) result).setParent(cloningContext
180                             .get(cloningContext.size() - 2));
181                     // }
182
} else {
183                     Object JavaDoc fieldValue = f.get(object);
184                     if (!Modifier.isFinal(f.getModifiers())
185                             && !Modifier.isStatic(f.getModifiers())) {
186                         if (fieldValue instanceof Collection JavaDoc) {
187                             // System.err.println(" cloning collection " + f+" :
188
// "+cloningContext.peek().getClass().getSimpleName());
189
Collection JavaDoc c = (Collection JavaDoc) fieldValue.getClass()
190                                     .getMethod("clone").invoke(fieldValue);
191                             c.clear();
192                             f.set(result, c);
193                             for (Object JavaDoc o : (Collection JavaDoc) fieldValue) {
194                                 c.add(clone(o));
195                             }
196                         } else if (fieldValue instanceof Map JavaDoc) {
197                             // System.err.println(" cloning collection " + f+" :
198
// "+cloningContext.peek().getClass().getSimpleName());
199
Map JavaDoc m = (Map JavaDoc) fieldValue.getClass().getMethod(
200                                     "clone").invoke(fieldValue);
201                             // m.clear();
202
f.set(result, m);
203                             for (Entry e : ((Map JavaDoc<?, ?>) fieldValue).entrySet()) {
204                                 m.put(e.getKey(), clone(e.getValue()));
205                             }
206                         } else if ((object instanceof CtReference) && (fieldValue instanceof CtElement)) {
207
208                             f.set(result, fieldValue);
209                         } else {
210                             // System.err.println(" cloning field " + f+" :
211
// "+cloningContext.peek().getClass().getSimpleName());
212
f.set(result, clone(f.get(object)));
213                         }
214                     }
215                 }
216                 // clonedFields.pop();
217
}
218             // }
219
if (result instanceof CtElement) {
220                 cloningContext.pop();
221             }
222         } catch (Exception JavaDoc e) {
223             e.printStackTrace();
224         }
225         return result;
226
227     }
228
229     @SuppressWarnings JavaDoc("unchecked")
230     public <A extends Annotation JavaDoc> CtAnnotation<A> createAnnotation() {
231         CtAnnotation e = new CtAnnotationImpl<A>();
232         e.setFactory(getMainFactory());
233         return e;
234     }
235
236     public <T extends Annotation JavaDoc> CtAnnotationType<T> createAnnotationType() {
237         CtAnnotationType<T> e = new CtAnnotationTypeImpl<T>();
238         e.setFactory(getMainFactory());
239         return e;
240     }
241
242     public CtAnonymousExecutable createAnonymousExecutable() {
243         CtAnonymousExecutable e = new CtAnonymousExecutableImpl();
244         e.setFactory(getMainFactory());
245         return e;
246     }
247
248     public <T, E extends CtExpression<?>> CtArrayAccess<T, E> createArrayAccess() {
249         CtArrayAccess<T, E> e = new CtArrayAccessImpl<T, E>();
250         e.setFactory(getMainFactory());
251         return e;
252     }
253
254     public <T> CtArrayTypeReference<T> createArrayTypeReference() {
255         CtArrayTypeReference<T> e = new CtArrayTypeReferenceImpl<T>();
256         e.setFactory(getMainFactory());
257         return e;
258     }
259
260     public CtAssert createAssert() {
261         CtAssert e = new CtAssertImpl();
262         e.setFactory(getMainFactory());
263         return e;
264     }
265
266     public <T, A extends T> CtAssignment<T, A> createAssignment() {
267         CtAssignment<T, A> e = new CtAssignmentImpl<T, A>();
268         e.setFactory(getMainFactory());
269         return e;
270     }
271
272     public <T> CtBinaryOperator<T> createBinaryOperator() {
273         CtBinaryOperator<T> e = new CtBinaryOperatorImpl<T>();
274         e.setFactory(getMainFactory());
275         return e;
276     }
277
278     public <R> CtBlock<R> createBlock() {
279         CtBlock<R> e = new CtBlockImpl<R>();
280         e.setFactory(getMainFactory());
281         return e;
282     }
283
284     public CtBreak createBreak() {
285         CtBreak e = new CtBreakImpl();
286         e.setFactory(getMainFactory());
287         return e;
288     }
289
290     public <S> CtCase<S> createCase() {
291         CtCase<S> e = new CtCaseImpl<S>();
292         e.setFactory(getMainFactory());
293         return e;
294     }
295
296     public CtCatch createCatch() {
297         CtCatch e = new CtCatchImpl();
298         e.setFactory(getMainFactory());
299         return e;
300     }
301
302     public <T> CtClass<T> createClass() {
303         CtClass<T> e = new CtClassImpl<T>();
304         e.setFactory(getMainFactory());
305         return e;
306     }
307
308     public <T> CtConditional<T> createConditional() {
309         CtConditional<T> e = new CtConditionalImpl<T>();
310         e.setFactory(getMainFactory());
311         return e;
312     }
313
314     public <T> CtConstructor<T> createConstructor() {
315         CtConstructor<T> e = new CtConstructorImpl<T>();
316         e.setFactory(getMainFactory());
317         return e;
318     }
319
320     public CtContinue createContinue() {
321         CtContinue e = new CtContinueImpl();
322         e.setFactory(getMainFactory());
323         return e;
324     }
325
326     public CtDo createDo() {
327         CtDo e = new CtDoImpl();
328         e.setFactory(getMainFactory());
329         return e;
330     }
331
332     public <T extends Enum JavaDoc> CtEnum<T> createEnum() {
333         CtEnum<T> e = new CtEnumImpl<T>();
334         e.setFactory(getMainFactory());
335         return e;
336     }
337
338     public <T> CtExecutableReference<T> createExecutableReference() {
339         CtExecutableReference<T> e = new CtExecutableReferenceImpl<T>();
340         e.setFactory(getMainFactory());
341         return e;
342     }
343
344     public <T> CtField<T> createField() {
345         CtField<T> e = new CtFieldImpl<T>();
346         e.setFactory(getMainFactory());
347         return e;
348     }
349
350     public <T> CtFieldAccess<T> createFieldAccess() {
351         CtFieldAccess<T> e = new CtFieldAccessImpl<T>();
352         e.setFactory(getMainFactory());
353         return e;
354     }
355
356     public <T> CtFieldReference<T> createFieldReference() {
357         CtFieldReference<T> e = new CtFieldReferenceImpl<T>();
358         e.setFactory(getMainFactory());
359         return e;
360     }
361
362     public CtFor createFor() {
363         CtFor e = new CtForImpl();
364         e.setFactory(getMainFactory());
365         return e;
366     }
367
368     public CtForEach createForEach() {
369         CtForEach e = new CtForEachImpl();
370         e.setFactory(getMainFactory());
371         return e;
372     }
373
374     public CtIf createIf() {
375         CtIf e = new CtIfImpl();
376         e.setFactory(getMainFactory());
377         return e;
378     }
379
380     public <T> CtInterface<T> createInterface() {
381         CtInterface<T> e = new CtInterfaceImpl<T>();
382         e.setFactory(getMainFactory());
383         return e;
384     }
385
386     public <T> CtInvocation<T> createInvocation() {
387         CtInvocation<T> e = new CtInvocationImpl<T>();
388         e.setFactory(getMainFactory());
389         return e;
390     }
391
392     public <T> CtLiteral<T> createLiteral() {
393         CtLiteral<T> e = new CtLiteralImpl<T>();
394         e.setFactory(getMainFactory());
395         return e;
396     }
397
398     public <T> CtLocalVariable<T> createLocalVariable() {
399         CtLocalVariable<T> e = new CtLocalVariableImpl<T>();
400         e.setFactory(getMainFactory());
401         return e;
402     }
403
404     public <T> CtLocalVariableReference<T> createLocalVariableReference() {
405         CtLocalVariableReference<T> e = new CtLocalVariableReferenceImpl<T>();
406         e.setFactory(getMainFactory());
407         return e;
408     }
409
410     public <T> CtMethod<T> createMethod() {
411         CtMethod<T> e = new CtMethodImpl<T>();
412         e.setFactory(getMainFactory());
413         return e;
414     }
415
416     public <T> CtNewArray<T> createNewArray() {
417         CtNewArray<T> e = new CtNewArrayImpl<T>();
418         e.setFactory(getMainFactory());
419         return e;
420     }
421
422     public <T> CtNewClass<T> createNewClass() {
423         CtNewClass<T> e = new CtNewClassImpl<T>();
424         e.setFactory(getMainFactory());
425         return e;
426     }
427
428     public <T, A extends T> CtOperatorAssignment<T, A> createOperatorAssignment() {
429         CtOperatorAssignment<T, A> e = new CtOperatorAssignmentImpl<T, A>();
430         e.setFactory(getMainFactory());
431         return e;
432     }
433
434     public CtPackage createPackage() {
435         CtPackage e = new CtPackageImpl();
436         e.setFactory(getMainFactory());
437         return e;
438     }
439
440     public CtPackageReference createPackageReference() {
441         CtPackageReference e = new CtPackageReferenceImpl();
442         e.setFactory(getMainFactory());
443         return e;
444     }
445
446     public <T> CtParameter<T> createParameter() {
447         CtParameter<T> e = new CtParameterImpl<T>();
448         e.setFactory(getMainFactory());
449         return e;
450     }
451
452     public <T> CtParameterReference<T> createParameterReference() {
453         CtParameterReference<T> e = new CtParameterReferenceImpl<T>();
454         e.setFactory(getMainFactory());
455         return e;
456     }
457
458     public <R> CtReturn<R> createReturn() {
459         CtReturn<R> e = new CtReturnImpl<R>();
460         e.setFactory(getMainFactory());
461         return e;
462     }
463
464     public <R> CtStatementList<R> createStatementList() {
465         CtStatementList<R> e = new CtStatementListImpl<R>();
466         e.setFactory(getMainFactory());
467         return e;
468     }
469
470     public <S> CtSwitch<S> createSwitch() {
471         CtSwitch<S> e = new CtSwitchImpl<S>();
472         e.setFactory(getMainFactory());
473         return e;
474     }
475
476     public CtSynchronized createSynchronized() {
477         CtSynchronized e = new CtSynchronizedImpl();
478         e.setFactory(getMainFactory());
479         return e;
480     }
481
482     public CtThrow createThrow() {
483         CtThrow e = new CtThrowImpl();
484         e.setFactory(getMainFactory());
485         return e;
486     }
487
488     public CtTry createTry() {
489         CtTry e = new CtTryImpl();
490         e.setFactory(getMainFactory());
491         return e;
492     }
493
494     public CtTypeParameter createTypeParameter() {
495         CtTypeParameter e = new CtTypeParameterImpl();
496         e.setFactory(getMainFactory());
497         return e;
498     }
499
500     public CtTypeParameterReference createTypeParameterReference() {
501         CtTypeParameterReference e = new CtTypeParameterReferenceImpl();
502         e.setFactory(getMainFactory());
503         return e;
504     }
505
506     public <T> CtTypeReference<T> createTypeReference() {
507         CtTypeReference<T> e = new CtTypeReferenceImpl<T>();
508         e.setFactory(getMainFactory());
509         return e;
510     }
511
512     public <T> CtUnaryOperator<T> createUnaryOperator() {
513         CtUnaryOperator<T> e = new CtUnaryOperatorImpl<T>();
514         e.setFactory(getMainFactory());
515         return e;
516     }
517
518     public <T> CtVariableAccess<T> createVariableAccess() {
519         CtVariableAccess<T> e = new CtVariableAccessImpl<T>();
520         e.setFactory(getMainFactory());
521         return e;
522     }
523
524     public CtWhile createWhile() {
525         CtWhile e = new CtWhileImpl();
526         e.setFactory(getMainFactory());
527         return e;
528     }
529
530     public Factory getMainFactory() {
531         return mainFactory;
532     }
533
534     public void setMainFactory(Factory mainFactory) {
535         this.mainFactory = mainFactory;
536     }
537
538     public SourcePosition createSourcePosition(File JavaDoc sourceFileName, int start,
539             int end, int[] lineSeparatorPositions) {
540         return new SourcePositionImpl(sourceFileName, start, end,
541                 lineSeparatorPositions);
542     }
543
544 }
545
Popular Tags