简体   繁体   English

如何获取从具有指定泛型(接口)的接口继承的所有类的列表<concrete class> )</concrete>

[英]How get list of all classes that inherit from the interface with the specified generic (interface<concrete class>)

I have command interface我有命令界面

public interface ICommand {...}

and handler interface that is associated with specific command和与特定命令关联的处理程序接口

public interface ICommandHandler<T extends ICommand> {
   IResponse handle(T command);
}

For example, I have concrete command例如,我有具体的命令

public class GetCatalogById implements ICommand{

    private final long catalogId;

    public GetCatalogById(long catalogId) {
        this.catalogId = catalogId;
    }

    public long getCatalogId() {
        return catalogId;
    }  
}

How programmatically get list (List) of all classes in project that如何以编程方式获取项目中所有类的列表(List)

implements ICommandHandler<GetCatalogById>

? ?

Your problem can be divided into 2 subproblems:您的问题可以分为 2 个子问题:

  1. Getting a list of all classes that implement ICommandHandler获取实现ICommandHandler的所有类的列表
  2. Filtering ones which have a required type parameter过滤具有所需类型参数的那些

As @ArvindKumarAvinash said, you can find many solutions to the first subproblem here .正如@ArvindKumarAvinash所说,您可以在此处找到第一个子问题的许多解决方案。

And here is my solution for a second one:这是我的第二个解决方案:

public static <T extends ICommand> List<Class<? extends ICommandHandler<T>>> getCommandHandlers(
        Class<T> commandClass, String packageName
) {
    return new Reflections(packageName).getSubTypesOf(ICommandHandler.class).stream()
            .filter(subtype -> !subtype.isInterface())
            .filter(subtype -> Objects.equals(getParameter(subtype, ICommandHandler.class, 0), commandClass))
            .map(subtype -> (Class<? extends ICommandHandler<T>>) subtype)
            .collect(Collectors.toList());
}


@Nullable
public static <T> Type getParameter(
        Class<T> clazz,
        Class<? super T> parametrizedParent,
        int index
) {
    Type result = null;
    for (ParameterizedType parent : getParameterizedParents(clazz, parametrizedParent)) {
        result = parent.getActualTypeArguments()[index];
        if (!(result instanceof TypeVariable)) return result;
        index = getTypeVariableIndex((TypeVariable<?>) result);
    }
    return result;
}

private static <T> List<ParameterizedType> getParameterizedParents(Class<? extends T> clazz, Class<T> parent) {
    List<ParameterizedType> genericParents = new ArrayList<>();
    Class<? extends T> current = clazz;
    while (true) {
        Type supertype = getSuperType(current, parent);
        if (supertype instanceof ParameterizedType)
            genericParents.add((ParameterizedType) supertype);
        else genericParents.clear();
        Type rawSupertype = toRawType(supertype);
        if (rawSupertype == parent) {
            Collections.reverse(genericParents);
            return genericParents;
        }
        current = (Class<? extends T>) rawSupertype;
    }
}

private static <T> Type getSuperType(Class<? extends T> child, Class<T> parent) {
    if (child == parent) return child;
    Type superclass = child.getGenericSuperclass();
    if (isSubTypeOfClass(superclass, parent)) return superclass;
    for (Type type : child.getGenericInterfaces())
        if (isSubTypeOfClass(type, parent)) return type;
    throw new IllegalArgumentException(child.getName() + " is not assignable from " + parent.getName());
}

private static int getTypeVariableIndex(final TypeVariable<?> typeVariable) {
    return Arrays.asList(typeVariable.getGenericDeclaration().getTypeParameters()).indexOf(typeVariable);
}

private static boolean isSubTypeOfClass(Type type, Class<?> clazz) {
    Type rawType = toRawType(type);
    return rawType instanceof Class && clazz.isAssignableFrom((Class<?>) rawType);
}

private static Type toRawType(Type type) {
    return type instanceof ParameterizedType ? ((ParameterizedType) type).getRawType() : type;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM