Skip to content

Commit

Permalink
Polish apache/incubator-dubbo/apache#4096 : Add Utilities class
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed May 20, 2019
1 parent 2f20ddd commit 90431be
Showing 1 changed file with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,28 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URL;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.Arrays.asList;
import static java.util.Collections.unmodifiableSet;

/**
* ReflectUtils
Expand Down Expand Up @@ -1096,4 +1105,61 @@ public static Map<String, Method> getBeanPropertyReadMethods(Class cl) {

return properties;
}

/**
* Find the {@link Set} of {@link ParameterizedType}
*
* @param sourceClass the source {@link Class class}
* @return non-null read-only {@link Set}
* @since 2.7.2
*/
public static Set<ParameterizedType> findParameterizedTypes(Class<?> sourceClass) {
// Add Generic Interfaces
List<Type> genericTypes = new LinkedList<>(asList(sourceClass.getGenericInterfaces()));
// Add Generic Super Class
genericTypes.add(sourceClass.getGenericSuperclass());

Set<ParameterizedType> parameterizedTypes = genericTypes.stream()
.filter(type -> type instanceof ParameterizedType)// filter ParameterizedType
.map(type -> ParameterizedType.class.cast(type)) // cast to ParameterizedType
.collect(Collectors.toSet());

if (parameterizedTypes.isEmpty()) { // If not found, try to search super types recursively
genericTypes.stream()
.filter(type -> type instanceof Class)
.map(type -> Class.class.cast(type))
.forEach(superClass -> {
parameterizedTypes.addAll(findParameterizedTypes(superClass));
});
}

return unmodifiableSet(parameterizedTypes); // build as a Set

}

/**
* Find the hierarchical types form the source {@link Class class} by specified {@link Class type}.
*
* @param sourceClass the source {@link Class class}
* @param matchType the type to match
* @param <T> the type to match
* @return non-null read-only {@link Set}
* @since 2.7.2
*/
public static <T> Set<Class<T>> findHierarchicalTypes(Class<?> sourceClass, Class<T> matchType) {
if (sourceClass == null) {
return Collections.emptySet();
}

Set<Class<T>> hierarchicalTypes = new LinkedHashSet<>();

if (matchType.isAssignableFrom(sourceClass)) {
hierarchicalTypes.add((Class<T>) sourceClass);
}

// Find all super classes
hierarchicalTypes.addAll(findHierarchicalTypes(sourceClass.getSuperclass(), matchType));

return unmodifiableSet(hierarchicalTypes);
}
}

0 comments on commit 90431be

Please sign in to comment.