001: /*
002: * Spoon - http://spoon.gforge.inria.fr/
003: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
004: *
005: * This software is governed by the CeCILL-C License under French law and
006: * abiding by the rules of distribution of free software. You can use, modify
007: * and/or redistribute the software under the terms of the CeCILL-C license as
008: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
009: *
010: * This program is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
013: *
014: * The fact that you are presently reading this means that you have had
015: * knowledge of the CeCILL-C license and that you accept its terms.
016: */
017:
018: package spoon.reflect.factory;
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.StringTokenizer;
023:
024: import spoon.reflect.Factory;
025: import spoon.reflect.code.CtBlock;
026: import spoon.reflect.declaration.CtAnonymousExecutable;
027: import spoon.reflect.declaration.CtClass;
028: import spoon.reflect.declaration.CtConstructor;
029: import spoon.reflect.declaration.CtExecutable;
030: import spoon.reflect.declaration.CtMethod;
031: import spoon.reflect.declaration.CtParameter;
032: import spoon.reflect.reference.CtExecutableReference;
033: import spoon.reflect.reference.CtParameterReference;
034: import spoon.reflect.reference.CtTypeReference;
035:
036: /**
037: * The {@link CtExecutable} sub-factory.
038: */
039: public class ExecutableFactory extends SubFactory {
040:
041: private static final long serialVersionUID = 1L;
042:
043: /**
044: * Creates a new executable sub-factory.
045: *
046: * @param factory
047: * the parent factory
048: */
049: public ExecutableFactory(Factory factory) {
050: super (factory);
051: }
052:
053: /**
054: * Creates an anonymous executable (initializer block) in a target class).
055: */
056: public CtAnonymousExecutable createAnonymous(CtClass<?> target,
057: CtBlock<?> body) {
058: CtAnonymousExecutable a = factory.Core()
059: .createAnonymousExecutable();
060: target.getAnonymousExecutables().add(a);
061: a.setParent(target);
062: a.setBody(body);
063: body.setParent(a);
064: return a;
065: }
066:
067: /**
068: * Creates a new parameter.
069: */
070: public <T> CtParameter<T> createParameter(CtExecutable<?> parent,
071: CtTypeReference<T> type, String name) {
072: CtParameter<T> parameter = factory.Core().createParameter();
073: parameter.setType(type);
074: parameter.setSimpleName(name);
075: if (parent != null) {
076: parent.getParameters().add(parameter);
077: parameter.setParent(parent);
078: }
079: return parameter;
080: }
081:
082: /**
083: * Creates a parameter reference from an existing parameter.
084: *
085: * @param <T>
086: * the parameter's type
087: * @param parameter
088: * the parameter
089: */
090: @SuppressWarnings("unchecked")
091: public <T> CtParameterReference<T> createParameterReference(
092: CtParameter<T> parameter) {
093: CtParameterReference<T> ref = factory.Core()
094: .createParameterReference();
095: if (parameter.getParent() != null) {
096: ref.setDeclaringExecutable(factory.Executable()
097: .createReference(
098: (CtExecutable) parameter.getParent()));
099: }
100: ref.setSimpleName(parameter.getSimpleName());
101: ref.setType(parameter.getType());
102: return ref;
103: }
104:
105: /**
106: * Creates an executable reference from an existing executable.
107: */
108: public <T> CtExecutableReference<T> createReference(
109: CtExecutable<T> e) {
110: CtTypeReference<?> refs[] = new CtTypeReference[e
111: .getParameters().size()];
112: int i = 0;
113: for (CtParameter<?> param : e.getParameters()) {
114: refs[i++] = param.getType();
115: }
116: if (e instanceof CtMethod) {
117: return createReference(e.getDeclaringType().getReference(),
118: ((CtMethod<T>) e).getType(), e.getSimpleName(),
119: refs);
120: }
121: return createReference(e.getDeclaringType().getReference(),
122: ((CtConstructor<T>) e).getType(), e.getSimpleName(),
123: refs);
124: }
125:
126: /**
127: * Creates an executable reference.
128: *
129: * @param declaringType
130: * reference to the declaring type
131: * @param type
132: * the executable's type
133: * @param methodName
134: * simple name
135: * @param parameterTypes
136: * list of parameter's types
137: */
138: public <T> CtExecutableReference<T> createReference(
139: CtTypeReference<?> declaringType, CtTypeReference<T> type,
140: String methodName, CtTypeReference<?>... parameterTypes) {
141: CtExecutableReference<T> methodRef = factory.Core()
142: .createExecutableReference();
143: methodRef.setDeclaringType(declaringType);
144: methodRef.setSimpleName(methodName);
145: methodRef.setType(type);
146: List<CtTypeReference<?>> l = new ArrayList<CtTypeReference<?>>();
147: for (CtTypeReference<?> ref : parameterTypes) {
148: l.add(ref);
149: }
150: methodRef.setParameterTypes(l);
151: return methodRef;
152: }
153:
154: /**
155: * Creates an executable reference.
156: *
157: * @param declaringType
158: * reference to the declaring type
159: * @param isStatic
160: * if this reference references a static executable
161: * @param type
162: * the return type of the executable
163: * @param methodName
164: * simple name
165: * @param parameterTypes
166: * list of parameter's types
167: */
168: public <T> CtExecutableReference<T> createReference(
169: CtTypeReference<?> declaringType, boolean isStatic,
170: CtTypeReference<T> type, String methodName,
171: CtTypeReference<?>... parameterTypes) {
172: CtExecutableReference<T> methodRef = factory.Core()
173: .createExecutableReference();
174: methodRef.setStatic(isStatic);
175: methodRef.setDeclaringType(declaringType);
176: methodRef.setSimpleName(methodName);
177: methodRef.setType(type);
178: List<CtTypeReference<?>> l = new ArrayList<CtTypeReference<?>>();
179: for (CtTypeReference<?> ref : parameterTypes) {
180: l.add(ref);
181: }
182: methodRef.setParameterTypes(l);
183: return methodRef;
184: }
185:
186: /**
187: * Creates an executable reference.
188: *
189: * @param declaringType
190: * reference to the declaring type
191: * @param isStatic
192: * if this reference references a static executable
193: * @param type
194: * the return type of the executable
195: * @param methodName
196: * simple name
197: * @param parameterTypes
198: * list of parameter's types
199: */
200: public <T> CtExecutableReference<T> createReference(
201: CtTypeReference<?> declaringType, boolean isStatic,
202: CtTypeReference<T> type, String methodName,
203: List<CtTypeReference<?>> parameterTypes) {
204: CtExecutableReference<T> methodRef = factory.Core()
205: .createExecutableReference();
206: methodRef.setStatic(isStatic);
207: methodRef.setDeclaringType(declaringType);
208: methodRef.setSimpleName(methodName);
209: methodRef.setType(type);
210: List<CtTypeReference<?>> l = new ArrayList<CtTypeReference<?>>();
211: for (CtTypeReference<?> ref : parameterTypes) {
212: l.add(ref);
213: }
214: methodRef.setParameterTypes(l);
215: return methodRef;
216: }
217:
218: /**
219: * Creates an executable reference.
220: *
221: * @param declaringType
222: * reference to the declaring type
223: * @param type
224: * the return type of the executable
225: * @param methodName
226: * simple name
227: * @param parameterTypes
228: * list of parameter's types
229: */
230: public <T> CtExecutableReference<T> createReference(
231: CtTypeReference<?> declaringType, CtTypeReference<T> type,
232: String methodName, List<CtTypeReference<?>> parameterTypes) {
233: CtExecutableReference<T> methodRef = factory.Core()
234: .createExecutableReference();
235: methodRef.setDeclaringType(declaringType);
236: methodRef.setSimpleName(methodName);
237: methodRef.setType(type);
238: List<CtTypeReference<?>> l = new ArrayList<CtTypeReference<?>>();
239: for (CtTypeReference<?> ref : parameterTypes) {
240: l.add(ref);
241: }
242: methodRef.setParameterTypes(l);
243: return methodRef;
244: }
245:
246: /**
247: * Creates an executable reference from its signature, as defined by the
248: * executable reference's toString.
249: */
250: public <T> CtExecutableReference<T> createReference(String signature) {
251: CtExecutableReference<T> executableRef = factory.Core()
252: .createExecutableReference();
253: String type = signature.substring(0, signature.indexOf(" "));
254: String declaringType = signature.substring(signature
255: .indexOf(" ") + 1, signature
256: .indexOf(CtExecutable.EXECUTABLE_SEPARATOR));
257: String executableName = signature.substring(signature
258: .indexOf(CtExecutable.EXECUTABLE_SEPARATOR) + 1,
259: signature.indexOf("("));
260: executableRef.setSimpleName(executableName);
261: executableRef.setDeclaringType(factory.Type().createReference(
262: declaringType));
263: CtTypeReference<T> typeRef = factory.Type().createReference(
264: type);
265: executableRef.setType(typeRef);
266: String parameters = signature.substring(
267: signature.indexOf("(") + 1, signature.indexOf(")"));
268: StringTokenizer t = new StringTokenizer(parameters, ",");
269: while (t.hasMoreTokens()) {
270: String paramType = t.nextToken();
271: executableRef.getParameterTypes().add(
272: factory.Type().createReference(paramType));
273: }
274: return executableRef;
275: }
276:
277: }
|