Skip to content

Commit

Permalink
feat: remove dynamic void restriction (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
YongwuHe authored Aug 9, 2023
1 parent 2727905 commit 060e0ed
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public List<MethodInstrumentation> methodAdvices() {
ElementMatcher.Junction<MethodDescription> matcher = null;
if (onlyClass != null) {
matcher = isMethod().and(not(takesNoArguments()))
.and(not(returns(TypeDescription.VOID)))
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
if (isNotAbstractClass(onlyClass.getClazzName())) {
matcher = matcher.and(not(isOverriddenFrom(namedOneOf(Config.get().getDynamicAbstractClassList()))));
Expand All @@ -108,7 +107,6 @@ public List<MethodInstrumentation> methodAdvices() {
private ElementMatcher.Junction<MethodDescription> builderMethodMatcher(DynamicClassEntity entity) {
ElementMatcher.Junction<MethodDescription> matcher =
parseTypeMatcher(entity.getOperation(), this::parseMethodMatcher)
.and(not(returns(TypeDescription.VOID)))
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
if (CollectionUtil.isNotEmpty(entity.getParameters())) {
matcher = matcher.and(takesArguments(entity.getParameters().size()));
Expand All @@ -126,19 +124,26 @@ public static boolean onEnter(@Advice.Origin Method method,
@Advice.AllArguments Object[] args,
@Advice.Local("extractor") DynamicClassExtractor extractor,
@Advice.Local("mockResult") MockResult mockResult) {
if (ContextManager.needRecord()) {
RepeatedCollectManager.enter();
}
if (ContextManager.needRecordOrReplay()) {
if (void.class.isAssignableFrom(method.getReturnType())) {
return ContextManager.needReplay();
}
extractor = new DynamicClassExtractor(method, args);
}
if (ContextManager.needReplay()) {
mockResult = extractor.replay();
return mockResult != null && mockResult.notIgnoreMockResult();
}
if (ContextManager.needRecord()) {
RepeatedCollectManager.enter();
}
return false;
}

/**
* void method will not record because of extractor == null
* when replay, just return;
*/
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Local("extractor") DynamicClassExtractor extractor,
@Advice.Local("mockResult") MockResult mockResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ static Stream<Arguments> methodAdvicesCase() {
DynamicClassEntity emptyOperation = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "", "", "");
Predicate<List<MethodInstrumentation>> emptyOperationPredicate = methodAdvices -> {
ElementMatcher<? super MethodDescription> matcher = methodAdvices.get(0).getMethodMatcher();
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 2;
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 3;
};

DynamicClassEntity testReturnVoidEntity = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "testReturnVoid", "", "");
DynamicClassEntity testReturnVoidWithParameterEntity = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "testReturnVoidWithParameter", "java.lang.String", "java.lang.System.currentTimeMillis");
Predicate<List<MethodInstrumentation>> emptyOperationAndVoidPredicate = methodAdvices -> {
ElementMatcher<? super MethodDescription> matcher = methodAdvices.get(0).getMethodMatcher();
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 0;
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 1;
};

DynamicClassEntity testReturnNonPrimitiveTypeWithParameterEntity = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "testReturnNonPrimitiveTypeWithParameter", "java.lang.String", null);
Expand All @@ -150,17 +150,21 @@ static Stream<Arguments> methodAdvicesCase() {
};

DynamicClassEntity testReturnWithParameterWildcard = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "*WithParameter*,testReturnVoid*,*WithParameter", "", null);
Predicate<List<MethodInstrumentation>> operationWithParameterWildcardPredicate = methodAdvices -> {
ElementMatcher<? super MethodDescription> matcher = methodAdvices.get(0).getMethodMatcher();
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 4;
};

final DynamicClassEntity testReturnNonPrimitiveType = new DynamicClassEntity(
"io.arex.inst.dynamic.DynamicTestClass", "testReturnNonPrimitiveType", "",
ArexConstants.UUID_SIGNATURE);
Predicate<List<MethodInstrumentation>> emptyListPredicate = List::isEmpty;

return Stream.of(
arguments("should_match_2_methods_when_empty_operation", Collections.singletonList(emptyOperation), NOT_EMPTY_PREDICATE.and(emptyOperationPredicate)),
arguments("should_match_0_method_when_with_return_void", Arrays.asList(testReturnVoidEntity, testReturnVoidWithParameterEntity), NOT_EMPTY_PREDICATE.and(emptyOperationAndVoidPredicate)),
arguments("should_match_3_methods_when_empty_operation", Collections.singletonList(emptyOperation), NOT_EMPTY_PREDICATE.and(emptyOperationPredicate)),
arguments("should_match_1_method_when_with_return_void", Arrays.asList(testReturnVoidEntity, testReturnVoidWithParameterEntity), NOT_EMPTY_PREDICATE.and(emptyOperationAndVoidPredicate)),
arguments("should_match_2_method_when_with_parameter", Arrays.asList(testReturnNonPrimitiveTypeWithParameterEntity, testReturnPrimitiveTypeWithParameter), NOT_EMPTY_PREDICATE.and(operationWithParameterPredicate)),
arguments("should_match_2_method_when_with_parameter_wildcard", Arrays.asList(testReturnWithParameterWildcard), NOT_EMPTY_PREDICATE.and(operationWithParameterPredicate)),
arguments("should_match_4_method_when_with_parameter_wildcard", Arrays.asList(testReturnWithParameterWildcard), NOT_EMPTY_PREDICATE.and(operationWithParameterWildcardPredicate)),
arguments("should_match_0_method_when_with_replace_uuid", Collections.singletonList(testReturnNonPrimitiveType), emptyListPredicate)
);
}
Expand Down

0 comments on commit 060e0ed

Please sign in to comment.