use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class NullPrimitiveTests method testNullPrimitiveWithJdkProxy.
@Test
public void testNullPrimitiveWithJdkProxy() {
class SimpleFoo implements Foo {
@Override
public int getValue() {
return 100;
}
}
SimpleFoo target = new SimpleFoo();
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null;
}
});
Foo foo = (Foo) factory.getProxy();
thrown.expect(AopInvocationException.class);
thrown.expectMessage("Foo.getValue()");
assertEquals(0, foo.getValue());
}
use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class NullPrimitiveTests method testNullPrimitiveWithCglibProxy.
@Test
public void testNullPrimitiveWithCglibProxy() {
Bar target = new Bar();
ProxyFactory factory = new ProxyFactory(target);
factory.addAdvice(new MethodInterceptor() {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return null;
}
});
Bar bar = (Bar) factory.getProxy();
thrown.expect(AopInvocationException.class);
thrown.expectMessage("Bar.getValue()");
assertEquals(0, bar.getValue());
}
use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class HttpInvokerTests method httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes.
@Test
public void httpInvokerProxyFactoryBeanAndServiceExporterWithInvocationAttributes() throws Exception {
TestBean target = new TestBean("myname", 99);
final HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
exporter.setServiceInterface(ITestBean.class);
exporter.setService(target);
exporter.setRemoteInvocationExecutor(new DefaultRemoteInvocationExecutor() {
@Override
public Object invoke(RemoteInvocation invocation, Object targetObject) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
assertNotNull(invocation.getAttributes());
assertEquals(1, invocation.getAttributes().size());
assertEquals("myValue", invocation.getAttributes().get("myKey"));
assertEquals("myValue", invocation.getAttribute("myKey"));
return super.invoke(invocation, targetObject);
}
});
exporter.afterPropertiesSet();
HttpInvokerProxyFactoryBean pfb = new HttpInvokerProxyFactoryBean();
pfb.setServiceInterface(ITestBean.class);
pfb.setServiceUrl("http://myurl");
pfb.setRemoteInvocationFactory(new RemoteInvocationFactory() {
@Override
public RemoteInvocation createRemoteInvocation(MethodInvocation methodInvocation) {
RemoteInvocation invocation = new RemoteInvocation(methodInvocation);
invocation.addAttribute("myKey", "myValue");
try {
invocation.addAttribute("myKey", "myValue");
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException ex) {
// expected: already defined
}
assertNotNull(invocation.getAttributes());
assertEquals(1, invocation.getAttributes().size());
assertEquals("myValue", invocation.getAttributes().get("myKey"));
assertEquals("myValue", invocation.getAttribute("myKey"));
return invocation;
}
});
pfb.setHttpInvokerRequestExecutor(new AbstractHttpInvokerRequestExecutor() {
@Override
protected RemoteInvocationResult doExecuteRequest(HttpInvokerClientConfiguration config, ByteArrayOutputStream baos) throws Exception {
assertEquals("http://myurl", config.getServiceUrl());
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setContent(baos.toByteArray());
exporter.handleRequest(request, response);
return readRemoteInvocationResult(new ByteArrayInputStream(response.getContentAsByteArray()), config.getCodebaseUrl());
}
});
pfb.afterPropertiesSet();
ITestBean proxy = (ITestBean) pfb.getObject();
assertEquals("myname", proxy.getName());
assertEquals(99, proxy.getAge());
}
use of org.aopalliance.intercept.MethodInvocation in project spring-framework by spring-projects.
the class AspectJExpressionPointcut method matches.
@Override
public boolean matches(Method method, Class<?> targetClass, Object... args) {
checkReadyToMatch();
ShadowMatch shadowMatch = getShadowMatch(AopUtils.getMostSpecificMethod(method, targetClass), method);
ShadowMatch originalShadowMatch = getShadowMatch(method, method);
// Bind Spring AOP proxy to AspectJ "this" and Spring AOP target to AspectJ target,
// consistent with return of MethodInvocationProceedingJoinPoint
ProxyMethodInvocation pmi = null;
Object targetObject = null;
Object thisObject = null;
try {
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
targetObject = mi.getThis();
if (!(mi instanceof ProxyMethodInvocation)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
pmi = (ProxyMethodInvocation) mi;
thisObject = pmi.getProxy();
} catch (IllegalStateException ex) {
// No current invocation...
if (logger.isDebugEnabled()) {
logger.debug("Could not access current invocation - matching with limited context: " + ex);
}
}
try {
JoinPointMatch joinPointMatch = shadowMatch.matchesJoinPoint(thisObject, targetObject, args);
/*
* Do a final check to see if any this(TYPE) kind of residue match. For
* this purpose, we use the original method's (proxy method's) shadow to
* ensure that 'this' is correctly checked against. Without this check,
* we get incorrect match on this(TYPE) where TYPE matches the target
* type but not 'this' (as would be the case of JDK dynamic proxies).
* <p>See SPR-2979 for the original bug.
*/
if (pmi != null) {
// there is a current invocation
RuntimeTestWalker originalMethodResidueTest = getRuntimeTestWalker(originalShadowMatch);
if (!originalMethodResidueTest.testThisInstanceOfResidue(thisObject.getClass())) {
return false;
}
if (joinPointMatch.matches()) {
bindParameters(pmi, joinPointMatch);
}
}
return joinPointMatch.matches();
} catch (Throwable ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to evaluate join point for arguments " + Arrays.asList(args) + " - falling back to non-match", ex);
}
return false;
}
}
use of org.aopalliance.intercept.MethodInvocation in project spring-security by spring-projects.
the class MethodExpressionVoterTests method nullNamedFilterTargetIsRejected.
@Test(expected = IllegalArgumentException.class)
public void nullNamedFilterTargetIsRejected() throws Exception {
MethodInvocation mi = new SimpleMethodInvocation(new TargetImpl(), methodTakingACollection(), new Object[] { null });
am.vote(joe, mi, createAttributes(new PreInvocationExpressionAttribute("(filterObject == 'joe')", "collection", null)));
}
Aggregations