use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.
the class JCacheModule method advise.
private static void advise(Class<? extends Annotation> annotationClass, ObjectLocator objectLocator, Class<? extends CacheMethodAdvice> adviceClass, MethodAdviceReceiver methodAdviceReceiver) {
// TAP5-2466: create the advice on-demand to avoid recursion issues
MethodAdvice advice = null;
if (methodAdviceReceiver.getClassAnnotationProvider().getAnnotation(annotationClass) != null) {
advice = build(objectLocator, adviceClass);
methodAdviceReceiver.adviseAllMethods(advice);
} else {
for (Method method : methodAdviceReceiver.getInterface().getMethods()) {
if (methodAdviceReceiver.getMethodAnnotation(method, annotationClass) != null) {
if (advice == null) {
advice = build(objectLocator, adviceClass);
}
methodAdviceReceiver.adviseMethod(method, advice);
}
}
}
}
use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.
the class DecorateByMarkerModule method doDecorate.
private static <T> T doDecorate(final String decoratorId, ServiceResources resources, T delegate, AspectDecorator aspectDecorator) {
Class<T> serviceInterface = resources.getServiceInterface();
AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, String.format("<Interceptor for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
builder.adviseAllMethods(new MethodAdvice() {
@Override
public void advise(MethodInvocation invocation) {
invocation.proceed();
Object result = invocation.getReturnValue();
invocation.setReturnValue(String.format("Decorated by %s[%s]", decoratorId, result));
}
});
return builder.build();
}
use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.
the class DecorateByMarkerModule2 method doDecorate.
private static <T> T doDecorate(final String decoratorId, ServiceResources resources, T delegate, AspectDecorator aspectDecorator) {
Class<T> serviceInterface = resources.getServiceInterface();
AspectInterceptorBuilder<T> builder = aspectDecorator.createBuilder(serviceInterface, delegate, String.format("<Interceptor for %s(%s)>", resources.getServiceId(), serviceInterface.getName()));
builder.adviseAllMethods(new MethodAdvice() {
@Override
public void advise(MethodInvocation invocation) {
invocation.proceed();
Object result = invocation.getReturnValue();
invocation.setReturnValue(String.format("Decorated by %s[%s]", decoratorId, result));
}
});
return builder.build();
}
use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.
the class AdviceDemoModule method adviseGreeter.
@Order("after:Logging")
public static void adviseGreeter(MethodAdviceReceiver receiver) {
MethodAdvice advice = new MethodAdvice() {
@Override
public void advise(MethodInvocation invocation) {
invocation.proceed();
String result = (String) invocation.getReturnValue();
if (result != null)
invocation.setReturnValue(result.toUpperCase());
}
};
for (Method m : receiver.getInterface().getMethods()) {
if (m.getReturnType().equals(String.class)) {
receiver.adviseMethod(m, advice);
}
}
}
use of org.apache.tapestry5.ioc.annotations.Advise in project tapestry-5 by apache.
the class DefaultModuleDefImpl method addAdvisorDef.
private void addAdvisorDef(Method method) {
Advise annotation = method.getAnnotation(Advise.class);
Class serviceInterface = annotation == null ? null : annotation.serviceInterface();
// TODO: methods just named "decorate"
String advisorId = annotation == null ? stripMethodPrefix(method, ADVISE_METHOD_NAME_PREFIX) : extractId(serviceInterface, annotation.id());
// TODO: Check for duplicates
Class returnType = method.getReturnType();
if (!returnType.equals(void.class))
throw new RuntimeException(String.format("Advise method %s does not return void.", toString(method)));
boolean found = false;
for (Class pt : method.getParameterTypes()) {
if (pt.equals(MethodAdviceReceiver.class)) {
found = true;
break;
}
}
if (!found)
throw new RuntimeException(String.format("Advise method %s must take a parameter of type %s.", toString(method), MethodAdviceReceiver.class.getName()));
Set<Class> markers = extractMarkers(method, Advise.class);
AdvisorDef def = new AdvisorDefImpl(method, extractPatterns(advisorId, method), extractConstraints(method), proxyFactory, advisorId, serviceInterface, markers);
advisorDefs.put(advisorId, def);
}
Aggregations