org.springframework.aop.interceptor.ExposeInvocationInterceptor Java Examples
The following examples show how to use
org.springframework.aop.interceptor.ExposeInvocationInterceptor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AspectJProxyUtils.java From spring-analysis-note with MIT License | 6 votes |
/** * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors: * concretely, {@link ExposeInvocationInterceptor} at the beginning of the list. * <p>This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut * matching) and make available the current AspectJ JoinPoint. The call will have no effect * if there are no AspectJ advisors in the advisor chain. * @param advisors the advisors available * @return {@code true} if an {@link ExposeInvocationInterceptor} was added to the list, * otherwise {@code false} */ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) { // Don't add advisors to an empty list; may indicate that proxying is just not required if (!advisors.isEmpty()) { boolean foundAspectJAdvice = false; for (Advisor advisor : advisors) { // Be careful not to get the Advice without a guard, as this might eagerly // instantiate a non-singleton AspectJ aspect... if (isAspectJAdvice(advisor)) { foundAspectJAdvice = true; break; } } if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) { advisors.add(0, ExposeInvocationInterceptor.ADVISOR); return true; } } return false; }
Example #2
Source File: DeclareMixinAspectJAdvisorFactoryTest.java From jdal with Apache License 2.0 | 6 votes |
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); for (Object a : advisors) { pf.addAdvisor((Advisor) a); } pf.setExposeProxy(true); return pf.getProxy(); }
Example #3
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Override public Object invoke(MethodInvocation mi) throws Throwable { String task = "get invocation on way IN"; try { MethodInvocation current = ExposeInvocationInterceptor.currentInvocation(); assertEquals(mi.getMethod(), current.getMethod()); Object retval = mi.proceed(); task = "get invocation on way OUT"; assertEquals(current, ExposeInvocationInterceptor.currentInvocation()); return retval; } catch (IllegalStateException ex) { System.err.println(task + " for " + mi.getMethod()); ex.printStackTrace(); throw ex; } }
Example #4
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testTargetCanGetInvocation() throws Throwable { final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean(); AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class}); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); TrapTargetInterceptor tii = new TrapTargetInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
Example #5
Source File: MethodInvocationProceedingJoinPointTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@Test public void testCanGetStaticPartFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, Object target) throws Throwable { StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); } }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do itb.getAge(); }
Example #6
Source File: AbstractAspectJAdvisorFactoryTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); for (Object a : advisors) { pf.addAdvisor((Advisor) a); } pf.setExposeProxy(true); return pf.getProxy(); }
Example #7
Source File: AspectJProxyUtils.java From spring4-understanding with Apache License 2.0 | 6 votes |
/** * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors. * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching) * and make available the current AspectJ JoinPoint. The call will have no effect if there are no * AspectJ advisors in the advisor chain. * @param advisors Advisors available * @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false}. */ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) { // Don't add advisors to an empty list; may indicate that proxying is just not required if (!advisors.isEmpty()) { boolean foundAspectJAdvice = false; for (Advisor advisor : advisors) { // Be careful not to get the Advice without a guard, as // this might eagerly instantiate a non-singleton AspectJ aspect if (isAspectJAdvice(advisor)) { foundAspectJAdvice = true; } } if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) { advisors.add(0, ExposeInvocationInterceptor.ADVISOR); return true; } } return false; }
Example #8
Source File: AspectJProxyUtils.java From lams with GNU General Public License v2.0 | 6 votes |
/** * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors. * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching) * and make available the current AspectJ JoinPoint. The call will have no effect if there are no * AspectJ advisors in the advisor chain. * @param advisors Advisors available * @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false}. */ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) { // Don't add advisors to an empty list; may indicate that proxying is just not required if (!advisors.isEmpty()) { boolean foundAspectJAdvice = false; for (Advisor advisor : advisors) { // Be careful not to get the Advice without a guard, as // this might eagerly instantiate a non-singleton AspectJ aspect if (isAspectJAdvice(advisor)) { foundAspectJAdvice = true; } } if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) { advisors.add(0, ExposeInvocationInterceptor.ADVISOR); return true; } } return false; }
Example #9
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Override public Object invoke(MethodInvocation mi) throws Throwable { String task = "get invocation on way IN"; try { MethodInvocation current = ExposeInvocationInterceptor.currentInvocation(); assertEquals(mi.getMethod(), current.getMethod()); Object retval = mi.proceed(); task = "get invocation on way OUT"; assertEquals(current, ExposeInvocationInterceptor.currentInvocation()); return retval; } catch (IllegalStateException ex) { System.err.println(task + " for " + mi.getMethod()); ex.printStackTrace(); throw ex; } }
Example #10
Source File: AbstractAopProxyTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testTargetCanGetInvocation() throws Throwable { final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean(); AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); TrapTargetInterceptor tii = new TrapTargetInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
Example #11
Source File: MethodInvocationProceedingJoinPointTests.java From java-technology-stack with MIT License | 6 votes |
@Test public void testCanGetStaticPartFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); } }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do itb.getAge(); }
Example #12
Source File: AbstractAspectJAdvisorFactoryTests.java From java-technology-stack with MIT License | 6 votes |
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); pf.addAdvisors(advisors); pf.setExposeProxy(true); return pf.getProxy(); }
Example #13
Source File: AspectJProxyUtils.java From java-technology-stack with MIT License | 6 votes |
/** * Add special advisors if necessary to work with a proxy chain that contains AspectJ advisors. * This will expose the current Spring AOP invocation (necessary for some AspectJ pointcut matching) * and make available the current AspectJ JoinPoint. The call will have no effect if there are no * AspectJ advisors in the advisor chain. * @param advisors the advisors available * @return {@code true} if any special {@link Advisor Advisors} were added, otherwise {@code false} */ public static boolean makeAdvisorChainAspectJCapableIfNecessary(List<Advisor> advisors) { // Don't add advisors to an empty list; may indicate that proxying is just not required if (!advisors.isEmpty()) { boolean foundAspectJAdvice = false; for (Advisor advisor : advisors) { // Be careful not to get the Advice without a guard, as // this might eagerly instantiate a non-singleton AspectJ aspect if (isAspectJAdvice(advisor)) { foundAspectJAdvice = true; } } if (foundAspectJAdvice && !advisors.contains(ExposeInvocationInterceptor.ADVISOR)) { advisors.add(0, ExposeInvocationInterceptor.ADVISOR); return true; } } return false; }
Example #14
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
@Override public Object invoke(MethodInvocation mi) throws Throwable { String task = "get invocation on way IN"; try { MethodInvocation current = ExposeInvocationInterceptor.currentInvocation(); assertEquals(mi.getMethod(), current.getMethod()); Object retval = mi.proceed(); task = "get invocation on way OUT"; assertEquals(current, ExposeInvocationInterceptor.currentInvocation()); return retval; } catch (IllegalStateException ex) { System.err.println(task + " for " + mi.getMethod()); ex.printStackTrace(); throw ex; } }
Example #15
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testTargetCanGetInvocation() throws Throwable { final InvocationCheckExposedInvocationTestBean expectedTarget = new InvocationCheckExposedInvocationTestBean(); AdvisedSupport pc = new AdvisedSupport(ITestBean.class, IOther.class); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); TrapTargetInterceptor tii = new TrapTargetInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } }; pc.addAdvice(tii); pc.setTarget(expectedTarget); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); tb.getName(); }
Example #16
Source File: MethodInvocationProceedingJoinPointTests.java From spring-analysis-note with MIT License | 6 votes |
@Test public void testCanGetStaticPartFromJoinPoint() { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart(); assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart()); assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind()); assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature()); assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation()); } }); ITestBean itb = (ITestBean) pf.getProxy(); // Any call will do itb.getAge(); }
Example #17
Source File: AbstractAspectJAdvisorFactoryTests.java From spring-analysis-note with MIT License | 6 votes |
protected Object createProxy(Object target, List<Advisor> advisors, Class<?>... interfaces) { ProxyFactory pf = new ProxyFactory(target); if (interfaces.length > 1 || interfaces[0].isInterface()) { pf.setInterfaces(interfaces); } else { pf.setProxyTargetClass(true); } // Required everywhere we use AspectJ proxies pf.addAdvice(ExposeInvocationInterceptor.INSTANCE); pf.addAdvisors(advisors); pf.setExposeProxy(true); return pf.getProxy(); }
Example #18
Source File: AopUtilsTests.java From spring-analysis-note with MIT License | 5 votes |
/** * Test that when we serialize and deserialize various canonical instances * of AOP classes, they return the same instance, not a new instance * that's subverted the singleton construction limitation. */ @Test public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception { assertSame(MethodMatcher.TRUE, SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE)); assertSame(ClassFilter.TRUE, SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE)); assertSame(Pointcut.TRUE, SerializationTestUtils.serializeAndDeserialize(Pointcut.TRUE)); assertSame(EmptyTargetSource.INSTANCE, SerializationTestUtils.serializeAndDeserialize(EmptyTargetSource.INSTANCE)); assertSame(Pointcuts.SETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.SETTERS)); assertSame(Pointcuts.GETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.GETTERS)); assertSame(ExposeInvocationInterceptor.INSTANCE, SerializationTestUtils.serializeAndDeserialize(ExposeInvocationInterceptor.INSTANCE)); }
Example #19
Source File: AbstractAopProxyTests.java From spring-analysis-note with MIT License | 5 votes |
/** * Throw an exception if there is an Invocation. */ private void assertNoInvocationContext() { try { ExposeInvocationInterceptor.currentInvocation(); fail("Expected no invocation context"); } catch (IllegalStateException ex) { // ok } }
Example #20
Source File: MethodInvocationProceedingJoinPointTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, @Nullable Object target) throws Throwable { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't really care... } }
Example #21
Source File: AbstractAspectJAdvice.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Lazily instantiate joinpoint for the current invocation. * Requires MethodInvocation to be bound with ExposeInvocationInterceptor. * <p>Do not use if access is available to the current ReflectiveMethodInvocation * (in an around advice). * @return current AspectJ joinpoint, or through an exception if we're not in a * Spring AOP invocation. */ public static JoinPoint currentJoinPoint() { MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation(); if (!(mi instanceof ProxyMethodInvocation)) { throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); } ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; JoinPoint jp = (JoinPoint) pmi.getUserAttribute(JOIN_POINT_KEY); if (jp == null) { jp = new MethodInvocationProceedingJoinPoint(pmi); pmi.setUserAttribute(JOIN_POINT_KEY, jp); } return jp; }
Example #22
Source File: AbstractAspectJAdvice.java From lams with GNU General Public License v2.0 | 5 votes |
/** * Get the current join point match at the join point we are being dispatched on. */ protected JoinPointMatch getJoinPointMatch() { MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation(); if (!(mi instanceof ProxyMethodInvocation)) { throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); } return getJoinPointMatch((ProxyMethodInvocation) mi); }
Example #23
Source File: AbstractAspectJAdvice.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Lazily instantiate joinpoint for the current invocation. * Requires MethodInvocation to be bound with ExposeInvocationInterceptor. * <p>Do not use if access is available to the current ReflectiveMethodInvocation * (in an around advice). * @return current AspectJ joinpoint, or through an exception if we're not in a * Spring AOP invocation. */ public static JoinPoint currentJoinPoint() { MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation(); if (!(mi instanceof ProxyMethodInvocation)) { throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); } ProxyMethodInvocation pmi = (ProxyMethodInvocation) mi; JoinPoint jp = (JoinPoint) pmi.getUserAttribute(JOIN_POINT_KEY); if (jp == null) { jp = new MethodInvocationProceedingJoinPoint(pmi); pmi.setUserAttribute(JOIN_POINT_KEY, jp); } return jp; }
Example #24
Source File: AbstractAspectJAdvice.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Get the current join point match at the join point we are being dispatched on. */ protected JoinPointMatch getJoinPointMatch() { MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation(); if (!(mi instanceof ProxyMethodInvocation)) { throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi); } return getJoinPointMatch((ProxyMethodInvocation) mi); }
Example #25
Source File: MethodInvocationProceedingJoinPointTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { final Object raw = new TestBean(); ProxyFactory pf = new ProxyFactory(raw); pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR); pf.addAdvice(new MethodBeforeAdvice() { @Override public void before(Method method, Object[] args, Object target) throws Throwable { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't realy care... } }
Example #26
Source File: AopUtilsTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * Test that when we serialize and deserialize various canonical instances * of AOP classes, they return the same instance, not a new instance * that's subverted the singleton construction limitation. */ @Test public void testCanonicalFrameworkClassesStillCanonicalOnDeserialization() throws Exception { assertSame(MethodMatcher.TRUE, SerializationTestUtils.serializeAndDeserialize(MethodMatcher.TRUE)); assertSame(ClassFilter.TRUE, SerializationTestUtils.serializeAndDeserialize(ClassFilter.TRUE)); assertSame(Pointcut.TRUE, SerializationTestUtils.serializeAndDeserialize(Pointcut.TRUE)); assertSame(EmptyTargetSource.INSTANCE, SerializationTestUtils.serializeAndDeserialize(EmptyTargetSource.INSTANCE)); assertSame(Pointcuts.SETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.SETTERS)); assertSame(Pointcuts.GETTERS, SerializationTestUtils.serializeAndDeserialize(Pointcuts.GETTERS)); assertSame(ExposeInvocationInterceptor.INSTANCE, SerializationTestUtils.serializeAndDeserialize(ExposeInvocationInterceptor.INSTANCE)); }
Example #27
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * @param context if true, want context */ private void testContext(final boolean context) throws Throwable { final String s = "foo"; // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { if (!context) { assertNoInvocationContext(); } else { assertNotNull("have context", ExposeInvocationInterceptor.currentInvocation()); } return s; } }; AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class}); if (context) { pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); } pc.addAdvice(mi); // Keep CGLIB happy if (requiresTarget()) { pc.setTarget(new TestBean()); } AopProxy aop = createAopProxy(pc); assertNoInvocationContext(); ITestBean tb = (ITestBean) aop.getProxy(); assertNoInvocationContext(); assertSame("correct return value", s, tb.getName()); }
Example #28
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testDeclaredException() throws Throwable { final Exception expectedException = new Exception(); // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { throw expectedException; } }; AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class}); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(mi); // We don't care about the object mockTargetSource.setTarget(new Object()); pc.setTargetSource(mockTargetSource); AopProxy aop = createAopProxy(pc); try { ITestBean tb = (ITestBean) aop.getProxy(); // Note: exception param below isn't used tb.exceptional(expectedException); fail("Should have thrown exception raised by interceptor"); } catch (Exception thrown) { assertEquals("exception matches", expectedException, thrown); } }
Example #29
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
/** * An interceptor throws a checked exception not on the method signature. * For efficiency, we don't bother unifying java.lang.reflect and * org.springframework.cglib UndeclaredThrowableException */ @Test public void testUndeclaredCheckedException() throws Throwable { final Exception unexpectedException = new Exception(); // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { throw unexpectedException; } }; AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class}); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(mi); // We don't care about the object pc.setTarget(new TestBean()); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); try { // Note: exception param below isn't used tb.getAge(); fail("Should have wrapped exception raised by interceptor"); } catch (UndeclaredThrowableException thrown) { assertEquals("exception matches", unexpectedException, thrown.getUndeclaredThrowable()); } catch (Exception ex) { ex.printStackTrace(); fail("Didn't expect exception: " + ex); } }
Example #30
Source File: AbstractAopProxyTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testUndeclaredUnheckedException() throws Throwable { final RuntimeException unexpectedException = new RuntimeException(); // Test return value MethodInterceptor mi = new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { throw unexpectedException; } }; AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class}); pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); pc.addAdvice(mi); // We don't care about the object pc.setTarget(new TestBean()); AopProxy aop = createAopProxy(pc); ITestBean tb = (ITestBean) aop.getProxy(); try { // Note: exception param below isn't used tb.getAge(); fail("Should have wrapped exception raised by interceptor"); } catch (RuntimeException thrown) { assertEquals("exception matches", unexpectedException, thrown); } }