KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

32     public ExecutableFactory(Factory factory) {
33         super(factory);
34     }
35
36     /**
37      * Creates an anonymous executable (initializer block) in a target class).
38      */

39     public CtAnonymousExecutable createAnonymous(CtClass<?> target, CtBlock body) {
40         CtAnonymousExecutable a = factory.Core().createAnonymousExecutable();
41         target.getAnonymousExecutables().add(a);
42         a.setParent(target);
43         a.setBody(body);
44         body.setParent(a);
45         return a;
46     }
47
48     /**
49      * Creates a new parameter.
50      */

51     public <T> CtParameter<T> createParameter(CtExecutable<?> parent,
52             CtTypeReference<T> type, String JavaDoc name) {
53         CtParameter<T> parameter = factory.Core().createParameter();
54         parameter.setType(type);
55         parameter.setSimpleName(name);
56         if (parent != null) {
57             parent.getParameters().add(parameter);
58             parameter.setParent(parent);
59         }
60         return parameter;
61     }
62
63     /**
64      * Creates a parameter reference from an existing parameter.
65      *
66      * @param <T>
67      * the parameter's type
68      * @param parameter
69      * the parameter
70      */

71     @SuppressWarnings JavaDoc("unchecked")
72     public <T> CtParameterReference<T> createParameterReference(
73             CtParameter<T> parameter) {
74         CtParameterReference<T> ref = factory.Core().createParameterReference();
75         if (parameter.getParent() != null)
76             ref.setDeclaringExecutable(factory.Executable().createReference(
77                     (CtExecutable) parameter.getParent()));
78         ref.setSimpleName(parameter.getSimpleName());
79         ref.setType(parameter.getType());
80         return ref;
81     }
82
83     /**
84      * Creates an executable reference from an existing executable.
85      */

86     public <T> CtExecutableReference<T> createReference(CtExecutable<T> e) {
87         CtTypeReference<?> refs[] = new CtTypeReference[e.getParameters()
88                 .size()];
89         int i = 0;
90         for (CtParameter<?> param : e.getParameters()) {
91             refs[i++] = param.getType();
92         }
93         if (e instanceof CtMethod) {
94             return createReference(e.getDeclaringType().getReference(),
95                     ((CtMethod<T>) e).getType(), e.getSimpleName(), refs);
96         } else {
97             return createReference(e.getDeclaringType().getReference(),
98                     ((CtConstructor<T>) e).getType(), e.getSimpleName(), refs);
99         }
100     }
101
102     /**
103      * Creates an executable reference.
104      *
105      * @param declaringType
106      * reference to the declaring type
107      * @param type
108      * the executable's type
109      * @param methodName
110      * simple name
111      * @param parameterTypes
112      * list of parameter's types
113      */

114     public <T> CtExecutableReference<T> createReference(
115             CtTypeReference<?> declaringType, CtTypeReference<T> type,
116             String JavaDoc methodName, CtTypeReference<?>... parameterTypes) {
117         CtExecutableReference<T> methodRef = factory.Core()
118                 .createExecutableReference();
119         methodRef.setDeclaringType(declaringType);
120         methodRef.setSimpleName(methodName);
121         methodRef.setType(type);
122         List JavaDoc<CtTypeReference<?>> l = new ArrayList JavaDoc<CtTypeReference<?>>();
123         for (CtTypeReference<?> ref : parameterTypes)
124             l.add(ref);
125         methodRef.setParameterTypes(l);
126         return methodRef;
127     }
128
129     /**
130      * Creates an executable reference.
131      *
132      * @param declaringType
133      * reference to the declaring type
134      * @param isStatic
135      * if this reference references a static executable
136      * @param type
137      * the return type of the executable
138      * @param methodName
139      * simple name
140      * @param parameterTypes
141      * list of parameter's types
142      */

143     public <T> CtExecutableReference<T> createReference(
144             CtTypeReference<?> declaringType, boolean isStatic,
145             CtTypeReference<T> type, String JavaDoc methodName,
146             CtTypeReference<?>... parameterTypes) {
147         CtExecutableReference<T> methodRef = factory.Core()
148                 .createExecutableReference();
149         methodRef.setStatic(isStatic);
150         methodRef.setDeclaringType(declaringType);
151         methodRef.setSimpleName(methodName);
152         methodRef.setType(type);
153         List JavaDoc<CtTypeReference<?>> l = new ArrayList JavaDoc<CtTypeReference<?>>();
154         for (CtTypeReference<?> ref : parameterTypes)
155             l.add(ref);
156         methodRef.setParameterTypes(l);
157         return methodRef;
158     }
159
160     /**
161      * Creates an executable reference.
162      *
163      * @param declaringType
164      * reference to the declaring type
165      * @param isStatic
166      * if this reference references a static executable
167      * @param type
168      * the return type of the executable
169      * @param methodName
170      * simple name
171      * @param parameterTypes
172      * list of parameter's types
173      */

174     public <T> CtExecutableReference<T> createReference(
175             CtTypeReference<?> declaringType, boolean isStatic,
176             CtTypeReference<T> type, String JavaDoc methodName,
177             List JavaDoc<CtTypeReference<?>> parameterTypes) {
178         CtExecutableReference<T> methodRef = factory.Core()
179                 .createExecutableReference();
180         methodRef.setStatic(isStatic);
181         methodRef.setDeclaringType(declaringType);
182         methodRef.setSimpleName(methodName);
183         methodRef.setType(type);
184         List JavaDoc<CtTypeReference<?>> l = new ArrayList JavaDoc<CtTypeReference<?>>();
185         for (CtTypeReference<?> ref : parameterTypes)
186             l.add(ref);
187         methodRef.setParameterTypes(l);
188         return methodRef;
189     }
190
191     /**
192      * Creates an executable reference.
193      *
194      * @param declaringType
195      * reference to the declaring type
196      * @param type
197      * the return type of the executable
198      * @param methodName
199      * simple name
200      * @param parameterTypes
201      * list of parameter's types
202      */

203     public <T> CtExecutableReference<T> createReference(
204             CtTypeReference<?> declaringType, CtTypeReference<T> type,
205             String JavaDoc methodName, List JavaDoc<CtTypeReference<?>> parameterTypes) {
206         CtExecutableReference<T> methodRef = factory.Core()
207                 .createExecutableReference();
208         methodRef.setDeclaringType(declaringType);
209         methodRef.setSimpleName(methodName);
210         methodRef.setType(type);
211         List JavaDoc<CtTypeReference<?>> l = new ArrayList JavaDoc<CtTypeReference<?>>();
212         for (CtTypeReference<?> ref : parameterTypes)
213             l.add(ref);
214         methodRef.setParameterTypes(l);
215         return methodRef;
216     }
217
218     /**
219      * Creates an executable reference from its signature, as defined by the
220      * executable reference's toString.
221      */

222     public <T> CtExecutableReference<T> createReference(String JavaDoc signature) {
223         CtExecutableReference<T> executableRef = factory.Core().createExecutableReference();
224         String JavaDoc type = signature.substring(0, signature.indexOf(" "));
225         String JavaDoc declaringType = signature.substring(signature.indexOf(" ") + 1,
226                 signature.indexOf(CtExecutable.EXECUTABLE_SEPARATOR));
227         String JavaDoc executableName = signature.substring(signature
228                 .indexOf(CtExecutable.EXECUTABLE_SEPARATOR) + 1,signature.indexOf("("));
229         executableRef.setSimpleName(executableName);
230         executableRef
231                 .setDeclaringType(factory.Type().createReference(declaringType));
232         CtTypeReference<T> typeRef = factory.Type().createReference(type);
233         executableRef.setType(typeRef);
234         String JavaDoc parameters=signature.substring(signature.indexOf("(")+1, signature.indexOf(")"));
235         StringTokenizer JavaDoc t=new StringTokenizer JavaDoc(parameters,",");
236         while(t.hasMoreTokens()) {
237             String JavaDoc paramType=t.nextToken();
238             executableRef.getParameterTypes().add(factory.Type().createReference(paramType));
239         }
240         return executableRef;
241     }
242
243 }
244
Popular Tags