Skip to content

Commit 793a22b

Browse files
authored
feat: remove spring cache dynamic class configuration restrictions (#444)
1 parent e22f84f commit 793a22b

File tree

6 files changed

+22
-131
lines changed

6 files changed

+22
-131
lines changed

arex-instrumentation/dynamic/arex-cache/src/main/java/io/arex/inst/cache/spring/SpringCacheAdviceHelper.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

arex-instrumentation/dynamic/arex-cache/src/main/java/io/arex/inst/cache/spring/SpringCacheInstrumentation.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
66

77
import io.arex.agent.bootstrap.model.MockResult;
8+
import io.arex.inst.cache.util.CacheLoaderUtil;
89
import io.arex.inst.dynamic.common.DynamicClassExtractor;
910
import io.arex.inst.extension.MethodInstrumentation;
1011
import io.arex.inst.extension.TypeInstrumentation;
@@ -46,7 +47,7 @@ public static boolean onEnter(@Advice.Argument(2) Method method,
4647
return false;
4748
}
4849

49-
if (SpringCacheAdviceHelper.needRecordOrReplay(method)) {
50+
if (CacheLoaderUtil.needRecordOrReplay(method)) {
5051
Cacheable cacheable = method.getDeclaredAnnotation(Cacheable.class);
5152
String keyExpression = cacheable != null ? cacheable.key() : null;
5253
extractor = new DynamicClassExtractor(method, args, keyExpression, null);

arex-instrumentation/dynamic/arex-cache/src/main/java/io/arex/inst/cache/util/CacheLoaderUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.arex.inst.runtime.log.LogManager;
88

99
import java.lang.reflect.Field;
10+
import java.lang.reflect.Method;
1011
import java.util.Map;
1112
import java.util.concurrent.ConcurrentHashMap;
1213

@@ -116,12 +117,21 @@ public static boolean needRecordOrReplay(Object cacheLoader) {
116117
if (ArrayUtils.isEmpty(coveragePackages)){
117118
return false;
118119
}
119-
String loaderClassName = MapUtils.getString(NO_REFERENCE_MAP, System.identityHashCode(cacheLoader), cacheLoader.getClass().getName());
120+
String loaderClassName = getClassName(cacheLoader);
120121
for (String packageName : coveragePackages) {
121122
if (StringUtil.startWith(loaderClassName, packageName)) {
122123
return true;
123124
}
124125
}
125126
return false;
126127
}
128+
129+
private static String getClassName(Object cacheLoader) {
130+
// spring cache
131+
if (cacheLoader instanceof Method) {
132+
return ((Method) cacheLoader).getDeclaringClass().getName();
133+
}
134+
// caffeine/guava cache
135+
return MapUtils.getString(NO_REFERENCE_MAP, System.identityHashCode(cacheLoader), cacheLoader.getClass().getName());
136+
}
127137
}

arex-instrumentation/dynamic/arex-cache/src/test/java/io/arex/inst/cache/spring/SpringCacheAdviceHelperTest.java

Lines changed: 0 additions & 88 deletions
This file was deleted.

arex-instrumentation/dynamic/arex-cache/src/test/java/io/arex/inst/cache/spring/SpringCacheInstrumentationTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import io.arex.agent.bootstrap.model.MockResult;
77
import io.arex.inst.cache.TestArexMock;
8+
import io.arex.inst.cache.util.CacheLoaderUtil;
89
import io.arex.inst.dynamic.common.DynamicClassExtractor;
910
import io.arex.inst.extension.MethodInstrumentation;
1011
import io.arex.inst.runtime.context.ContextManager;
@@ -31,7 +32,7 @@ static void setUp() {
3132
Mockito.mockStatic(ContextManager.class);
3233
Mockito.mockStatic(MockUtils.class);
3334
Mockito.mockStatic(RepeatedCollectManager.class);
34-
Mockito.mockStatic(SpringCacheAdviceHelper.class);
35+
Mockito.mockStatic(CacheLoaderUtil.class);
3536
}
3637

3738
@AfterAll
@@ -63,12 +64,12 @@ void onEnter() throws NoSuchMethodException {
6364
assertFalse(actualResult);
6465

6566
// not record
66-
Mockito.when(SpringCacheAdviceHelper.needRecordOrReplay(any())).thenReturn(false);
67+
Mockito.when(CacheLoaderUtil.needRecordOrReplay(any())).thenReturn(false);
6768
actualResult = SpringCacheInstrumentation.SpringCacheAdvice.onEnter(test1, new Object[]{ "name", 18 }, null, null);
6869
assertFalse(actualResult);
6970

7071
// record
71-
Mockito.when(SpringCacheAdviceHelper.needRecordOrReplay(any())).thenReturn(true);
72+
Mockito.when(CacheLoaderUtil.needRecordOrReplay(any())).thenReturn(true);
7273
Mockito.when(ContextManager.needRecord()).thenReturn(true);
7374
DynamicClassExtractor extractor = new DynamicClassExtractor(test1, new Object[]{"mock"}, "#val", null);
7475
actualResult = SpringCacheInstrumentation.SpringCacheAdvice.onEnter(test1, new Object[]{ "name", 18 }, extractor, null);
@@ -106,4 +107,4 @@ void onExit() throws NoSuchMethodException {
106107
Mockito.verify(extractor, Mockito.times(1)).recordResponse(throwable);
107108
}
108109
}
109-
}
110+
}

arex-instrumentation/dynamic/arex-cache/src/test/java/io/arex/inst/cache/util/CacheLoaderUtilTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@ void getLocatedClass() throws Exception {
6868
}
6969

7070
@Test
71-
void needRecordOrReplay() {
71+
void needRecordOrReplay() throws NoSuchMethodException {
7272
try (MockedStatic<Config> configMockedStatic = Mockito.mockStatic(Config.class)) {
7373
Config config = Mockito.mock(Config.class);
7474
Mockito.when(Config.get()).thenReturn(config);
7575
Mockito.when(config.getCoveragePackages()).thenReturn(new String[]{"io.arex.inst"});
76+
// method in coverage packages
77+
assertTrue(CacheLoaderUtil.needRecordOrReplay(CacheLoaderUtilTest.class.getDeclaredMethod("getLocatedClass")));
78+
7679
// loader is null
7780
assertFalse(CacheLoaderUtil.needRecordOrReplay(null));
7881
// loader is in coverage packages

0 commit comments

Comments
 (0)