Search in sources :

Example 1 with StaticPart

use of org.aspectj.lang.JoinPoint.StaticPart in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method testCanGetStaticPartFromJoinPoint.

@Test
public void testCanGetStaticPartFromJoinPoint() {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
        assertThat(AbstractAspectJAdvice.currentJoinPoint().getStaticPart()).as("Same static part must be returned on subsequent requests").isEqualTo(staticPart);
        assertThat(staticPart.getKind()).isEqualTo(ProceedingJoinPoint.METHOD_EXECUTION);
        assertThat(staticPart.getSignature()).isSameAs(AbstractAspectJAdvice.currentJoinPoint().getSignature());
        assertThat(staticPart.getSourceLocation()).isEqualTo(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation());
    });
    ITestBean itb = (ITestBean) pf.getProxy();
    // Any call will do
    itb.getAge();
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) Test(org.junit.jupiter.api.Test)

Example 2 with StaticPart

use of org.aspectj.lang.JoinPoint.StaticPart in project spring-framework by spring-projects.

the class MethodInvocationProceedingJoinPointTests method toShortAndLongStringFormedCorrectly.

@Test
public void toShortAndLongStringFormedCorrectly() throws Exception {
    final Object raw = new TestBean();
    ProxyFactory pf = new ProxyFactory(raw);
    pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
    pf.addAdvice((MethodBeforeAdvice) (method, args, target) -> {
        // makeEncSJP, although meant for computing the enclosing join point,
        // it serves our purpose here
        StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
        JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
        assertThat(jp.getSignature().toLongString()).isEqualTo(aspectJVersionJp.getSignature().toLongString());
        assertThat(jp.getSignature().toShortString()).isEqualTo(aspectJVersionJp.getSignature().toShortString());
        assertThat(jp.getSignature().toString()).isEqualTo(aspectJVersionJp.getSignature().toString());
        assertThat(jp.toLongString()).isEqualTo(aspectJVersionJp.toLongString());
        assertThat(jp.toShortString()).isEqualTo(aspectJVersionJp.toShortString());
        assertThat(jp.toString()).isEqualTo(aspectJVersionJp.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...
    }
}
Also used : ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Arrays(java.util.Arrays) AopUtils(org.springframework.aop.support.AopUtils) SourceLocation(org.aspectj.lang.reflect.SourceLocation) Factory(org.aspectj.runtime.reflect.Factory) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) AopContext(org.springframework.aop.framework.AopContext) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProxyFactory(org.springframework.aop.framework.ProxyFactory) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) TestBean(org.springframework.beans.testfixture.beans.TestBean) MethodSignature(org.aspectj.lang.reflect.MethodSignature) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) ProxyFactory(org.springframework.aop.framework.ProxyFactory) IOException(java.io.IOException) StaticPart(org.aspectj.lang.JoinPoint.StaticPart) JoinPoint(org.aspectj.lang.JoinPoint) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)2 Arrays (java.util.Arrays)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 JoinPoint (org.aspectj.lang.JoinPoint)2 StaticPart (org.aspectj.lang.JoinPoint.StaticPart)2 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)2 MethodSignature (org.aspectj.lang.reflect.MethodSignature)2 SourceLocation (org.aspectj.lang.reflect.SourceLocation)2 Factory (org.aspectj.runtime.reflect.Factory)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 Test (org.junit.jupiter.api.Test)2 MethodBeforeAdvice (org.springframework.aop.MethodBeforeAdvice)2 AopContext (org.springframework.aop.framework.AopContext)2 ProxyFactory (org.springframework.aop.framework.ProxyFactory)2 ExposeInvocationInterceptor (org.springframework.aop.interceptor.ExposeInvocationInterceptor)2 AopUtils (org.springframework.aop.support.AopUtils)2 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2