Example usage for org.springframework.aop.support AopUtils invokeJoinpointUsingReflection

List of usage examples for org.springframework.aop.support AopUtils invokeJoinpointUsingReflection

Introduction

In this page you can find the example usage for org.springframework.aop.support AopUtils invokeJoinpointUsingReflection.

Prototype

@Nullable
public static Object invokeJoinpointUsingReflection(@Nullable Object target, Method method, Object[] args)
        throws Throwable 

Source Link

Document

Invoke the given target via reflection, as part of an AOP method invocation.

Usage

From source file:org.jdal.aop.DelegateFactoryIntroductionInterceptor.java

public Object invoke(MethodInvocation mi) throws Throwable {
    if (isMethodOnIntroducedInterface(mi)) {
        Object delegate = getIntroductionDelegateFor(mi.getThis());
        Object retVal = AopUtils.invokeJoinpointUsingReflection(delegate, mi.getMethod(), mi.getArguments());

        if (retVal == delegate && mi instanceof ProxyMethodInvocation) {
            retVal = ((ProxyMethodInvocation) mi).getProxy();
        }// ww  w  .  ja va  2 s  .c  om
        return retVal;
    }

    return doProceed(mi);
}

From source file:hotbeans.support.HotBeanProxyFactory.java

/**
 * Called to invoke a method on the target bean.
 *///from   w  ww  . j a va 2s . co  m
public Object invoke(final MethodInvocation methodInvocation) throws Throwable {
    if (logger.isDebugEnabled())
        logger.debug("Preparing to invoke method " + methodInvocation.getMethod().getName() + " on bean '"
                + this.beanName + "' in module " + this.currentModule + ". Current bean: " + this.currentBean
                + ".");

    // Validate reference to module/bean
    this.repository.validateHotBeanProxyFactory(this);

    if (logger.isDebugEnabled())
        logger.debug("Invoking method " + methodInvocation.getMethod().getName() + " on bean '" + this.beanName
                + "' in module " + this.currentModule + ". Current bean: " + this.currentBean + ".");

    if (this.currentBean != null) {
        try {
            this.currentModule.incrementUsageCount();

            return AopUtils.invokeJoinpointUsingReflection(this.currentBean, methodInvocation.getMethod(),
                    methodInvocation.getArguments());
        } finally {
            this.currentModule.decrementUsageCount();
        }
    } else {
        if (this.currentModule == null)
            throw new ModuleNotFoundException(this.moduleName,
                    "Unable to find module '" + this.moduleName + "'!");
        else
            throw new BeanNotFoundException(this.moduleName, this.beanName,
                    "Unable to find bean '" + this.beanName + "' in module '" + this.moduleName + "'!");
    }
}

From source file:com.mystudy.source.spring.aop.JdkDynamicAopProxy.java

/**
 * /*from ww  w.j av  a 2 s  .  c o  m*/
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodInvocation invocation;
    Object oldProxy = null;
    boolean setProxyContext = false;

    TargetSource targetSource = this.advised.targetSource;
    Class<?> targetClass = null;
    Object target = null;

    try {//eqal
        if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
            // The target does not implement the equals(Object) method itself.
            return equals(args[0]);
        }
        //hashcode
        if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
            // The target does not implement the hashCode() method itself.
            return hashCode();
        }

        if (!this.advised.opaque && method.getDeclaringClass().isInterface()
                && method.getDeclaringClass().isAssignableFrom(Advised.class)) {
            // Service invocations on ProxyConfig with the proxy config...
            return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
        }

        Object retVal;

        if (this.advised.exposeProxy) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            setProxyContext = true;
        }

        // May be null. Get as late as possible to minimize the time we "own" the target,
        // in case it comes from a pool.
        target = targetSource.getTarget();
        if (target != null) {
            targetClass = target.getClass();
        }

        // Get the interception chain for this method.
        List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

        // Check whether we have any advice. If we don't, we can fallback on direct
        // reflective invocation of the target, and avoid creating a MethodInvocation.
        if (chain.isEmpty()) {
            // We can skip creating a MethodInvocation: just invoke the target directly
            // Note that the final invoker must be an InvokerInterceptor so we know it does
            // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
            retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
        } else {
            //chain????
            // We need to create a method invocation...
            invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
            // Proceed to the joinpoint through the interceptor chain.
            retVal = invocation.proceed();
        }

        // Massage return value if necessary.
        Class<?> returnType = method.getReturnType();
        if (retVal != null && retVal == target && returnType.isInstance(proxy)
                && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
            // Special case: it returned "this" and the return type of the method
            // is type-compatible. Note that we can't help if the target sets
            // a reference to itself in another returned object.
            retVal = proxy;
        } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
            throw new AopInvocationException(
                    "Null return value from advice does not match primitive return type for: " + method);
        }
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            // Must have come from TargetSource.
            targetSource.releaseTarget(target);
        }
        if (setProxyContext) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}

From source file:org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.java

/**
 * Actual invocation - the class is being executed on a different object
 * then the one exposed in the invocation object.
 * //from w w  w  .ja v a2 s  .  c  o m
 * @param service
 * @param invocation
 * @return
 * @throws Throwable
 */
protected Object doInvoke(Object service, MethodInvocation invocation) throws Throwable {
    return AopUtils.invokeJoinpointUsingReflection(service, invocation.getMethod(), invocation.getArguments());
}

From source file:org.impalaframework.spring.service.proxy.ServiceEndpointInterceptor.java

public Object invoke(MethodInvocation invocation) throws Throwable {

    final Method method = invocation.getMethod();
    final Object[] arguments = invocation.getArguments();

    final boolean setCCCL = optionsHelper.isSetContextClassLoader();

    ServiceRegistryEntry serviceReference = targetSource.getServiceRegistryReference();

    int retriesUsed = 0;
    while (serviceReference == null && retriesUsed < optionsHelper.getRetryCount()) {
        try {//from w  w w . j av  a 2  s  . c o  m
            Thread.sleep(optionsHelper.getRetryInterval());
        } catch (InterruptedException e) {
        }
        serviceReference = targetSource.getServiceRegistryReference();
        retriesUsed++;
    }

    if (serviceReference != null) {

        final Thread currentThread;
        final ClassLoader existingClassLoader;
        if (setCCCL) {
            currentThread = Thread.currentThread();
            existingClassLoader = currentThread.getContextClassLoader();
        } else {
            currentThread = null;
            existingClassLoader = null;
        }

        try {
            if (setCCCL) {
                currentThread.setContextClassLoader(serviceReference.getBeanClassLoader());
            }

            return AopUtils.invokeJoinpointUsingReflection(
                    serviceReference.getServiceBeanReference().getService(), method, arguments);

        } finally {
            //reset the previous class loader
            if (setCCCL) {
                Thread.currentThread().setContextClassLoader(existingClassLoader);
            }
        }
    } else {

        if (optionsHelper.isLogWarningNoService()) {
            log.warn("************************************************************************* ");
            log.warn("No service available for bean " + beanName + ". Proceeding with stub implementation");
            log.warn("************************************************************************* ");
        }

        if (optionsHelper.isProceedWithNoService()) {
            return invokeDummy(invocation);
        } else {
            throw new NoServiceException("No service available for bean " + beanName);
        }
    }
}

From source file:org.springframework.aop.framework.JdkDynamicAopProxy.java

/**
 * Implementation of {@code InvocationHandler.invoke}.
 * <p>Callers will see exactly the exception thrown by the target,
 * unless a hook method throws an exception.
 *///from w  w w  .  j a  v a 2s  .  co  m
@Override
@Nullable
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    MethodInvocation invocation;
    Object oldProxy = null;
    boolean setProxyContext = false;

    TargetSource targetSource = this.advised.targetSource;
    Object target = null;

    try {
        if (!this.equalsDefined && AopUtils.isEqualsMethod(method)) {
            // The target does not implement the equals(Object) method itself.
            return equals(args[0]);
        } else if (!this.hashCodeDefined && AopUtils.isHashCodeMethod(method)) {
            // The target does not implement the hashCode() method itself.
            return hashCode();
        } else if (method.getDeclaringClass() == DecoratingProxy.class) {
            // There is only getDecoratedClass() declared -> dispatch to proxy config.
            return AopProxyUtils.ultimateTargetClass(this.advised);
        } else if (!this.advised.opaque && method.getDeclaringClass().isInterface()
                && method.getDeclaringClass().isAssignableFrom(Advised.class)) {
            // Service invocations on ProxyConfig with the proxy config...
            return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
        }

        Object retVal;

        if (this.advised.exposeProxy) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            setProxyContext = true;
        }

        // Get as late as possible to minimize the time we "own" the target,
        // in case it comes from a pool.
        target = targetSource.getTarget();
        Class<?> targetClass = (target != null ? target.getClass() : null);

        // Get the interception chain for this method.
        List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);

        // Check whether we have any advice. If we don't, we can fallback on direct
        // reflective invocation of the target, and avoid creating a MethodInvocation.
        if (chain.isEmpty()) {
            // We can skip creating a MethodInvocation: just invoke the target directly
            // Note that the final invoker must be an InvokerInterceptor so we know it does
            // nothing but a reflective operation on the target, and no hot swapping or fancy proxying.
            Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
            retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
        } else {
            // We need to create a method invocation...
            invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);
            // Proceed to the joinpoint through the interceptor chain.
            retVal = invocation.proceed();
        }

        // Massage return value if necessary.
        Class<?> returnType = method.getReturnType();
        if (retVal != null && retVal == target && returnType != Object.class && returnType.isInstance(proxy)
                && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
            // Special case: it returned "this" and the return type of the method
            // is type-compatible. Note that we can't help if the target sets
            // a reference to itself in another returned object.
            retVal = proxy;
        } else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
            throw new AopInvocationException(
                    "Null return value from advice does not match primitive return type for: " + method);
        }
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            // Must have come from TargetSource.
            targetSource.releaseTarget(target);
        }
        if (setProxyContext) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}

From source file:org.springframework.aop.framework.OptimizedJdkDynamicAopProxy.java

/**
 * Implementation of InvocationHandler.invoke.
 * Callers will see exactly the exception thrown by the target, unless a hook
 * method throws an exception.//  w w w.  ja  v  a  2 s . com
 */
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

    Class declaringClass = method.getDeclaringClass();

    // Try special rules for equals() method and implementation of the
    // Advised AOP configuration interface
    if (declaringClass == Object.class && "equals".equals(method.getName())) {
        // What if equals throws exception!?

        // This class implements the equals() method itself
        return new Boolean(equals(args[0]));
    } else if (Advised.class == declaringClass) {
        // Service invocations on ProxyConfig with the proxy config
        return method.invoke(this.advised, args);
    }

    Object retVal = null;

    // Get the interception chain for this method
    List chain = this.advised.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(this.advised,
            method, this.targetClass);

    // Check whether we have any advice. If we don't, we can fallback on
    // direct reflective invocation of the target, and avoid creating a MethodInvocation
    if (chain.isEmpty()) {
        // We can skip creating a MethodInvocation: just invoke the target directly
        // Note that the final invoker must be an InvokerInterceptor so we know it does
        // nothing but a reflective operation on the target, and no hot swapping or fancy proxying
        retVal = AopUtils.invokeJoinpointUsingReflection(target, method, args);
    } else {
        // We need to create a method invocation...
        //invocation = advised.getMethodInvocationFactory().getMethodInvocation(proxy, method, targetClass, target, args, chain, advised);
        MethodInvocation invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass,
                chain);

        // Proceed to the joinpoint through the interceptor chain
        retVal = invocation.proceed();
    }

    // Massage return value if necessary
    if (retVal != null && retVal == target) {
        // Special case: it returned "this"
        // Note that we can't help if the target sets
        // a reference to itself in another returned object
        retVal = proxy;
    }
    return retVal;
}