Skip to content

Commit

Permalink
Fix method reflection bug (#5030)
Browse files Browse the repository at this point in the history
* Remove unnecessary dependencies for audit module

* Remove unnecessary import

* Update ApolloAuditSpanAspect.java

* Update ApolloAuditSpanAspectTest.java

* Update ApolloAuditSpanAspect.java

* Update ApolloAuditSpanAspectTest.java
  • Loading branch information
BlackBear2003 authored Dec 4, 2023
1 parent d56a92f commit b67db2b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import com.ctrip.framework.apollo.audit.constants.ApolloAuditConstants;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.cglib.core.ReflectUtils;

@Aspect
public class ApolloAuditSpanAspect {
Expand Down Expand Up @@ -58,11 +60,15 @@ public Object around(ProceedingJoinPoint pjp, ApolloAuditLog auditLog) throws Th

void auditDataInfluenceArg(ProceedingJoinPoint pjp) {
Method method = findMethod(pjp);
if (Objects.isNull(method)) {
return;
}
Object[] args = pjp.getArgs();
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
Annotation[] annotations = method.getParameterAnnotations()[i];


boolean needAudit = false;
String entityName = null;
String fieldName = null;
Expand All @@ -87,13 +93,18 @@ void auditDataInfluenceArg(ProceedingJoinPoint pjp) {

Method findMethod(ProceedingJoinPoint pjp) {
Class<?> clazz = pjp.getTarget().getClass();
Signature pjpSignature = pjp.getSignature();
String methodName = pjp.getSignature().getName();
for (Method method : clazz.getDeclaredMethods()) {
if (method.getName().equals(methodName)) {
return method;
}
Class[] parameterTypes = null;
if (pjpSignature instanceof MethodSignature) {
parameterTypes = ((MethodSignature) pjpSignature).getParameterTypes();
}
try {
Method method = ReflectUtils.findDeclaredMethod(clazz, methodName, parameterTypes);
return method;
} catch (NoSuchMethodException e) {
return null;
}
return null;
}

void parseArgAndAppend(String entityName, String fieldName, Object arg) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.persistence.Id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.ctrip.framework.apollo.audit.aop;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
Expand All @@ -37,7 +38,7 @@
import java.util.Arrays;
import java.util.List;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -95,20 +96,36 @@ public void testAuditDataInfluenceArg() throws NoSuchMethodException {
.parseArgAndAppend(eq("App"), eq("Name"), eq(args[0]));
}

@Test
public void testAuditDataInfluenceArgCaseFindMethodReturnNull() throws NoSuchMethodException {
ProceedingJoinPoint mockPJP = mock(ProceedingJoinPoint.class);
Object[] args = new Object[]{new Object(), new Object()};
{
doReturn(null).when(aspect).findMethod(any());
when(mockPJP.getArgs()).thenReturn(args);
}
aspect.auditDataInfluenceArg(mockPJP);
verify(aspect, times(0))
.parseArgAndAppend(eq("App"), eq("Name"), eq(args[0]));
}

@Test
public void testFindMethod() throws NoSuchMethodException {
ProceedingJoinPoint mockPJP = mock(ProceedingJoinPoint.class);
MockAuditClass mockAuditClass = new MockAuditClass();
Signature signature = mock(Signature.class);
MethodSignature signature = mock(MethodSignature.class);
Method method = MockAuditClass.class.getMethod("mockAuditMethod", Object.class, Object.class);
Method sameNameMethod = MockAuditClass.class.getMethod("mockAuditMethod", Object.class);
{
when(mockPJP.getTarget()).thenReturn(mockAuditClass);
when(mockPJP.getSignature()).thenReturn(signature);
when(signature.getName()).thenReturn("mockAuditMethod");
when(signature.getParameterTypes()).thenReturn(new Class[]{Object.class, Object.class});
}
Method methodFounded = aspect.findMethod(mockPJP);

assertEquals(method, methodFounded);
assertNotEquals(sameNameMethod, methodFounded);
}

@Test
Expand Down Expand Up @@ -155,5 +172,10 @@ public void mockAuditMethod(
@ApolloAuditLogDataInfluenceTableField(fieldName = "Name") Object val1,
Object val2) {
}

// same name method test
public void mockAuditMethod(
Object val2) {
}
}
}
4 changes: 0 additions & 4 deletions apollo-audit/apollo-audit-spring-boot-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>

</project>

0 comments on commit b67db2b

Please sign in to comment.