As a return type of listener method,ILink can be used to perform a
redirect-after-post.So it will be a great convenience to inject ILink
using annotations.
@InjectPageLink("Home")
public abstract ILink getHomeLink();
@InjectExternalLink("ShowCategory")
public abstract ILink getCategoryLink(Long id);
public ILink goHome(){
return getHomeLink();
}
public ILink showCategory(Long id){
return getCategoryLink(id);
}
The sources:
PageLink
@Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectPageLink {
String value();
}
public class InjectPageLinkAnnotationWorker implements
MethodAnnotationEnhancementWorker {
public void performEnhancement(EnhancementOperation op,
IComponentSpecification spec, Method method, Location
location) {
if (!method.getReturnType().equals(ILink.class))
throw new ApplicationRuntimeException(
"InjectPageLink annotation must return
ILink");
InjectPageLink injectPageLink = method
.getAnnotation(InjectPageLink.class);
String pageName = injectPageLink.value();
BodyBuilder builder = new BodyBuilder();
builder.begin();
builder
.addln(
"return
getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.PAGE_SERVICE).getLink(false,\"{0}\");",
pageName);
builder.end();
op.addMethod(Modifier.PUBLIC, new MethodSignature(method),
builder
.toString(), location);
if (isGetter(method))
op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
}
boolean isGetter(Method method) {
return method.getName().startsWith("get")
&& method.getParameterTypes().length == 0;
}
}
ExternalLink
@Target( { ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectExternalLink {
String value();
}
public class InjectExternalLinkAnnotationWorker implements
MethodAnnotationEnhancementWorker {
public void performEnhancement(EnhancementOperation op,
IComponentSpecification spec, Method method, Location
location) {
if (!method.getReturnType().equals(ILink.class))
throw new ApplicationRuntimeException(
"injectExternalLink annotation must return
ILink");
InjectExternalLink injectExternalLink = method
.getAnnotation(InjectExternalLink.class);
String pageName = injectExternalLink.value();
BodyBuilder builder = new BodyBuilder();
builder.begin();
Class[] parameterTypes = method.getParameterTypes();
int paramCount = Tapestry.size(parameterTypes);
if (paramCount > 0) {
if (parameterTypes[0].isArray()) {
builder
.addln(
"return
getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,
new org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",$1));",
pageName);
} else {
builder
.addln(
"java.lang.Object[]
params = new java.lang.Object[{0}];",
paramCount);
for (int i = 0; i < paramCount; i++) {
builder.add("params[{0}] = ", i);
if (parameterTypes[i].isPrimitive())
builder.add("($w) ");
builder.addln("${0};", i + 1);
}
builder
.addln(
"return
getPage().getRequestCycle().getInfrastructure().getServiceMap().getService(org.apache.tapestry.Tapestry.EXTERNAL_SERVICE).getLink(false,
new org.apache.tapestry.engine.ExternalServiceParameter(\"{0}\",params));",
pageName);
}
}
builder.end();
op.addMethod(Modifier.PUBLIC, new MethodSignature(method),
builder