KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.reflect.factory;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import spoon.reflect.Factory;
9 import spoon.reflect.code.CtBlock;
10 import spoon.reflect.declaration.CtClass;
11 import spoon.reflect.declaration.CtConstructor;
12 import spoon.reflect.declaration.CtMethod;
13 import spoon.reflect.declaration.CtParameter;
14 import spoon.reflect.declaration.ModifierKind;
15 import spoon.reflect.reference.CtExecutableReference;
16 import spoon.reflect.reference.CtTypeReference;
17
18 /**
19  * The {@link CtConstructor} sub-factory.
20  */

21 public class ConstructorFactory extends ExecutableFactory {
22
23     private static final long serialVersionUID = 1L;
24
25     /**
26      * Creates a new constructor sub-factory.
27      * @param factory the parent factory
28      */

29     public ConstructorFactory(Factory factory) {
30         super(factory);
31     }
32
33     /**
34      * Copies a constructor into a target class.
35      *
36      * @param target
37      * the target class
38      * @param source
39      * the constructor to be copied
40      * @return the new constructor
41      */

42     public CtConstructor create(CtClass<?> target, CtConstructor source) {
43         CtConstructor newConstructor = factory.Core().clone(source);
44         target.getConstructors().add(newConstructor);
45         newConstructor.setParent(target);
46         return newConstructor;
47     }
48
49     /**
50      * Creates a constructor into a target class by copying it from a source
51      * method.
52      *
53      * @param target
54      * the target class
55      * @param source
56      * the method to be copied
57      * @return the new constructor
58      */

59     public <T> CtConstructor<T> create(CtClass<?> target, CtMethod<T> source) {
60         CtMethod<T> method = factory.Core().clone(source);
61         CtConstructor<T> newConstructor = factory.Core().createConstructor();
62         newConstructor.setAnnotations(method.getAnnotations());
63         newConstructor.setBody(method.getBody());
64         newConstructor.setDocComment(method.getDocComment());
65         newConstructor
66                 .setFormalTypeParameters(method.getFormalTypeParameters());
67         newConstructor.setModifiers(method.getModifiers());
68         newConstructor.setParameters(method.getParameters());
69         setParent(newConstructor, method.getAnnotations(), method
70                 .getBody(), method.getParameters(), method
71                 .getFormalTypeParameters());
72         target.getConstructors().add(newConstructor);
73         newConstructor.setParent(target);
74         return newConstructor;
75     }
76
77     /**
78      * Creates an empty constructor.
79      *
80      * @param modifiers
81      * the modifiers
82      * @param parameters
83      * the parameters
84      * @param thrownTypes
85      * the thrown types
86      */

87     public <T> CtConstructor<T> create(CtClass<?> target, Set JavaDoc<ModifierKind> modifiers,
88             List JavaDoc<CtParameter<?>> parameters,
89             Set JavaDoc<CtTypeReference<? extends Throwable JavaDoc>> thrownTypes) {
90         CtConstructor<T> constructor = factory.Core().createConstructor();
91         constructor.setModifiers(modifiers);
92         constructor.setParameters(parameters);
93         constructor.setThrownTypes(thrownTypes);
94         setParent(constructor, parameters);
95         target.getConstructors().add(constructor);
96         return constructor;
97     }
98
99     /**
100      * Creates a constructor.
101      *
102      * @param modifiers
103      * the modifiers
104      * @param parameters
105      * the parameters
106      * @param thrownTypes
107      * the thrown types
108      * @param body
109      * the body
110      */

111     public <T> CtConstructor<T> create(CtClass<T> target, Set JavaDoc<ModifierKind> modifiers,
112             List JavaDoc<CtParameter<?>> parameters,
113             Set JavaDoc<CtTypeReference<? extends Throwable JavaDoc>> thrownTypes,
114             CtBlock<T> body) {
115         CtConstructor<T> constructor = create(target, modifiers, parameters,
116                 thrownTypes);
117         constructor.setBody(body);
118         body.setParent(constructor);
119         return constructor;
120     }
121
122     /**
123      * Creates a constructor reference from an existing constructor.
124      */

125     public <T> CtExecutableReference<T> createReference(CtConstructor<T> c) {
126         return factory.Executable().createReference(c);
127     }
128     
129     /**
130      * Creates a constructor reference from an actual constructor.
131      */

132     @SuppressWarnings JavaDoc("unchecked")
133     public <T> CtExecutableReference<T> createReference(Constructor JavaDoc constructor) {
134         return createReference(factory.Type().createReference(
135                 constructor.getDeclaringClass()), null, constructor.getName(), factory.Type()
136                 .createReferences((List JavaDoc<Class JavaDoc<?>>)(List JavaDoc)Arrays.asList(constructor.getParameterTypes())));
137     }
138
139 }
140
Popular Tags