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