Search in sources :

Example 36 with Advised

use of org.springframework.aop.framework.Advised in project spring-security by spring-projects.

the class MethodInvocationUtils method create.

// ~ Methods
// ========================================================================================================
/**
	 * Generates a <code>MethodInvocation</code> for specified <code>methodName</code> on
	 * the passed object, using the <code>args</code> to locate the method.
	 *
	 * @param object the object that will be used to find the relevant <code>Method</code>
	 * @param methodName the name of the method to find
	 * @param args arguments that are required as part of the method signature (can be
	 * empty)
	 *
	 * @return a <code>MethodInvocation</code>, or <code>null</code> if there was a
	 * problem
	 */
public static MethodInvocation create(Object object, String methodName, Object... args) {
    Assert.notNull(object, "Object required");
    Class<?>[] classArgs = null;
    if (args != null) {
        classArgs = new Class<?>[args.length];
        for (int i = 0; i < args.length; i++) {
            classArgs[i] = args[i].getClass();
        }
    }
    // Determine the type that declares the requested method, taking into account
    // proxies
    Class<?> target = AopUtils.getTargetClass(object);
    if (object instanceof Advised) {
        Advised a = (Advised) object;
        if (!a.isProxyTargetClass()) {
            Class<?>[] possibleInterfaces = a.getProxiedInterfaces();
            for (Class<?> possibleInterface : possibleInterfaces) {
                try {
                    possibleInterface.getMethod(methodName, classArgs);
                    // to get here means no exception happened
                    target = possibleInterface;
                    break;
                } catch (Exception ignored) {
                // try the next one
                }
            }
        }
    }
    return createFromClass(object, target, methodName, classArgs, args);
}
Also used : Advised(org.springframework.aop.framework.Advised)

Example 37 with Advised

use of org.springframework.aop.framework.Advised in project cxf by apache.

the class SpringAopClassHelper method getRealClassInternal.

protected Class<?> getRealClassInternal(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;
        try {
            TargetSource targetSource = advised.getTargetSource();
            Object target = null;
            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // 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();
}
Also used : TargetSource(org.springframework.aop.TargetSource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Advised(org.springframework.aop.framework.Advised) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 38 with Advised

use of org.springframework.aop.framework.Advised in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateTests method setUp.

@Before
public void setUp() {
    MongoClient client = new MongoClient();
    MongoDbFactory factory = new SimpleMongoDbFactory(client, "session-bound-mongo-template-tests") {

        @Override
        public MongoDatabase getDb() throws DataAccessException {
            MongoDatabase spiedDatabse = Mockito.spy(super.getDb());
            spiedDatabases.add(spiedDatabse);
            return spiedDatabse;
        }
    };
    session = client.startSession(ClientSessionOptions.builder().build());
    this.template = new MongoTemplate(factory);
    this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {

        @Override
        protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
            injectCollectionSpy(collection);
            return super.prepareCollection(collection);
        }

        @SuppressWarnings({ "ConstantConditions", "unchecked" })
        private void injectCollectionSpy(MongoCollection<Document> collection) {
            InvocationHandler handler = Proxy.getInvocationHandler(collection);
            Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
            for (Advisor advisor : advised.getAdvisors()) {
                Advice advice = advisor.getAdvice();
                if (advice instanceof SessionAwareMethodInterceptor) {
                    MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
                    spiedCollections.add(spiedCollection);
                    ReflectionTestUtils.setField(advice, "target", spiedCollection);
                }
            }
        }
    };
}
Also used : SessionAwareMethodInterceptor(org.springframework.data.mongodb.SessionAwareMethodInterceptor) MongoDbFactory(org.springframework.data.mongodb.MongoDbFactory) Advisor(org.springframework.aop.Advisor) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Document(org.bson.Document) InvocationHandler(java.lang.reflect.InvocationHandler) MongoClient(com.mongodb.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) Advised(org.springframework.aop.framework.Advised) Advice(org.aopalliance.aop.Advice) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) MongoDatabase(com.mongodb.client.MongoDatabase) Before(org.junit.Before)

Aggregations

Advised (org.springframework.aop.framework.Advised)38 Test (org.junit.Test)22 ITestBean (org.springframework.tests.sample.beans.ITestBean)15 Advisor (org.springframework.aop.Advisor)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)10 TestBean (org.springframework.tests.sample.beans.TestBean)4 JoinPoint (org.aspectj.lang.JoinPoint)3 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)3 SyntheticInstantiationAdvisor (org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Person (org.springframework.tests.sample.beans.Person)3 StopWatch (org.springframework.util.StopWatch)3 Advice (org.aopalliance.aop.Advice)2 PrototypeTargetSource (org.springframework.aop.target.PrototypeTargetSource)2 BeanCreationException (org.springframework.beans.factory.BeanCreationException)2 BeanFactory (org.springframework.beans.factory.BeanFactory)2 AnotherTestEvent (org.springframework.context.event.test.AnotherTestEvent)2 TestEvent (org.springframework.context.event.test.TestEvent)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2