Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix method reflection bug #5030

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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 @@

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;

Check warning on line 106 in apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java

View check run for this annotation

Codecov / codecov/patch

apollo-audit/apollo-audit-impl/src/main/java/com/ctrip/framework/apollo/audit/aop/ApolloAuditSpanAspect.java#L105-L106

Added lines #L105 - L106 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary for the caller to implement handling mechanisms for null value scenarios?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for my late response, this method is trying to get the method which cause this method called.

So, when this method returns null, that would be a very strange situation.

Maybe just add a patch to end the audit logic rapidly when method returns 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>
Loading