📄 injectexternallinkannotationworker.java
字号:
package com.cucu.tapestry.annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.hivemind.Location;
import org.apache.hivemind.service.BodyBuilder;
import org.apache.hivemind.service.MethodSignature;
import org.apache.tapestry.Tapestry;
import org.apache.tapestry.annotations.AnnotationUtils;
import org.apache.tapestry.annotations.MethodAnnotationEnhancementWorker;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.enhance.EnhancementOperation;
import org.apache.tapestry.spec.IComponentSpecification;
/**
* @author Chris
* @version $Id$
*/
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) "); // Parameter $0 is this, so $1 is the first real parameter.
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.toString(), location);
if (isGetter(method)) op.claimReadonlyProperty(AnnotationUtils.getPropertyName(method));
}
boolean isGetter(Method method) { // We already know the return type is String
return method.getName().startsWith("get") && method.getParameterTypes().length == 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -