1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.tacos.annotations.enhance;
15
16 import java.lang.reflect.Modifier;
17
18 import org.apache.hivemind.ApplicationRuntimeException;
19 import org.apache.hivemind.Location;
20 import org.apache.hivemind.service.BodyBuilder;
21 import org.apache.hivemind.service.MethodSignature;
22 import org.apache.hivemind.util.Defense;
23 import org.apache.tapestry.enhance.EnhancementOperation;
24 import org.apache.tapestry.enhance.InjectEnhancementWorker;
25 import org.apache.tapestry.spec.InjectSpecification;
26
27
28
29
30 public class InjectParameterFlagWorker implements InjectEnhancementWorker {
31
32 public void performEnhancement(EnhancementOperation op,
33 InjectSpecification spec) {
34 injectParameterFlag(op, spec.getObject(), spec.getProperty(), spec.getLocation());
35 }
36 void injectParameterFlag(EnhancementOperation op, String parameterName, String propertyName,
37 Location location)
38 {
39 Defense.notNull(op, "op");
40 Defense.notNull(parameterName, "parameterName");
41 Defense.notNull(propertyName, "propertyName");
42
43 Class propertyType = op.getPropertyType(propertyName);
44
45
46
47
48 if (propertyType != null && propertyType != boolean.class)
49 throw new ApplicationRuntimeException("Property " + propertyName + " must be boolean",
50 location, null);
51
52 op.claimReadonlyProperty(propertyName);
53
54 BodyBuilder builder = new BodyBuilder();
55 builder.begin();
56 builder.add("return isParameterBound(");
57 builder.addQuoted(parameterName);
58 builder.addln(");");
59 builder.end();
60
61 String methodName = op.getAccessorMethodName(propertyName);
62
63 MethodSignature sig = new MethodSignature(boolean.class, methodName, null, null);
64
65 op.addMethod(Modifier.PUBLIC, sig, builder.toString(), location);
66 }
67
68 }