Skip to content

Commit

Permalink
fix: remove thread-unsafe cache operations (GitHub #299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Createsequence committed Jun 3, 2024
1 parent 3b15510 commit 5d90241
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions crane4j-core/src/main/java/cn/crane4j/core/util/ReflectUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ public class ReflectUtils {
*/
private static final Map<Class<?>, Method[]> METHOD_CACHE = CollectionUtils.newWeakConcurrentMap();

/**
* declared super class with interface
*/
private static final Map<Class<?>, Set<Class<?>>> DECLARED_SUPER_CLASS_WITH_INTERFACE = CollectionUtils.newWeakConcurrentMap();

// ====================== method ======================

/**
Expand Down Expand Up @@ -195,9 +190,14 @@ public static Method getDeclaredMethod(
*/
public static Method[] getMethods(@NonNull Class<?> type) {
Asserts.isNotNull(type, "type must not be null");
return CollectionUtils.computeIfAbsent(METHOD_CACHE, type, curr -> {
return CollectionUtils.computeIfAbsent(METHOD_CACHE, type, t -> {
List<Method> methods = new ArrayList<>();
traverseTypeHierarchy(curr, t -> methods.addAll(Arrays.asList(getDeclaredMethods(t))));
traverseTypeHierarchy(t, curr -> {
Method[] currMethods = getDeclaredMethods(curr);
if (ArrayUtils.isNotEmpty(currMethods)) {
methods.addAll(Arrays.asList(currMethods));
}
});
return methods.toArray(new Method[0]);
});
}
Expand Down Expand Up @@ -414,15 +414,13 @@ public static <A extends Annotation, E extends AnnotatedElement> void scanAllAnn
* @return declared super class with interface
*/
public static Set<Class<?>> getDeclaredSuperClassWithInterface(Class<?> type) {
return CollectionUtils.computeIfAbsent(DECLARED_SUPER_CLASS_WITH_INTERFACE, type, k -> {
Set<Class<?>> result = new LinkedHashSet<>();
Class<?> superClass = type.getSuperclass();
if (superClass != null) {
result.add(superClass);
}
result.addAll(Arrays.asList(type.getInterfaces()));
return result;
});
Set<Class<?>> result = new LinkedHashSet<>();
Class<?> superClass = type.getSuperclass();
if (superClass != null) {
result.add(superClass);
}
result.addAll(Arrays.asList(type.getInterfaces()));
return result;
}

/**
Expand Down Expand Up @@ -499,7 +497,12 @@ public static Field getField(Class<?> type, String fieldName) {
public static Field[] getFields(Class<?> type) {
return CollectionUtils.computeIfAbsent(FIELD_CACHE, type, t -> {
List<Field> fields = new ArrayList<>();
traverseTypeHierarchy(t, curr -> fields.addAll(Arrays.asList(getDeclaredFields(curr))));
traverseTypeHierarchy(t, curr -> {
Field[] currFields = getDeclaredFields(curr);
if (ArrayUtils.isNotEmpty(currFields)) {
fields.addAll(Arrays.asList(currFields));
}
});
return fields.toArray(new Field[0]);
});
}
Expand Down

0 comments on commit 5d90241

Please sign in to comment.