Skip to content

Commit 060e0ed

Browse files
authored
feat: remove dynamic void restriction (#249)
1 parent 2727905 commit 060e0ed

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

arex-instrumentation/dynamic/arex-dynamic/src/main/java/io/arex/inst/dynamic/DynamicClassInstrumentation.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ public List<MethodInstrumentation> methodAdvices() {
8585
ElementMatcher.Junction<MethodDescription> matcher = null;
8686
if (onlyClass != null) {
8787
matcher = isMethod().and(not(takesNoArguments()))
88-
.and(not(returns(TypeDescription.VOID)))
8988
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
9089
if (isNotAbstractClass(onlyClass.getClazzName())) {
9190
matcher = matcher.and(not(isOverriddenFrom(namedOneOf(Config.get().getDynamicAbstractClassList()))));
@@ -108,7 +107,6 @@ public List<MethodInstrumentation> methodAdvices() {
108107
private ElementMatcher.Junction<MethodDescription> builderMethodMatcher(DynamicClassEntity entity) {
109108
ElementMatcher.Junction<MethodDescription> matcher =
110109
parseTypeMatcher(entity.getOperation(), this::parseMethodMatcher)
111-
.and(not(returns(TypeDescription.VOID)))
112110
.and(not(isAnnotatedWith(namedOneOf(DynamiConstants.SPRING_CACHE, DynamiConstants.AREX_MOCK))));
113111
if (CollectionUtil.isNotEmpty(entity.getParameters())) {
114112
matcher = matcher.and(takesArguments(entity.getParameters().size()));
@@ -126,19 +124,26 @@ public static boolean onEnter(@Advice.Origin Method method,
126124
@Advice.AllArguments Object[] args,
127125
@Advice.Local("extractor") DynamicClassExtractor extractor,
128126
@Advice.Local("mockResult") MockResult mockResult) {
127+
if (ContextManager.needRecord()) {
128+
RepeatedCollectManager.enter();
129+
}
129130
if (ContextManager.needRecordOrReplay()) {
131+
if (void.class.isAssignableFrom(method.getReturnType())) {
132+
return ContextManager.needReplay();
133+
}
130134
extractor = new DynamicClassExtractor(method, args);
131135
}
132136
if (ContextManager.needReplay()) {
133137
mockResult = extractor.replay();
134138
return mockResult != null && mockResult.notIgnoreMockResult();
135139
}
136-
if (ContextManager.needRecord()) {
137-
RepeatedCollectManager.enter();
138-
}
139140
return false;
140141
}
141142

143+
/**
144+
* void method will not record because of extractor == null
145+
* when replay, just return;
146+
*/
142147
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
143148
public static void onExit(@Advice.Local("extractor") DynamicClassExtractor extractor,
144149
@Advice.Local("mockResult") MockResult mockResult,

arex-instrumentation/dynamic/arex-dynamic/src/test/java/io/arex/inst/dynamic/DynamicClassInstrumentationTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ static Stream<Arguments> methodAdvicesCase() {
132132
DynamicClassEntity emptyOperation = new DynamicClassEntity("io.arex.inst.dynamic.DynamicTestClass", "", "", "");
133133
Predicate<List<MethodInstrumentation>> emptyOperationPredicate = methodAdvices -> {
134134
ElementMatcher<? super MethodDescription> matcher = methodAdvices.get(0).getMethodMatcher();
135-
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 2;
135+
return methodAdvices.size() == 1 && matchedMethodCount(matcher, DynamicTestClass.class) == 3;
136136
};
137137

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

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

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

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

159163
return Stream.of(
160-
arguments("should_match_2_methods_when_empty_operation", Collections.singletonList(emptyOperation), NOT_EMPTY_PREDICATE.and(emptyOperationPredicate)),
161-
arguments("should_match_0_method_when_with_return_void", Arrays.asList(testReturnVoidEntity, testReturnVoidWithParameterEntity), NOT_EMPTY_PREDICATE.and(emptyOperationAndVoidPredicate)),
164+
arguments("should_match_3_methods_when_empty_operation", Collections.singletonList(emptyOperation), NOT_EMPTY_PREDICATE.and(emptyOperationPredicate)),
165+
arguments("should_match_1_method_when_with_return_void", Arrays.asList(testReturnVoidEntity, testReturnVoidWithParameterEntity), NOT_EMPTY_PREDICATE.and(emptyOperationAndVoidPredicate)),
162166
arguments("should_match_2_method_when_with_parameter", Arrays.asList(testReturnNonPrimitiveTypeWithParameterEntity, testReturnPrimitiveTypeWithParameter), NOT_EMPTY_PREDICATE.and(operationWithParameterPredicate)),
163-
arguments("should_match_2_method_when_with_parameter_wildcard", Arrays.asList(testReturnWithParameterWildcard), NOT_EMPTY_PREDICATE.and(operationWithParameterPredicate)),
167+
arguments("should_match_4_method_when_with_parameter_wildcard", Arrays.asList(testReturnWithParameterWildcard), NOT_EMPTY_PREDICATE.and(operationWithParameterWildcardPredicate)),
164168
arguments("should_match_0_method_when_with_replace_uuid", Collections.singletonList(testReturnNonPrimitiveType), emptyListPredicate)
165169
);
166170
}

0 commit comments

Comments
 (0)