01: /*
02: * Spoon - http://spoon.gforge.inria.fr/
03: * Copyright (C) 2006 INRIA Futurs <renaud.pawlak@inria.fr>
04: *
05: * This software is governed by the CeCILL-C License under French law and
06: * abiding by the rules of distribution of free software. You can use, modify
07: * and/or redistribute the software under the terms of the CeCILL-C license as
08: * circulated by CEA, CNRS and INRIA at http://www.cecill.info.
09: *
10: * This program is distributed in the hope that it will be useful, but WITHOUT
11: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12: * FITNESS FOR A PARTICULAR PURPOSE. See the CeCILL-C License for more details.
13: *
14: * The fact that you are presently reading this means that you have had
15: * knowledge of the CeCILL-C license and that you accept its terms.
16: */
17:
18: package spoon.template;
19:
20: import spoon.reflect.code.CtBlock;
21: import spoon.reflect.code.CtStatementList;
22: import spoon.reflect.declaration.CtClass;
23: import spoon.reflect.declaration.CtSimpleType;
24:
25: /**
26: * This class represents a template parameter that defines a statement list
27: * directly expressed in Java (the statement list ends with a return statement
28: * returning a expression of type <code>R</code>).
29: *
30: * <p>
31: * To define a new statement list template parameter, you must subclass this
32: * class and implement the {@link #statements()} method, which actually defines
33: * the Java statements. It corresponds to a
34: * {@link spoon.reflect.code.CtStatementList}.
35: */
36: public abstract class TypedStatementListTemplateParameter<R> implements
37: TemplateParameterList<R> {
38:
39: /**
40: * Creates a new statement list template parameter.
41: */
42: public TypedStatementListTemplateParameter() {
43: }
44:
45: public CtStatementList<R> getSubstitution(CtSimpleType<?> targetType) {
46: CtClass<?> c;
47: CtBlock<?> b;
48: c = targetType.getFactory().Template().get(this .getClass());
49: if (c == null) {
50: c = targetType.getFactory().Class().get(this .getClass());
51: }
52: CtStatementList<R> l = targetType.getFactory().Core()
53: .createStatementList();
54: if (this instanceof Template) {
55: b = Substitution.substitute(targetType, (Template) this , c
56: .getMethod("statements").getBody());
57: } else {
58: b = targetType.getFactory().Core().clone(
59: c.getMethod("statements").getBody());
60: }
61: l.setStatements(b.getStatements());
62: return l;
63: }
64:
65: public R S() {
66: return null;
67: }
68:
69: /**
70: * This method must be implemented to define the template statement list.
71: */
72: public abstract R statements() throws Throwable;
73: }
|