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);
}
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();
}
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);
}
}
}
};
}
Aggregations