Skip to content

Commit 4b2e911

Browse files
YongwuHemr3
andauthored
feat: remove lambda suffix from cache loader name (#429)
Co-authored-by: Mark Zhang <[email protected]>
1 parent 11ee390 commit 4b2e911

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

arex-agent-bootstrap/src/main/java/io/arex/agent/bootstrap/util/StringUtil.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,4 +626,18 @@ public static int countMatches(final String str, final String sub) {
626626
}
627627
return count;
628628
}
629+
630+
public static String substringBefore(final String str, final String separator) {
631+
if (isEmpty(str) || separator == null) {
632+
return str;
633+
}
634+
if (separator.isEmpty()) {
635+
return EMPTY;
636+
}
637+
final int pos = str.indexOf(separator);
638+
if (pos == INDEX_NOT_FOUND) {
639+
return str;
640+
}
641+
return str.substring(0, pos);
642+
}
629643
}

arex-agent-bootstrap/src/test/java/io/arex/agent/bootstrap/util/StringUtilTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,15 @@ void countMatches() {
360360
assertEquals(0, StringUtil.countMatches("mock", "M"));
361361
assertEquals(1, StringUtil.countMatches("mock", "m"));
362362
}
363+
364+
@Test
365+
void substringBefore() {
366+
assertEquals(null, StringUtil.substringBefore(null, null));
367+
assertEquals("", StringUtil.substringBefore("", null));
368+
assertEquals("", StringUtil.substringBefore("", ""));
369+
assertEquals("", StringUtil.substringBefore("mock", ""));
370+
assertEquals("mock", StringUtil.substringBefore("mock", null));
371+
assertEquals("mock", StringUtil.substringBefore("mock", "cd"));
372+
assertEquals("mo", StringUtil.substringBefore("mock", "c"));
373+
}
363374
}

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
public class CacheLoaderUtil {
1515
private static final Map<Integer, Field> REFERENCE_FIELD_MAP = new ConcurrentHashMap<>();
1616
private static final Map<Integer, String> NO_REFERENCE_MAP = new ConcurrentHashMap<>();
17+
private static final String LAMBDA_SUFFIX = "$$";
1718

1819
/**
1920
* return the reference to the outer class in an inner class, if exists.
@@ -31,16 +32,12 @@ public static String getLocatedClass(Object loader) {
3132

3233
Class<?> loaderClass = loader.getClass();
3334
if (isNotAbstractOrInterface(loaderClass.getEnclosingClass())) {
34-
String loaderClassName = loaderClass.getName();
35-
NO_REFERENCE_MAP.put(loaderHashCode, loaderClassName);
36-
return loaderClassName;
35+
return generateNameWithNoReference(loaderHashCode, loaderClass);
3736
}
3837

3938
Field field = REFERENCE_FIELD_MAP.computeIfAbsent(loaderHashCode, k -> getReferenceField(loaderClass));
4039
if (field == null) {
41-
String loaderClassName = loaderClass.getName();
42-
NO_REFERENCE_MAP.put(loaderHashCode, loaderClassName);
43-
return loaderClassName;
40+
return generateNameWithNoReference(loaderHashCode, loaderClass);
4441
}
4542

4643
try {
@@ -52,6 +49,16 @@ public static String getLocatedClass(Object loader) {
5249
}
5350
}
5451

52+
/**
53+
* cache loader without external references directly obtain class name, and remove lambda suffix.
54+
* @return
55+
*/
56+
private static String generateNameWithNoReference(int loaderHashCode, Class<?> loaderClass) {
57+
String loaderClassName = StringUtil.substringBefore(loaderClass.getName(), LAMBDA_SUFFIX);
58+
NO_REFERENCE_MAP.put(loaderHashCode, loaderClassName);
59+
return loaderClassName;
60+
}
61+
5562
private static boolean isNotAbstractOrInterface(Class<?> clazz) {
5663
if (clazz == null) {
5764
return true;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.common.cache.CacheLoader;
44
import io.arex.inst.runtime.config.Config;
5-
import io.arex.inst.runtime.model.ArexConstants;
65
import org.junit.jupiter.api.Test;
76
import org.mockito.MockedStatic;
87
import org.mockito.Mockito;

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
<properties>
5656
<!-- Maven CI Friendly Versions https://maven.apache.org/maven-ci-friendly.html -->
57-
<revision>0.4.1</revision>
57+
<revision>0.4.2</revision>
5858
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5959
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
6060

0 commit comments

Comments
 (0)