Skip to content

Commit 8429292

Browse files
fix: remove thread-unsafe cache operations (GitHub #299)
1 parent 67b3c80 commit 8429292

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

crane4j-core/src/main/java/cn/crane4j/core/util/ReflectUtils.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ public class ReflectUtils {
6767
*/
6868
private static final Map<Class<?>, Method[]> METHOD_CACHE = CollectionUtils.newWeakConcurrentMap();
6969

70-
/**
71-
* declared super class with interface
72-
*/
73-
private static final Map<Class<?>, Set<Class<?>>> DECLARED_SUPER_CLASS_WITH_INTERFACE = CollectionUtils.newWeakConcurrentMap();
74-
7570
// ====================== method ======================
7671

7772
/**
@@ -195,9 +190,14 @@ public static Method getDeclaredMethod(
195190
*/
196191
public static Method[] getMethods(@NonNull Class<?> type) {
197192
Asserts.isNotNull(type, "type must not be null");
198-
return CollectionUtils.computeIfAbsent(METHOD_CACHE, type, curr -> {
193+
return CollectionUtils.computeIfAbsent(METHOD_CACHE, type, t -> {
199194
List<Method> methods = new ArrayList<>();
200-
traverseTypeHierarchy(curr, t -> methods.addAll(Arrays.asList(getDeclaredMethods(t))));
195+
traverseTypeHierarchy(t, curr -> {
196+
Method[] currMethods = getDeclaredMethods(curr);
197+
if (ArrayUtils.isNotEmpty(currMethods)) {
198+
methods.addAll(Arrays.asList(currMethods));
199+
}
200+
});
201201
return methods.toArray(new Method[0]);
202202
});
203203
}
@@ -414,15 +414,13 @@ public static <A extends Annotation, E extends AnnotatedElement> void scanAllAnn
414414
* @return declared super class with interface
415415
*/
416416
public static Set<Class<?>> getDeclaredSuperClassWithInterface(Class<?> type) {
417-
return CollectionUtils.computeIfAbsent(DECLARED_SUPER_CLASS_WITH_INTERFACE, type, k -> {
418-
Set<Class<?>> result = new LinkedHashSet<>();
419-
Class<?> superClass = type.getSuperclass();
420-
if (superClass != null) {
421-
result.add(superClass);
422-
}
423-
result.addAll(Arrays.asList(type.getInterfaces()));
424-
return result;
425-
});
417+
Set<Class<?>> result = new LinkedHashSet<>();
418+
Class<?> superClass = type.getSuperclass();
419+
if (superClass != null) {
420+
result.add(superClass);
421+
}
422+
result.addAll(Arrays.asList(type.getInterfaces()));
423+
return result;
426424
}
427425

428426
/**
@@ -499,7 +497,12 @@ public static Field getField(Class<?> type, String fieldName) {
499497
public static Field[] getFields(Class<?> type) {
500498
return CollectionUtils.computeIfAbsent(FIELD_CACHE, type, t -> {
501499
List<Field> fields = new ArrayList<>();
502-
traverseTypeHierarchy(t, curr -> fields.addAll(Arrays.asList(getDeclaredFields(curr))));
500+
traverseTypeHierarchy(t, curr -> {
501+
Field[] currFields = getDeclaredFields(curr);
502+
if (ArrayUtils.isNotEmpty(currFields)) {
503+
fields.addAll(Arrays.asList(currFields));
504+
}
505+
});
503506
return fields.toArray(new Field[0]);
504507
});
505508
}

0 commit comments

Comments
 (0)