KickJava   Java API By Example, From Geeks To Geeks.

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


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

25 public class MethodFactory extends ExecutableFactory {
26
27     private static final long serialVersionUID = 1L;
28
29     /**
30      * Creates a new method sub-factory.
31      *
32      * @param factory
33      * the parent factory
34      */

35     public MethodFactory(Factory factory) {
36         super(factory);
37     }
38
39     /**
40      * Creates a method.
41      *
42      * @param target
43      * the class where the method is inserted
44      * @param modifiers
45      * the modifiers
46      * @param returnType
47      * the method's return type
48      * @param name
49      * the method's name
50      * @param parameters
51      * the parameters
52      * @param thrownTypes
53      * the thrown types
54      * @param body
55      * the method's body
56      */

57     public <T> CtMethod<T> create(CtClass<?> target,
58             Set JavaDoc<ModifierKind> modifiers, CtTypeReference<T> returnType,
59             String JavaDoc name, List JavaDoc<CtParameter<?>> parameters,
60             Set JavaDoc<CtTypeReference<? extends Throwable JavaDoc>> thrownTypes,
61             CtBlock<T> body) {
62         CtMethod<T> method = create(target, modifiers, returnType, name,
63                 parameters, thrownTypes);
64         method.setBody(body);
65         body.setParent(method);
66         return method;
67     }
68
69     /**
70      * Creates a method by copying an existing method.
71      *
72      * @param <T>
73      * the type of the method
74      * @param target
75      * the target type where the new method has to be inserted to
76      * @param source
77      * the source method to be copied
78      * @param redirectReferences
79      * tells if all the references to the owning type of the source
80      * method should be redirected to the target type (true is
81      * recommended for most uses)
82      * @return the newly created method
83      */

84     public <T> CtMethod<T> create(CtType<?> target, CtMethod<T> source,
85             boolean redirectReferences) {
86         CtMethod<T> newMethod = factory.Core().clone(source);
87         if (redirectReferences && source.getDeclaringType() != null) {
88             Substitution.redirectTypeReferences(newMethod, source
89                     .getDeclaringType().getReference(), target.getReference());
90         }
91         target.getMethods().add(newMethod);
92         newMethod.setParent(target);
93         return newMethod;
94     }
95
96     /**
97      * Creates an empty method.
98      *
99      * @param target
100      * the class where the method is inserted
101      * @param modifiers
102      * the modifiers
103      * @param returnType
104      * the method's return type
105      * @param name
106      * the method's name
107      * @param parameters
108      * the parameters
109      * @param thrownTypes
110      * the thrown types
111      */

112     public <T> CtMethod<T> create(CtType<?> target,
113             Set JavaDoc<ModifierKind> modifiers, CtTypeReference<T> returnType,
114             String JavaDoc name, List JavaDoc<CtParameter<?>> parameters,
115             Set JavaDoc<CtTypeReference<? extends Throwable JavaDoc>> thrownTypes) {
116         CtMethod<T> method = factory.Core().createMethod();
117         target.getMethods().add(method);
118         method.setParent(target);
119         if (modifiers != null)
120             method.setModifiers(modifiers);
121         method.setType(returnType);
122         method.setSimpleName(name);
123         if (parameters != null)
124             method.setParameters(parameters);
125         setParent(method, parameters);
126         if (thrownTypes != null)
127             method.setThrownTypes(thrownTypes);
128         return method;
129     }
130
131     /**
132      * Creates a method reference.
133      */

134     public <T> CtExecutableReference<T> createReference(CtMethod<T> m) {
135         return factory.Executable().createReference(m);
136     }
137
138     /**
139      * Creates a method reference from an actual method.
140      */

141     @SuppressWarnings JavaDoc("unchecked")
142     public <T> CtExecutableReference<T> createReference(Method JavaDoc method) {
143         return createReference(factory.Type().createReference(
144                 method.getDeclaringClass()), (CtTypeReference<T>) factory
145                 .Type().createReference(method.getReturnType()), method
146                 .getName(), factory.Type().createReferences(
147                 (List JavaDoc<Class JavaDoc<?>>) (List JavaDoc) Arrays.asList(method
148                         .getParameterTypes())).toArray(
149                 new CtTypeReference<?>[0]));
150     }
151
152     /**
153      * Gets all the main methods stored in this factory.
154      */

155     public Collection JavaDoc<CtMethod> getMainMethods() {
156         Collection JavaDoc<CtMethod> methods = new ArrayList JavaDoc<CtMethod>();
157         for (CtSimpleType<?> t : factory.Type().getAll()) {
158             if (t instanceof CtClass) {
159                 CtMethod<?> m = ((CtClass<?>) t).getMethod("main", factory
160                         .Type().createArrayReference(
161                                 factory.Type().createReference(String JavaDoc.class)));
162                 if (m != null && m.getModifiers().contains(ModifierKind.STATIC)) {
163                     methods.add(m);
164                 }
165             }
166         }
167         return methods;
168     }
169
170 }
171
Popular Tags