Search in sources :

Example 1 with MethodAdviceReceiver

use of org.apache.tapestry5.ioc.MethodAdviceReceiver in project tapestry-5 by apache.

the class ServiceAdvisorImpl method advise.

/**
 * Invokes the configured method, passing the builder. The method will always take, as a parameter, a
 * MethodAdvisor.
 */
@Override
public void advise(MethodAdviceReceiver methodAdviceReceiver) {
    Map<Class, Object> resources = CollectionFactory.newMap(this.resourcesDefaults);
    resources.put(MethodAdviceReceiver.class, methodAdviceReceiver);
    InjectionResources injectionResources = new MapInjectionResources(resources);
    // By design, advise methods return void, so we know that the return value is null.
    invoke(injectionResources);
}
Also used : InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Example 2 with MethodAdviceReceiver

use of org.apache.tapestry5.ioc.MethodAdviceReceiver in project tapestry-5 by apache.

the class JpaTransactionAdvisorImpl method addTransactionCommitAdvice.

@Override
public void addTransactionCommitAdvice(MethodAdviceReceiver receiver) {
    for (final Method m : receiver.getInterface().getMethods()) {
        if (m.getAnnotation(CommitAfter.class) != null) {
            PersistenceContext annotation = receiver.getMethodAnnotation(m, PersistenceContext.class);
            receiver.adviseMethod(m, methodAdvices.get(annotation == null ? null : annotation.unitName()));
        }
    }
}
Also used : PersistenceContext(javax.persistence.PersistenceContext) Method(java.lang.reflect.Method) CommitAfter(org.apache.tapestry5.jpa.annotations.CommitAfter)

Example 3 with MethodAdviceReceiver

use of org.apache.tapestry5.ioc.MethodAdviceReceiver in project tapestry-5 by apache.

the class TapestryModule method componentReplacer.

@Advise(serviceInterface = ComponentInstantiatorSource.class)
public static void componentReplacer(MethodAdviceReceiver methodAdviceReceiver, final ComponentOverride componentReplacer) throws NoSuchMethodException, SecurityException {
    if (componentReplacer.hasReplacements()) {
        MethodAdvice advice = new MethodAdvice() {

            @Override
            public void advise(MethodInvocation invocation) {
                String className = (String) invocation.getParameter(0);
                final Class<?> replacement = componentReplacer.getReplacement(className);
                if (replacement != null) {
                    invocation.setParameter(0, replacement.getName());
                }
                invocation.proceed();
            }
        };
        methodAdviceReceiver.adviseMethod(ComponentInstantiatorSource.class.getMethod("getInstantiator", String.class), advice);
    }
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) Advise(org.apache.tapestry5.ioc.annotations.Advise)

Example 4 with MethodAdviceReceiver

use of org.apache.tapestry5.ioc.MethodAdviceReceiver in project tapestry-5 by apache.

the class LazyAdvisorImpl method addAdvice.

private void addAdvice(Method method, MethodAdviceReceiver receiver) {
    final Class thunkType = method.getReturnType();
    final String description = String.format("<%s Thunk for %s>", thunkType.getName(), InternalUtils.asString(method));
    MethodAdvice advice = new MethodAdvice() {

        /**
         * When the method is invoked, we don't immediately proceed. Instead, we return a thunk instance
         * that defers its behavior to the lazily invoked invocation.
         */
        @Override
        public void advise(final MethodInvocation invocation) {
            ObjectCreator deferred = new ObjectCreator() {

                @Override
                public Object createObject() {
                    invocation.proceed();
                    return invocation.getReturnValue();
                }
            };
            ObjectCreator cachingObjectCreator = new CachingObjectCreator(deferred);
            Object thunk = thunkCreator.createThunk(thunkType, cachingObjectCreator, description);
            invocation.setReturnValue(thunk);
        }
    };
    receiver.adviseMethod(method, advice);
}
Also used : MethodInvocation(org.apache.tapestry5.plastic.MethodInvocation) MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice) ObjectCreator(org.apache.tapestry5.commons.ObjectCreator)

Example 5 with MethodAdviceReceiver

use of org.apache.tapestry5.ioc.MethodAdviceReceiver in project tapestry-5 by apache.

the class LoggingAdvisorImpl method addLoggingAdvice.

@Override
public void addLoggingAdvice(Logger logger, MethodAdviceReceiver receiver) {
    MethodAdvice advice = new LoggingAdvice(logger, exceptionTracker);
    receiver.adviseAllMethods(advice);
}
Also used : MethodAdvice(org.apache.tapestry5.plastic.MethodAdvice)

Aggregations

MethodAdvice (org.apache.tapestry5.plastic.MethodAdvice)5 Method (java.lang.reflect.Method)4 MethodInvocation (org.apache.tapestry5.plastic.MethodInvocation)3 PersistenceContext (javax.persistence.PersistenceContext)1 ObjectCreator (org.apache.tapestry5.commons.ObjectCreator)1 Advise (org.apache.tapestry5.ioc.annotations.Advise)1 Operation (org.apache.tapestry5.ioc.annotations.Operation)1 Order (org.apache.tapestry5.ioc.annotations.Order)1 InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)1 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)1 CacheMethodAdvice (org.apache.tapestry5.jcache.internal.CacheMethodAdvice)1 CachePutMethodAdvice (org.apache.tapestry5.jcache.internal.CachePutMethodAdvice)1 CacheRemoveAllMethodAdvice (org.apache.tapestry5.jcache.internal.CacheRemoveAllMethodAdvice)1 CacheRemoveMethodAdvice (org.apache.tapestry5.jcache.internal.CacheRemoveMethodAdvice)1 CacheResultMethodAdvice (org.apache.tapestry5.jcache.internal.CacheResultMethodAdvice)1 CommitAfter (org.apache.tapestry5.jpa.annotations.CommitAfter)1