1
2
3
4
5
6
7
8
9
10
11
12
13
14 package net.sf.tacos.annotations;
15
16 import java.lang.reflect.Method;
17 import java.lang.reflect.Modifier;
18
19 import org.apache.hivemind.ApplicationRuntimeException;
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.service.BodyBuilder;
22 import org.apache.hivemind.service.MethodSignature;
23 import org.apache.tapestry.annotations.AnnotationUtils;
24 import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
25 import org.apache.tapestry.engine.ILink;
26 import org.apache.tapestry.enhance.EnhancementOperation;
27 import org.apache.tapestry.spec.IComponentSpecification;
28
29
30
31
32
33
34 public class InjectPageLinkAnnotationWorker implements MethodAnnotationEnhancementWorker {
35
36 public void performEnhancement(EnhancementOperation op, IComponentSpecification spec,
37 Method method, Location location) {
38 if (!method.getReturnType().equals(ILink.class))
39 throw new ApplicationRuntimeException(
40 "InjectPageLink annotation must return ILink");
41
42 InjectPageLink injectPageLink = method
43 .getAnnotation(InjectPageLink.class);
44
45 String pageName = injectPageLink.value();
46
47 BodyBuilder builder = new BodyBuilder();
48
49 builder.begin();
50 builder.addln("String pageName=\"{0}\";", pageName);
51 if (injectPageLink.currentNamespace())
52 builder.addln("pageName=getNamespace().constructQualifiedName(pageName);");
53 builder.addln( "return getPage().getRequestCycle().getInfrastructure().getServiceMap()." +
54 "getService(org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,pageName);");
55
56 builder.end();
57
58 op.addMethod(Modifier.PUBLIC, new MethodSignature(method), builder
59 .toString(), location);
60
61 if (isGetter(method))
62 op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
63 }
64
65 boolean isGetter(Method method) {
66 return method.getName().startsWith("get")
67 && method.getParameterTypes().length == 0;
68 }
69 }