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.declaration.CtClass;
22: import spoon.reflect.declaration.CtMethod;
23: import spoon.reflect.declaration.CtSimpleType;
24:
25: /**
26: * This class represents a template parameter that defines a block statement
27: * directly expressed in Java (must return an expression of type <code>R</code>).
28: *
29: * <p>
30: * To define a new block template parameter, you must subclass this class and
31: * implement the {@link #block()} method, which actually defines the Java block.
32: * It corresponds to a {@link spoon.reflect.code.CtBlock}.
33: */
34: public abstract class TypedBlockTemplateParameter<R> implements
35: TemplateParameter<R> {
36:
37: /**
38: * Creates a new block template parameter.
39: */
40: public TypedBlockTemplateParameter() {
41: }
42:
43: /**
44: * This method must be implemented to define the template block.
45: */
46: public abstract R block() throws Throwable;
47:
48: @SuppressWarnings("unchecked")
49: public CtBlock<R> getSubstitution(CtSimpleType targetType) {
50: CtClass<?> c;
51: c = targetType.getFactory().Template().get(this .getClass());
52: if (c == null) {
53: c = targetType.getFactory().Class().get(this .getClass());
54: }
55: CtMethod<R> m = (CtMethod<R>) c.getMethod("block");
56: if (this instanceof Template) {
57: return Substitution.substitute(targetType, (Template) this ,
58: m.getBody());
59: }
60: return targetType.getFactory().Core().clone(m.getBody());
61: }
62:
63: public R S() {
64: return null;
65: }
66: }
|