Java Code Examples for org.springframework.aop.interceptor.ExposeInvocationInterceptor

The following examples show how to use org.springframework.aop.interceptor.ExposeInvocationInterceptor. These examples are extracted from open source projects. 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 Project: spring-analysis-note   Source File: AspectJProxyUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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
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 3
@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 4
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
@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 5
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
@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 6
Source Project: java-technology-stack   Source File: AspectJProxyUtils.java    License: MIT License 6 votes vote down vote up
/**
 * 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 7
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 8
@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 9
Source Project: java-technology-stack   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
@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 10
Source Project: java-technology-stack   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
@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 11
Source Project: lams   Source File: AspectJProxyUtils.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 12
Source Project: spring4-understanding   Source File: AspectJProxyUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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 13
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 14
@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 15
@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 16
@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 17
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 18
Source Project: sca-best-practice   Source File: CodelessDaoFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CodelessDao getObject() {
    ProxyFactory result = new ProxyFactory();
    result.setTarget(codelessDaoProxy);
    result.setInterfaces(CodelessDao.class, SpringProxy.class);
    result.addAdvice(SurroundingTransactionDetectorMethodInterceptor.INSTANCE);
    result.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    result.addAdvice(new DefaultMethodInvokingMethodInterceptor());
    return (CodelessDao)result.getProxy(classLoader);
}
 
Example 19
Source Project: spring-analysis-note   Source File: AbstractAspectJAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * 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 20
Source Project: spring-analysis-note   Source File: AbstractAspectJAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * Get the current join point match at the join point we are being dispatched on.
 */
@Nullable
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 21
@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 22
Source Project: spring-analysis-note   Source File: AopUtilsTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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 23
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 5 votes vote down vote up
/**
 * @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(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 24
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 5 votes vote down vote up
@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(ITestBean.class);
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	mockTargetSource.setTarget(new TestBean());
	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 25
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 5 votes vote down vote up
/**
 * 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(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 26
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testUndeclaredUncheckedException() 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(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);
	}
}
 
Example 27
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 5 votes vote down vote up
/**
 * Throw an exception if there is an Invocation.
 */
private void assertNoInvocationContext() {
	try {
		ExposeInvocationInterceptor.currentInvocation();
		fail("Expected no invocation context");
	}
	catch (IllegalStateException ex) {
		// ok
	}
}
 
Example 28
Source Project: java-technology-stack   Source File: AbstractAspectJAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * 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 29
Source Project: java-technology-stack   Source File: AbstractAspectJAdvice.java    License: MIT License 5 votes vote down vote up
/**
 * Get the current join point match at the join point we are being dispatched on.
 */
@Nullable
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 30
@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...
	}
}