如何从具有指定泛型(interface )]的接口继承的所有类的列表中获取)> 我有命令界面 public interface ICommand {...} 和与特定命令关联的处理程序接口 public interface ICommandHandler<T extends ICommand> { IResponse handle(T command); } 例如,我有具体的命令 public class GetCatalogById implements ICommand{ private final long catalogId; public GetCatalogById(long catalogId) { this.catalogId = catalogId; } public long getCatalogId() { return catalogId; } } 如何以编程方式获取项目中所有类的列表(列表) implements ICommandHandler<GetCatalogById> ? 我具有命令接口公共接口ICommand {...}和与特定命令公共接口ICommandHandler相关的处理程序接口 {IResponse ... 您的问题可以分为2个子问题: 获取实现ICommandHandler的所有类的列表 具有必需的类型参数的过滤器 正如@ArvindKumarAvinash所说,您可以找到第一个子问题here的许多解决方案。 这是我第二个解决方案: 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; }

问题描述 投票:0回答:1

我有命令界面

public interface ICommand {...}

和与特定命令关联的处理程序接口

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

例如,我有具体的命令

public class GetCatalogById implements ICommand{

    private final long catalogId;

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

    public long getCatalogId() {
        return catalogId;
    }  
}

如何以编程方式获取项目中所有类的列表(列表)

implements ICommandHandler<GetCatalogById>

我具有命令接口公共接口ICommand {...}和与特定命令公共接口ICommandHandler相关的处理程序接口{IResponse ...

java generics interface implementation
1个回答
0
投票

您的问题可以分为2个子问题:

  1. 获取实现ICommandHandler的所有类的列表
© www.soinside.com 2019 - 2024. All rights reserved.