BAEL-4071 - Get advised method info in Spring AOP

Fixing identation
This commit is contained in:
Sallo Szrajbman 2020-12-19 12:58:00 +00:00
parent 73af581e18
commit 0c926e7441
6 changed files with 94 additions and 95 deletions

View File

@ -23,9 +23,6 @@ public class Account {
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{" + "accountNumber='" + accountNumber + '\'' + ", balance=" + balance + '}';
"accountNumber='" + accountNumber + '\'' +
", balance=" + balance +
'}';
} }
} }

View File

@ -14,10 +14,10 @@ import java.util.Arrays;
@Component @Component
public class BankAccountAspect { public class BankAccountAspect {
@Before(value="@annotation(com.baeldung.method.info.AccountOperation)") @Before(value = "@annotation(com.baeldung.method.info.AccountOperation)")
public void getAccountOperationInfo(JoinPoint joinPoint) { public void getAccountOperationInfo(JoinPoint joinPoint) {
//Method Information // Method Information
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
System.out.println("full method description: " + signature.getMethod()); System.out.println("full method description: " + signature.getMethod());
@ -26,23 +26,26 @@ public class BankAccountAspect {
System.out.println("declaring type: " + signature.getDeclaringType()); System.out.println("declaring type: " + signature.getDeclaringType());
//Method args // Method args
System.out.println("Method args names:"); System.out.println("Method args names:");
Arrays.stream(signature.getParameterNames()).forEach(s -> System.out.println("arg name: " + s)); Arrays.stream(signature.getParameterNames())
.forEach(s -> System.out.println("arg name: " + s));
System.out.println("Method args types:"); System.out.println("Method args types:");
Arrays.stream(signature.getParameterTypes()).forEach(s -> System.out.println("arg type: " + s)); Arrays.stream(signature.getParameterTypes())
.forEach(s -> System.out.println("arg type: " + s));
System.out.println("Method args values:"); System.out.println("Method args values:");
Arrays.stream(joinPoint.getArgs()).forEach(o -> System.out.println("arg value: " + o.toString())); Arrays.stream(joinPoint.getArgs())
.forEach(o -> System.out.println("arg value: " + o.toString()));
//Additional Information // Additional Information
System.out.println("returning type: " + signature.getReturnType()); System.out.println("returning type: " + signature.getReturnType());
System.out.println("method modifier: " + Modifier.toString(signature.getModifiers())); System.out.println("method modifier: " + Modifier.toString(signature.getModifiers()));
Arrays.stream(signature.getExceptionTypes()) Arrays.stream(signature.getExceptionTypes())
.forEach(aClass -> System.out.println("exception type: " + aClass)); .forEach(aClass -> System.out.println("exception type: " + aClass));
//Method annotation // Method annotation
Method method = signature.getMethod(); Method method = signature.getMethod();
AccountOperation accountOperation = method.getAnnotation(AccountOperation.class); AccountOperation accountOperation = method.getAnnotation(AccountOperation.class);
System.out.println("Account operation annotation: " + accountOperation); System.out.println("Account operation annotation: " + accountOperation);
@ -50,4 +53,3 @@ public class BankAccountAspect {
} }
} }

View File

@ -14,7 +14,7 @@ public class BankAccountService {
@AccountOperation(operation = "withdraw") @AccountOperation(operation = "withdraw")
public void withdraw(Account account, Double amount) throws WithdrawLimitException { public void withdraw(Account account, Double amount) throws WithdrawLimitException {
if(amount > 500.0) { if (amount > 500.0) {
throw new WithdrawLimitException("Withdraw limit exceeded."); throw new WithdrawLimitException("Withdraw limit exceeded.");
} }
@ -26,5 +26,4 @@ public class BankAccountService {
return RandomUtils.nextDouble(); return RandomUtils.nextDouble();
} }
} }

View File

@ -31,7 +31,8 @@ class BankAccountServiceIntegrationTest {
@Test @Test
void withdrawWhenLimitReached() { void withdrawWhenLimitReached() {
Assertions.assertThatExceptionOfType(WithdrawLimitException.class).isThrownBy(() -> bankAccountService.withdraw(account, 600.0)); Assertions.assertThatExceptionOfType(WithdrawLimitException.class)
.isThrownBy(() -> bankAccountService.withdraw(account, 600.0));
assertTrue(account.getBalance() == 2000.0); assertTrue(account.getBalance() == 2000.0);
} }