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

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

Introduction

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

Prototype

public static Class<?> getTargetClass(Object candidate) 

Source Link

Document

Determine the target class of the given bean instance which might be an AOP proxy.

Usage

From source file:com.ryantenney.metrics.spring.AdvisingBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof AopInfrastructureBean) {
        return bean;
    }//  ww w  .  jav  a 2 s.c  om

    final Class<?> targetClass = AopUtils.getTargetClass(bean);

    if (AopUtils.canApply(pointcut, targetClass)) {
        final Advice advice = adviceFactory.getAdvice(bean, targetClass);
        final Advisor advisor = new DefaultPointcutAdvisor(pointcut, advice);

        if (bean instanceof Advised) {
            LOG.debug("Bean {} is already proxied, adding Advisor to existing proxy", beanName);

            ((Advised) bean).addAdvisor(0, advisor);

            return bean;
        } else {
            LOG.debug("Proxying bean {} of type {}", beanName, targetClass.getCanonicalName());

            final ProxyFactory proxyFactory = new ProxyFactory(bean);
            if (proxyConfig != null) {
                proxyFactory.copyFrom(proxyConfig);
            }
            proxyFactory.addAdvisor(advisor);

            final Object proxy = proxyFactory.getProxy(this.beanClassLoader);

            return proxy;
        }
    }

    return bean;
}

From source file:co.paralleluniverse.common.spring.MBeanNamingStrategy.java

@Override
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
    Class managedClass = AopUtils.getTargetClass(managedBean);
    ManagedResource mr = this.attributeSource.getManagedResource(managedClass);

    // Check that an object name has been specified.
    if (mr != null && StringUtils.hasText(mr.getObjectName())) {
        return ObjectName.getInstance(mr.getObjectName());
    } else {/*from   w  w  w  .  ja v  a  2 s  .  c  o  m*/
        String domain = this.defaultDomain;
        if (domain == null)
            domain = ClassUtils.getPackageName(managedClass);
        return ObjectName.getInstance(domain + ":type=components,name=" + beanKey);
    }
}

From source file:org.pshow.ecm.security.MethodAuditPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }/*from   ww  w . j  av  a2  s.c  o  m*/
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.advisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.advisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from
            // ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.advisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }
}

From source file:prospring3.ch5.beanpostprocessor.DeassociateBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }// w  ww  .j a  va 2  s.  com
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    if (AopUtils.canApply(this.deassociatePointcutAdvisor, targetClass)) {
        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(this.deassociatePointcutAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(this.deassociatePointcutAdvisor);
            return proxyFactory.getProxy(this.classLoader);
        }
    } else {
        // This is not a repository.
        return bean;
    }

}

From source file:org.springjutsu.validation.context.ValidationContextHandlerContainer.java

/**
 * Finds the annotated context handlers by searching the bean factory.
 * Also registers XML-configured context handlers.
 * @throws BeansException on a bad.// w ww .  jav  a  2  s. c  om
 */
@PostConstruct
public void registerContextHandlers() throws BeansException {
    if (addDefaultContextHandlers) {
        addDefaultContextHandlers();
    }
    Map<String, Object> contextHandlerBeans = ((ListableBeanFactory) beanFactory)
            .getBeansWithAnnotation(ConfiguredContextHandler.class);

    for (String springName : contextHandlerBeans.keySet()) {
        ValidationContextHandler handler = (ValidationContextHandler) contextHandlerBeans.get(springName);
        String contextType = AnnotationUtils
                .findAnnotation(AopUtils.getTargetClass(handler), ConfiguredContextHandler.class).type();
        setCustomContextHandler(contextType, handler);
    }
    if (beanRegistrants != null) {
        for (KeyedBeanRegistrant registrant : beanRegistrants) {
            setCustomContextHandler(registrant.getKey(),
                    (ValidationContextHandler) beanFactory.getBean(registrant.getBeanName()));
        }
    }
}

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.  ja  va2 s.com
        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:net.bull.javamelody.MonitoringSpringInterceptor.java

private static String getClassPart(MethodInvocation invocation) {
    // si guice et pas Spring, alors remplacer AopUtils.getTargetClass() par getMethod().getDeclaringClass()
    // http://ninomartinez.wordpress.com/2010/05/14/guice-caching-interceptors/
    // (faire exemple avec un interceptor static)
    final Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
    final MonitoredWithSpring classAnnotation = targetClass.getAnnotation(MonitoredWithSpring.class);
    if (classAnnotation == null || classAnnotation.name() == null || classAnnotation.name().length() == 0) {
        final Class<?> declaringClass = invocation.getMethod().getDeclaringClass();
        final MonitoredWithSpring declaringClassAnnotation = declaringClass
                .getAnnotation(MonitoredWithSpring.class);
        if (declaringClassAnnotation == null || declaringClassAnnotation.name() == null
                || declaringClassAnnotation.name().length() == 0) {
            return targetClass.getSimpleName();
        }//from ww w  . j a  va  2  s. c o m
        return declaringClassAnnotation.name();
    }
    return classAnnotation.name();
}

From source file:dk.clanie.actor.ActorAnnotationBeanPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof AopInfrastructureBean) {
        // Ignore AOP infrastructure such as scoped proxies.
        return bean;
    }/*  w  w w  . ja v  a 2s .  co  m*/
    Class<?> targetClass = AopUtils.getTargetClass(bean);
    Actor annotation = AnnotationUtils.findAnnotation(targetClass, Actor.class);
    if (annotation != null) {
        //      if (AopUtils.canApply(this.asyncAnnotationAdvisor, targetClass)) {

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setMaxPoolSize(1);
        executor.setDaemon(true);
        String threadNamePrefix = beanName + ",";
        executor.setThreadNamePrefix(threadNamePrefix);
        executor.initialize();

        ActorAnnotationAdvisor actorAnnotationAdvisor = new ActorAnnotationAdvisor(executor);

        if (bean instanceof Advised) {
            ((Advised) bean).addAdvisor(0, actorAnnotationAdvisor);
            return bean;
        } else {
            ProxyFactory proxyFactory = new ProxyFactory(bean);
            // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig.
            proxyFactory.copyFrom(this);
            proxyFactory.addAdvisor(actorAnnotationAdvisor);
            return proxyFactory.getProxy(this.beanClassLoader);
        }
    } else {
        // No async proxy needed.
        return bean;
    }
}

From source file:guru.qas.martini.annotation.StepsAnnotationProcessor.java

@Override
public Object postProcessAfterInitialization(@Nonnull Object bean, String beanName) throws BeansException {
    try {//from  w ww .j a  va2s . c om
        Class<?> wrapped = AopUtils.getTargetClass(bean);
        if (!isSpring(wrapped)) {
            Class<?> declaring = AnnotationUtils.findAnnotationDeclaringClass(Steps.class, wrapped);
            if (null != declaring) {
                processStepsBean(beanName, wrapped);
            }
        }
        return bean;
    } catch (Exception e) {
        throw new FatalBeanException("unable to processAnnotationContainer @Steps beans", e);
    }
}