Example usage for org.springframework.aop TargetSource getTargetClass

List of usage examples for org.springframework.aop TargetSource getTargetClass

Introduction

In this page you can find the example usage for org.springframework.aop TargetSource getTargetClass.

Prototype

@Override
@Nullable
Class<?> getTargetClass();

Source Link

Document

Return the type of targets returned by this TargetSource .

Usage

From source file:fr.mby.utils.spring.beans.factory.support.BasicProxywiredFactory.java

protected IManageableProxywired proxywire(final TargetSource targetSource) {
    final Class<?>[] proxtyInterfaces = new Class[] { targetSource.getTargetClass(),
            IManageableProxywired.class };
    final ProxyFactory proxyFactory = new ProxyFactory(proxtyInterfaces);
    proxyFactory.addAdvice(new InterfaceImplementationAdvice(IManageableProxywired.class,
            (IManageableProxywired) targetSource));
    proxyFactory.setTargetSource(targetSource);

    return (IManageableProxywired) proxyFactory.getProxy();
}

From source file:org.solmix.runtime.exchange.support.SpringAopClassHelper.java

@Override
protected Class<?> getRealClassInternal(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;//  w  w w  . j  a va2s  .  c om
        try {
            TargetSource targetSource = advised.getTargetSource();

            Object target = null;

            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // some scopes such as 'request' may not 
                // be active on the current thread yet
                return getRealClassFromClassInternal(targetSource.getTargetClass());
            }

            if (target == null) {
                Class<?> targetClass = AopUtils.getTargetClass(o);
                if (targetClass != null) {
                    return getRealClassFromClassInternal(targetClass);
                }
            } else {
                return getRealClassInternal(target);
            }
        } catch (Exception ex) {
            // ignore
        }

    } else if (ClassUtils.isCglibProxyClass(o.getClass())) {
        return getRealClassFromClassInternal(AopUtils.getTargetClass(o));
    }
    return o.getClass();
}

From source file:com.helpinput.spring.proxy.OptimizeTransactionProxyFactoryBean.java

public void afterPropertiesSet() {
    if (this.target == null) {
        throw new IllegalArgumentException("Property 'target' is required");
    }//from   ww  w .  j a  v a 2s  .  com
    if (this.target instanceof String) {
        throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
    }

    ClassLoader proxyClassLoader = target.getClass().getClassLoader();

    ProxyFactory proxyFactory = new ProxyFactory();

    if (this.preInterceptors != null) {
        for (Object interceptor : this.preInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    // Add the main interceptor (typically an Advisor).
    proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));

    if (this.postInterceptors != null) {
        for (Object interceptor : this.postInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    proxyFactory.copyFrom(this);

    TargetSource targetSource = createTargetSource(this.target);
    proxyFactory.setTargetSource(targetSource);

    if (this.proxyInterfaces != null) {
        proxyFactory.setInterfaces(this.proxyInterfaces);
    } else if (!isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        proxyFactory.setInterfaces(
                ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
    }

    /**
     * use this option to let proxyFactory user cglib to create proxy,otherwise in dynamic script ,this is no dynamic interface
     * ? cglib ??java??
     */
    proxyFactory.setOptimize(true);
    this.proxy = proxyFactory.getProxy(proxyClassLoader);
}

From source file:com.alibaba.dragoon.patrol.spring.DragoonMethodInterceptor.java

public MethodInfo getMethodInfo(MethodInvocation invocation, String paramter) {
    Object thisObject = invocation.getThis();
    Method method = invocation.getMethod();

    Class<?> clazz = null;// w  ww  . ja  v a 2  s.c o m

    try {
        if (thisObject == null) {
            clazz = method.getDeclaringClass();
        } else {
            // ?10?
            for (int i = 0; i < 10; ++i) {
                if (thisObject instanceof org.springframework.aop.framework.Advised) {
                    TargetSource targetSource = ((org.springframework.aop.framework.Advised) thisObject)
                            .getTargetSource();

                    if (targetSource == null) {
                        break;
                    }

                    Object target = targetSource.getTarget();
                    if (target != null) {
                        thisObject = target;
                    } else {
                        clazz = targetSource.getTargetClass();
                        break;
                    }
                } else {
                    break;
                }
            }

            if (clazz == null) {
                clazz = thisObject.getClass();

                if (clazz.getName().startsWith("$")) {
                    clazz = method.getDeclaringClass();
                }
            }
        }
    } catch (Exception ex) {
        LOG.error(ex.getMessage(), ex);
    }

    if (clazz == null) {
        clazz = method.getDeclaringClass();
    }

    return new MethodInfo(clazz, method, paramter);
}

From source file:org.iff.infra.util.spring.script.ScriptFactoryPostProcessor.java

/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy//  w  ww  . ja  v a  2 s  .c  o  m
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, Class<?>[] interfaces, boolean proxyTargetClass) {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTargetSource(ts);
    ClassLoader classLoader = this.beanClassLoader;

    if (interfaces == null) {
        interfaces = ClassUtils.getAllInterfacesForClass(ts.getTargetClass(), this.beanClassLoader);
    }
    proxyFactory.setInterfaces(interfaces);
    if (proxyTargetClass) {
        classLoader = null; // force use of Class.getClassLoader()
        proxyFactory.setProxyTargetClass(proxyTargetClass);
    }

    DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
    introduction.suppressInterface(TargetSource.class);
    proxyFactory.addAdvice(introduction);

    return proxyFactory.getProxy(classLoader);
}

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

/**
 * Create a new prototype instance of this class's created proxy object,
 * backed by an independent AdvisedSupport configuration.
 * @return a totally independent proxy, whose advice we may manipulate in isolation
 *///from w w w. ja v a  2s .  c o  m
private synchronized Object newPrototypeInstance() {
    // In the case of a prototype, we need to give the proxy
    // an independent instance of the configuration.
    // In this case, no proxy will have an instance of this object's configuration,
    // but will have an independent copy.
    if (logger.isTraceEnabled()) {
        logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
    }

    ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
    // The copy needs a fresh advisor chain, and a fresh TargetSource.
    TargetSource targetSource = freshTargetSource();
    copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
    if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        Class<?> targetClass = targetSource.getTargetClass();
        if (targetClass != null) {
            copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
        }
    }
    copy.setFrozen(this.freezeProxy);

    if (logger.isTraceEnabled()) {
        logger.trace("Using ProxyCreatorSupport copy: " + copy);
    }
    return getProxy(copy.createAopProxy());
}

From source file:org.springframework.batch.core.listener.AbstractListenerFactoryBean.java

/**
 * Convenience method to check whether the given object is or can be made
 * into a listener./*from ww  w . ja v  a 2 s. c  o m*/
 *
 * @param target the object to check
 * @return true if the delegate is an instance of any of the listener
 * interface, or contains the marker annotations
 */
public static boolean isListener(Object target, Class<?> listenerType, ListenerMetaData[] metaDataValues) {
    if (target == null) {
        return false;
    }
    if (listenerType.isInstance(target)) {
        return true;
    }
    if (target instanceof Advised) {
        TargetSource targetSource = ((Advised) target).getTargetSource();
        if (targetSource != null && targetSource.getTargetClass() != null
                && listenerType.isAssignableFrom(targetSource.getTargetClass())) {
            return true;
        }

        if (targetSource != null && targetSource.getTargetClass() != null
                && targetSource.getTargetClass().isInterface()) {
            logger.warn(String.format(
                    "%s is an interface.  The implementing class will not be queried for annotation based listener configurations.  If using @StepScope on a @Bean method, be sure to return the implementing class so listener annotations can be used.",
                    targetSource.getTargetClass().getName()));
        }
    }
    for (ListenerMetaData metaData : metaDataValues) {
        if (MethodInvokerUtils.getMethodInvokerByAnnotation(metaData.getAnnotation(), target) != null) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.scripting.support.ScriptFactoryPostProcessor.java

/**
 * Create a refreshable proxy for the given AOP TargetSource.
 * @param ts the refreshable TargetSource
 * @param interfaces the proxy interfaces (may be {@code null} to
 * indicate proxying of all interfaces implemented by the target class)
 * @return the generated proxy/*from  ww w  .  j a v  a2  s .c o  m*/
 * @see RefreshableScriptTargetSource
 */
protected Object createRefreshableProxy(TargetSource ts, @Nullable Class<?>[] interfaces,
        boolean proxyTargetClass) {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setTargetSource(ts);
    ClassLoader classLoader = this.beanClassLoader;

    if (interfaces != null) {
        proxyFactory.setInterfaces(interfaces);
    } else {
        Class<?> targetClass = ts.getTargetClass();
        if (targetClass != null) {
            proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.beanClassLoader));
        }
    }

    if (proxyTargetClass) {
        classLoader = null; // force use of Class.getClassLoader()
        proxyFactory.setProxyTargetClass(true);
    }

    DelegatingIntroductionInterceptor introduction = new DelegatingIntroductionInterceptor(ts);
    introduction.suppressInterface(TargetSource.class);
    proxyFactory.addAdvice(introduction);

    return proxyFactory.getProxy(classLoader);
}