KickJava   Java API By Example, From Geeks To Geeks.

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


1 package spoon.reflect.factory;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import spoon.reflect.Factory;
7 import spoon.reflect.declaration.CtElement;
8 import spoon.reflect.declaration.CtPackage;
9 import spoon.reflect.declaration.CtSimpleType;
10 import spoon.reflect.declaration.CtType;
11 import spoon.reflect.declaration.CtTypeParameter;
12 import spoon.reflect.reference.CtArrayTypeReference;
13 import spoon.reflect.reference.CtTypeParameterReference;
14 import spoon.reflect.reference.CtTypeReference;
15
16 /**
17  * The {@link CtType} sub-factory.
18  */

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

29     public TypeFactory(Factory factory) {
30         super(factory);
31     }
32
33     /**
34      * Creates a reference to an array of given type.
35      *
36      * @param <T>
37      * type of array
38      * @param type
39      * type of array values
40      */

41     public <T> CtArrayTypeReference<T[]> createArrayReference(CtSimpleType<T> type) {
42         CtArrayTypeReference<T[]> array = factory.Core()
43                 .createArrayTypeReference();
44         array.setComponentType(createReference(type));
45         return array;
46     }
47
48     /**
49      * Creates a reference to an array of given type.
50      */

51     public <T> CtArrayTypeReference<T[]> createArrayReference(
52             CtTypeReference<T> reference) {
53         CtArrayTypeReference<T[]> array = factory.Core()
54                 .createArrayTypeReference();
55         array.setComponentType(reference);
56         return array;
57     }
58
59     /**
60      * Creates a reference to an array of given type.
61      */

62     @SuppressWarnings JavaDoc("unchecked")
63     public <T> CtArrayTypeReference<T> createArrayReference(String JavaDoc qualifiedName) {
64         CtArrayTypeReference<T> array = factory.Core()
65                 .createArrayTypeReference();
66         array
67                 .setComponentType((CtTypeReference<T>) createReference(qualifiedName));
68         return array;
69     }
70
71     /**
72      * Creates a reference to a simple type
73      */

74     public <T> CtTypeReference<T> createReference(Class JavaDoc<T> type) {
75         if (type.isArray()) {
76             CtArrayTypeReference<T> array = factory.Core()
77                     .createArrayTypeReference();
78             array.setComponentType((CtTypeReference<?>) createReference(type
79                     .getComponentType()));
80             return array;
81         }
82         return createReference(type.getName());
83     }
84
85     /**
86      * Create a reference to a simple type
87      */

88     public <T> CtTypeReference<T> createReference(CtSimpleType<T> type) {
89         CtTypeReference<T> ref = factory.Core().createTypeReference();
90         if (type.isTopLevel()) {
91             ref
92                     .setPackage(factory.Package().createReference(
93                             type.getPackage()));
94         } else {
95             if (type.getDeclaringType() != null) {
96                 ref.setDeclaringType(createReference(type.getDeclaringType()));
97             }
98         }
99         ref.setSimpleName(type.getSimpleName());
100         return ref;
101     }
102
103     /**
104      * Create a reference to a simple type
105      */

106     @SuppressWarnings JavaDoc("unchecked")
107     public <T> CtTypeReference<T> createReference(String JavaDoc qualifiedName) {
108         if(qualifiedName.endsWith("[]")) {
109             return createArrayReference(qualifiedName.substring(0,qualifiedName.length()-2));
110         }
111         CtTypeReference ref = factory.Core().createTypeReference();
112         if (hasInnerType(qualifiedName) > 0) {
113             ref
114                     .setDeclaringType(createReference(getDeclaringTypeName(qualifiedName)));
115         } else if (hasPackage(qualifiedName) > 0) {
116             ref.setPackage(factory.Package().createReference(
117                     getPackageName(qualifiedName)));
118         }
119         ref.setSimpleName(getSimpleName(qualifiedName));
120         return ref;
121     }
122
123     /**
124      * Gets a created type from its qualified name.
125      *
126      * @return a found type or null if does not exist
127      */

128     @SuppressWarnings JavaDoc("unchecked")
129     public <T> CtSimpleType<T> get(String JavaDoc qualifiedName) {
130         int inertTypeIndex = qualifiedName
131                 .lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
132         if (inertTypeIndex > 0) {
133             String JavaDoc s = qualifiedName.substring(0, inertTypeIndex);
134             CtSimpleType t = (CtSimpleType<?>) get(s);
135             if (t == null)
136                 return null;
137             return t.getNestedType(qualifiedName.substring(inertTypeIndex + 1));
138         }
139
140         int packageIndex = qualifiedName
141                 .lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
142         CtPackage pack;
143         if (packageIndex > 0)
144             pack = factory.Package().get(
145                     qualifiedName.substring(0, packageIndex));
146         else
147             pack = factory.Package().get(
148                     CtPackage.TOP_LEVEL_PACKAGE_NAME);
149
150         if (pack == null)
151             return null;
152
153         return (CtSimpleType<T>)pack.getType(qualifiedName.substring(packageIndex + 1));
154     }
155
156     /**
157      * Gets the list of all created types.
158      */

159     public List JavaDoc<CtSimpleType> getAll() {
160         List JavaDoc<CtSimpleType> types = new ArrayList JavaDoc<CtSimpleType>();
161         for (CtPackage pack : factory.Package().getAll()) {
162             types.addAll(pack.getTypes());
163         }
164         return types;
165     }
166
167     /**
168      * Gets a type from its runtime Java class.
169      *
170      * @param <T>
171      * type of created class
172      * @param cl
173      * java class
174      */

175     @SuppressWarnings JavaDoc("unchecked")
176     public <T> CtSimpleType<T> get(Class JavaDoc<T> cl) {
177         return (CtSimpleType<T>)get(cl.getName());
178     }
179
180     
181     /**
182      * Gets the declaring type name for a given Java qualified name.
183      */

184     protected String JavaDoc getDeclaringTypeName(String JavaDoc qualifiedName) {
185         return qualifiedName.substring(0, hasInnerType(qualifiedName));
186     }
187
188     /**
189      * Creates a collection of type references from a collection of classes.
190      */

191     public List JavaDoc<CtTypeReference<?>> createReferences(List JavaDoc<Class JavaDoc<?>> classes) {
192         List JavaDoc<CtTypeReference<?>> refs = new ArrayList JavaDoc<CtTypeReference<?>>();
193         for (Class JavaDoc<?> c : classes) {
194             refs.add(createReference(c));
195         }
196         return refs;
197     }
198
199     /**
200      * Gets the package name for a given Java qualified name.
201      */

202     protected String JavaDoc getPackageName(String JavaDoc qualifiedName) {
203         if (hasPackage(qualifiedName) >= 0)
204             return qualifiedName.substring(0, hasPackage(qualifiedName));
205         else
206             return CtPackage.TOP_LEVEL_PACKAGE_NAME;
207     }
208
209     /**
210      * Gets the simple name for a given Java qualified name.
211      */

212     protected String JavaDoc getSimpleName(String JavaDoc qualifiedName) {
213         if (hasInnerType(qualifiedName) > 0) {
214             return qualifiedName.substring(hasInnerType(qualifiedName) + 1);
215         } else if (hasPackage(qualifiedName) > 0) {
216             return qualifiedName.substring(hasPackage(qualifiedName) + 1);
217         } else {
218             return qualifiedName;
219         }
220     }
221
222     /**
223      * Tells if a given Java qualified name is that of an inner type.
224      */

225     protected int hasInnerType(String JavaDoc qualifiedName) {
226         int ret = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
227         // if (ret < 0) {
228
// if (hasPackage(qualifiedName) > 0) {
229
// String buf = qualifiedName.substring(0,
230
// hasPackage(qualifiedName));
231
// int tmp = buf.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
232
// if (Character.isUpperCase(buf.charAt(tmp + 1))) {
233
// ret = hasPackage(qualifiedName);
234
// }
235
// }
236
// }
237
return ret;
238     }
239
240     /**
241      * Tells if a given Java qualified name contains a package name.
242      */

243     protected int hasPackage(String JavaDoc qualifiedName) {
244         return qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
245     }
246
247     /**
248      * Creates a type parameter with no bounds.
249      *
250      * @param owner
251      * the owning declaration
252      * @param name
253      * the name of the formal parameter
254      */

255     public CtTypeParameter createTypeParameter(CtElement owner, String JavaDoc name) {
256         CtTypeParameter typeParam = factory.Core().createTypeParameter();
257         typeParam.setParent(owner);
258         typeParam.setName(name);
259         return typeParam;
260     }
261
262     /**
263      * Creates a type parameter.
264      *
265      * @param owner
266      * the owning declaration
267      * @param name
268      * the name of the formal parameter
269      * @param bounds
270      * the bounds
271      */

272     public CtTypeParameter createTypeParameter(CtElement owner, String JavaDoc name,
273             List JavaDoc<CtTypeReference> bounds) {
274         CtTypeParameter typeParam = factory.Core().createTypeParameter();
275         typeParam.setParent(owner);
276         typeParam.setName(name);
277         typeParam.setBounds(bounds);
278         return typeParam;
279     }
280
281     /**
282      * Creates a type parameter reference with no bounds.
283      *
284      * @param name
285      * the name of the formal parameter
286      */

287     public CtTypeParameterReference createTypeParameterReference(String JavaDoc name) {
288         CtTypeParameterReference typeParam = factory.Core()
289                 .createTypeParameterReference();
290         typeParam.setSimpleName(name);
291         return typeParam;
292     }
293
294     /**
295      * Creates a type parameter reference.
296      *
297      * @param name
298      * the name of the formal parameter
299      * @param bounds
300      * the bounds
301      */

302     public CtTypeParameterReference createTypeParameterReference(String JavaDoc name,
303             List JavaDoc<CtTypeReference> bounds) {
304         CtTypeParameterReference typeParam = factory.Core()
305                 .createTypeParameterReference();
306         typeParam.setSimpleName(name);
307         typeParam.setBounds(bounds);
308         return typeParam;
309     }
310
311 }
312
Popular Tags