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

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

Introduction

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

Prototype

public static boolean isAopProxy(@Nullable Object object) 

Source Link

Document

Check whether the given object is a JDK dynamic proxy or a CGLIB proxy.

Usage

From source file:cn.guoyukun.spring.utils.AopProxyUtils.java

private static void removeAdvisor(Object proxy, Class<? extends Advice> adviceClass) {
    if (!AopUtils.isAopProxy(proxy)) {
        return;//from  w ww  .ja va  2 s . c o  m
    }
    ProxyFactory proxyFactory = null;
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if (AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if (advisors == null || advisors.length == 0) {
        return;
    }

    for (Advisor advisor : advisors) {
        if (adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            proxyFactory.removeAdvisor(advisor);
            break;
        }
    }
}

From source file:org.langera.freud.optional.spring.SpringBeanImpl.java

public boolean isAopProxy() {
    return AopUtils.isAopProxy(bean);
}

From source file:cz.fi.muni.fi.pa165.service.facade.SportActivityFacadeTest.java

@BeforeClass
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    if (AopUtils.isAopProxy(sportActivityFacade) && sportActivityFacade instanceof Advised) {
        sportActivityFacade = (SportActivityFacade) ((Advised) sportActivityFacade).getTargetSource()
                .getTarget();//from   w w  w.  j  av  a2 s  .  c  om
    }
    ReflectionTestUtils.setField(sportActivityFacade, "sportActivityService", sportActivityService);
}

From source file:cz.fi.muni.fi.pa165.service.facade.BurnedCaloriesFacadeTest.java

@BeforeClass
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    if (AopUtils.isAopProxy(burnedCaloriesFacade) && burnedCaloriesFacade instanceof Advised) {
        burnedCaloriesFacade = (BurnedCaloriesFacade) ((Advised) burnedCaloriesFacade).getTargetSource()
                .getTarget();//from  w  ww.ja  v a 2 s .c  o m
    }
    ReflectionTestUtils.setField(burnedCaloriesFacade, "burnedCaloriesService", burnedCaloriesService);
}

From source file:fr.putnami.pwt.plugin.spring.web.filter.DelegatingChainFilter.java

private void scanBean(Object bean, String name) {
    Class<?> implClass = bean.getClass();
    if (AopUtils.isAopProxy(bean)) {
        implClass = AopUtils.getTargetClass(bean);
    }/*from  ww w .  j a va2 s . c  o m*/
    if (Filter.class.isAssignableFrom(implClass)) {
        filters.add((Filter) bean);
    }
}

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  ww  .j  a v  a  2 s.  co  m
        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:cn.guoyukun.spring.utils.AopProxyUtils.java

private static boolean hasAdvice(Object proxy, Class<? extends Advice> adviceClass) {
    if (!AopUtils.isAopProxy(proxy)) {
        return false;
    }/*ww w.jav  a2s  .  com*/
    ProxyFactory proxyFactory = null;
    if (AopUtils.isJdkDynamicProxy(proxy)) {
        proxyFactory = findJdkDynamicProxyFactory(proxy);
    }
    if (AopUtils.isCglibProxy(proxy)) {
        proxyFactory = findCglibProxyFactory(proxy);
    }

    Advisor[] advisors = proxyFactory.getAdvisors();

    if (advisors == null || advisors.length == 0) {
        return false;
    }

    for (Advisor advisor : advisors) {
        if (adviceClass.isAssignableFrom(advisor.getAdvice().getClass())) {
            return true;
        }
    }
    return false;
}

From source file:fr.putnami.pwt.plugin.spring.rpc.server.service.CommandServiceImpl.java

private void scanBean(Object bean, String name) {
    Class<?> implClass = bean.getClass();
    if (AopUtils.isAopProxy(bean)) {
        implClass = AopUtils.getTargetClass(bean);
    }/*www  . j  av a 2s  . c  o m*/
    Service serviceAnnotation = AnnotationUtils.findAnnotation(implClass, Service.class);
    if (serviceAnnotation != null) {
        for (Class<?> inter : implClass.getInterfaces()) {
            this.injectService(inter, bean);
        }
    }
}

From source file:es.tid.fiware.rss.service.SettlementManagerTest.java

/**
 * // w  ww .j  a v a2  s .c o m
 * @return
 * @throws Exception
 */
private SettlementManager unwrapSettlementManager() throws Exception {
    if (AopUtils.isAopProxy(settlementManager) && settlementManager instanceof Advised) {
        Object target = ((Advised) settlementManager).getTargetSource().getTarget();
        return (SettlementManager) target;
    }
    return null;
}

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

@Test
public void metricsConfigTest() throws Throwable {
    // Initialize the context
    AnnotationConfigApplicationContext applicationContext = null;
    try {//w  ww. j a v a 2 s.co  m
        applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(MetricsConfig.class);
        applicationContext.refresh();

        // Assert that the custom registries were used
        assertSame(metricRegistry, applicationContext.getBean(MetricRegistry.class));
        assertSame(healthCheckRegistry, applicationContext.getBean(HealthCheckRegistry.class));

        // Verify that the configureReporters method was invoked
        assertThat(MetricsConfig.isConfigureReportersInvoked, is(true));

        // Assert that the bean has been proxied
        TestBean testBean = applicationContext.getBean(TestBean.class);
        assertNotNull(testBean);
        assertThat(AopUtils.isAopProxy(testBean), is(true));

        // Verify that the Gauge field's value is returned
        Gauge<Integer> fieldGauge = (Gauge<Integer>) forGaugeField(metricRegistry, TestBean.class,
                "intGaugeField");
        assertNotNull(fieldGauge);
        assertThat(fieldGauge.getValue(), is(5));

        // Verify that the Gauge method's value is returned
        Gauge<Integer> methodGauge = (Gauge<Integer>) forGaugeMethod(metricRegistry, TestBean.class,
                "intGaugeMethod");
        assertNotNull(methodGauge);
        assertThat(methodGauge.getValue(), is(6));

        // Verify that the Timer's counter is incremented on method invocation
        Timer timedMethodTimer = forTimedMethod(metricRegistry, TestBean.class, "timedMethod");
        assertNotNull(timedMethodTimer);
        assertThat(timedMethodTimer.getCount(), is(0L));
        testBean.timedMethod();
        assertThat(timedMethodTimer.getCount(), is(1L));

        // Verify that the Meter's counter is incremented on method invocation
        Meter meteredMethodMeter = forMeteredMethod(metricRegistry, TestBean.class, "meteredMethod");
        assertNotNull(meteredMethodMeter);
        assertThat(meteredMethodMeter.getCount(), is(0L));
        testBean.meteredMethod();
        assertThat(meteredMethodMeter.getCount(), is(1L));

        // Verify that the Meter's counter is incremented on method invocation
        Meter exceptionMeteredMethodMeter = forExceptionMeteredMethod(metricRegistry, TestBean.class,
                "exceptionMeteredMethod");
        assertNotNull(exceptionMeteredMethodMeter);
        assertThat(exceptionMeteredMethodMeter.getCount(), is(0L));
        try {
            testBean.exceptionMeteredMethod();
        } catch (Throwable t) {
        }
        assertThat(exceptionMeteredMethodMeter.getCount(), is(1L));
    } finally {
        if (applicationContext != null) {
            applicationContext.close();
        }
    }
}