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:com.p5solutions.core.utils.ReflectionUtility.java

/**
 * Invoke./* w w  w .j av a  2 s .com*/
 * 
 * @param <O>
 *          the generic type
 * @param <I>
 *          the generic type
 * @param object
 *          the object
 * @param method
 *          the method
 * @param ignoreAccess
 *          the ignore access
 * @param args
 *          the args
 * @return the object
 */
@SuppressWarnings("unchecked")
public static <O, I> O invokeWithAccessWithArguments(I object, Method method, boolean ignoreAccess,
        Object... args) {
    if (object == null) {
        throw new RuntimeException("Object cannot be null when invoking its methods");
    }

    Object returnObject = null;
    Class<?> clazz = object.getClass();

    try {

        /* call the method */
        if (method != null) {
            if (AopUtils.isAopProxy(object)) {
                InvocationHandler handler = Proxy.getInvocationHandler(object);
                returnObject = handler.invoke(object, method, args);
            } else {
                boolean isAccessible = method.isAccessible();
                try {
                    if (!isAccessible && ignoreAccess) {
                        method.setAccessible(true);
                    }
                    returnObject = method.invoke(object, args);
                } finally {
                    if (ignoreAccess) {
                        method.setAccessible(isAccessible);
                    }
                }
            }
        } else {
            throw new RuntimeException("Method cannot be null");
        }
    } catch (Throwable e) {
        // get the target class if its a proxy
        clazz = AopUtils.getTargetClass(object);

        /* Logger that is available to subclasses */
        Log logger = LogFactory.getLog(clazz);
        // logger.error("Unable to invoke method " + method.getName() + " within "
        // + clazz.getCanonicalName() + "\n"
        // + getStackTrace(e));

        throw new RuntimeException(e);
    }

    return (O) returnObject;
}

From source file:org.kuali.rice.krad.data.provider.impl.ProviderRegistryImpl.java

/**
 * Returns the object being proxied, otherwise the given object is returned.
 *
 * @param bean The proxy to get the underlying object.
 * @return object being proxied, otherwise the given object is returned.
 * @throws Exception if errors while getting the underlying object.
 *///from   w  ww . j a  v  a  2s  .c  o m
private Object unwrapProxy(Object bean) throws Exception {

    /*
     * If the given object is a proxy, set the return value as the object
     * being proxied, otherwise return the given object.
     */
    if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {

        Advised advised = (Advised) bean;

        bean = advised.getTargetSource().getTarget();
    }

    return bean;
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAreApplied() {
    ClassPathXmlApplicationContext bf = newContext("aspects.xml");
    ITestBean tb = (ITestBean) bf.getBean("adrian");
    assertEquals(68, tb.getAge());//  w ww  . ja v a2s  .  com
    MethodInvokingFactoryBean factoryBean = (MethodInvokingFactoryBean) bf.getBean("&factoryBean");
    assertTrue(AopUtils.isAopProxy(factoryBean.getTargetObject()));
    assertEquals(68, ((ITestBean) factoryBean.getTargetObject()).getAge());
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAndAdvisorNotAppliedToPrototypeIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);/*from  w ww  .j  a v  a2 s. c o m*/
    ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
    StopWatch sw = new StopWatch();
    sw.start("Prototype Creation");
    for (int i = 0; i < 100000; i++) {
        INestedTestBean shouldNotBeWeaved = (INestedTestBean) ac.getBean("i21");
        if (i < 10) {
            assertFalse(AopUtils.isAopProxy(shouldNotBeWeaved));
        }
    }
    sw.stop();

    // What's a reasonable expectation for _any_ server or developer machine load?
    // 3 seconds?
    assertStopWatchTimeLimit(sw, 6000);
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

protected void doTestAspectsAndAdvisorAreApplied(ApplicationContext ac, ITestBean shouldBeWeaved) {
    TestBeanAdvisor tba = (TestBeanAdvisor) ac.getBean("advisor");

    MultiplyReturnValue mrv = (MultiplyReturnValue) ac.getBean("aspect");
    assertEquals(3, mrv.getMultiple());// w  w w .  jav  a 2 s.  c om

    tba.count = 0;
    mrv.invocations = 0;

    assertTrue("Autoproxying must apply from @AspectJ aspect", AopUtils.isAopProxy(shouldBeWeaved));
    assertEquals("Adrian", shouldBeWeaved.getName());
    assertEquals(0, mrv.invocations);
    assertEquals(34 * mrv.getMultiple(), shouldBeWeaved.getAge());
    assertEquals("Spring advisor must be invoked", 2, tba.count);
    assertEquals("Must be able to hold state in aspect", 1, mrv.invocations);
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testPerThisAspect() {
    ClassPathXmlApplicationContext bf = newContext("perthis.xml");

    ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian1));

    assertEquals(0, adrian1.getAge());//  w w w.  j  a v a 2 s. c  o  m
    assertEquals(1, adrian1.getAge());

    ITestBean adrian2 = (ITestBean) bf.getBean("adrian");
    assertNotSame(adrian1, adrian2);
    assertTrue(AopUtils.isAopProxy(adrian1));
    assertEquals(0, adrian2.getAge());
    assertEquals(1, adrian2.getAge());
    assertEquals(2, adrian2.getAge());
    assertEquals(3, adrian2.getAge());
    assertEquals(2, adrian1.getAge());
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
    ClassPathXmlApplicationContext bf = newContext("pertarget.xml");

    ITestBean adrian1 = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian1));

    // Does not trigger advice or count
    int explicitlySetAge = 25;
    adrian1.setAge(explicitlySetAge);// w w  w . j  a  va  2s. c om

    assertEquals("Setter does not initiate advice", explicitlySetAge, adrian1.getAge());
    // Fire aspect

    AspectMetadata am = new AspectMetadata(PerTargetAspect.class, "someBean");
    assertTrue(
            am.getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));

    adrian1.getSpouse();

    assertEquals("Advice has now been instantiated", 0, adrian1.getAge());
    adrian1.setAge(11);
    assertEquals("Any int setter increments", 2, adrian1.getAge());
    adrian1.setName("Adrian");
    //assertEquals("Any other setter does not increment", 2, adrian1.getAge());

    ITestBean adrian2 = (ITestBean) bf.getBean("adrian");
    assertNotSame(adrian1, adrian2);
    assertTrue(AopUtils.isAopProxy(adrian1));
    assertEquals(34, adrian2.getAge());
    adrian2.getSpouse();
    assertEquals("Aspect now fired", 0, adrian2.getAge());
    assertEquals(1, adrian2.getAge());
    assertEquals(2, adrian2.getAge());
    assertEquals(3, adrian1.getAge());
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testIncludeMechanism() {
    ClassPathXmlApplicationContext bf = newContext("usesInclude.xml");

    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    assertEquals(68, adrian.getAge());//from ww  w .ja  v a  2  s.  c o m
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

private void testPrototype(ITestBean adrian1, int start) {
    assertTrue(AopUtils.isAopProxy(adrian1));
    //TwoAdviceAspect twoAdviceAspect = (TwoAdviceAspect) bf.getBean(TwoAdviceAspect.class.getName());
    adrian1.setName("");
    assertEquals(start++, adrian1.getAge());
    int newAge = 32;
    adrian1.setAge(newAge);/*from   w w w  .j a  v a  2 s.c o m*/
    assertEquals(start++, adrian1.getAge());
    adrian1.setAge(0);
    assertEquals(start++, adrian1.getAge());
}

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testWithAbstractFactoryBeanAreApplied() {
    ClassPathXmlApplicationContext bf = newContext("aspectsWithAbstractBean.xml");

    ITestBean adrian = (ITestBean) bf.getBean("adrian");
    assertTrue(AopUtils.isAopProxy(adrian));
    assertEquals(68, adrian.getAge());// w  w  w .j av a2s  .  c  om
}