diff --git a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java index 054d637139b..252f9d8a42c 100644 --- a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java +++ b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java @@ -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 { @@ -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; @@ -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) { diff --git a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/util/ApolloAuditUtil.java b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/util/ApolloAuditUtil.java index 85c69e1c70b..0b03535dfc3 100644 --- a/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/util/ApolloAuditUtil.java +++ b/apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/util/ApolloAuditUtil.java @@ -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; diff --git a/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspectTest.java b/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspectTest.java index 303a4f07df3..50414aae86c 100644 --- a/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspectTest.java +++ b/apollo-audit/apollo-audit-impl/src/test/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspectTest.java @@ -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; @@ -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; @@ -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 @@ -155,5 +172,10 @@ public void mockAuditMethod( @ApolloAuditLogDataInfluenceTableField(fieldName = "Name") Object val1, Object val2) { } + + // same name method test + public void mockAuditMethod( + Object val2) { + } } } diff --git a/apollo-audit/apollo-audit-spring-boot-starter/pom.xml b/apollo-audit/apollo-audit-spring-boot-starter/pom.xml index 11e52f2dd77..649ee2f2cb6 100644 --- a/apollo-audit/apollo-audit-spring-boot-starter/pom.xml +++ b/apollo-audit/apollo-audit-spring-boot-starter/pom.xml @@ -38,10 +38,6 @@ org.springframework.boot spring-boot-autoconfigure - - org.aspectj - aspectjrt - \ No newline at end of file