package org.cobbzilla.wizard.util;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.AssignableTypeFilter;
import org.springframework.core.type.filter.TypeFilter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.cobbzilla.util.reflect.ReflectionUtil.forName;

@NoArgsConstructor @Accessors(chain=true)
public class ClasspathScanner<T> {

    @Getter @Setter private TypeFilter filter;
    @Getter @Setter private String[] packages;

    public List<Class<? extends T>> scan() {
        final List<Class<? extends T>> classes = new ArrayList<>();
        final ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(filter);
        Arrays.stream(packages)
                .forEach(pkg -> scanner.findCandidateComponents(pkg)
                        .forEach(def -> classes.add(forName(def.getBeanClassName()))));
        return classes;
    }

    public static <C> List<C> scan(Class<C> iface, String[] packages) {
        return (List<C>) new ClasspathScanner<C>()
                .setFilter(new AssignableTypeFilter(iface))
                .setPackages(packages)
                .scan();
    }

}

@@ -1201,16 +1201,33 @@
 %3E List%3CC
+lass%3C? extends C%3E
 %3E scan(C
@@ -1280,18 +1280,8 @@
 turn
- (List%3CC%3E)
  new

fix bug in ClasspathScanner scan method signature