/*
 * Copyright 2015-2017 Canoo Engineering AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.canoo.dp.impl.server.scanner;

import com.canoo.dp.impl.platform.core.Assert;
import com.canoo.platform.server.spi.components.ClasspathScanner;
import org.apiguardian.api.API;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.FilterBuilder;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.net.URL;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static org.apiguardian.api.API.Status.INTERNAL;

/**
 * This class can be used to search for a set of classes in the classpath. Currently all classes that are annotated
 * with a specific annotation can be found.
 */
@API(since = "0.x", status = INTERNAL)
public class DefaultClasspathScanner implements ClasspathScanner {

    private final Reflections reflections;

    public DefaultClasspathScanner() {
        this(Collections.emptyList());
    }

    public DefaultClasspathScanner(final String rootPackage){
        this(Collections.singletonList(rootPackage));
    }

    public DefaultClasspathScanner(final String... rootPackages){
        this(Arrays.asList(rootPackages));
    }

    public DefaultClasspathScanner(final List<String> rootPackages) {

        ConfigurationBuilder configuration = ConfigurationBuilder.build(DefaultClasspathScanner.class.getClassLoader());
        configuration = configuration.setExpandSuperTypes(false);

        configuration = configuration.forPackages(rootPackages.toArray(new String[rootPackages.size()]));
        configuration = configuration.setUrls(rootPackages.stream().map(rootPackage -> ClasspathHelper.forPackage(rootPackage)).flatMap(list -> list.stream()).collect(Collectors.toList()));
        configuration = configuration.filterInputsBy(new FilterBuilder().includePackage(rootPackages.toArray(new String[rootPackages.size()])));

        //Special case for JBOSS Application server to get all classes
        try {
            Enumeration<URL> res = DefaultClasspathScanner.class.getClassLoader().getResources("");
            configuration.getUrls().addAll(Collections.list(res));
        } catch (IOException e) {
            throw new RuntimeException("Error in Dolphin Platform controller class scan", e);
        }


        //Remove native libs (will be added on Mac in a Spring Boot app)
        Set<URL> urls = configuration.getUrls();
        List<URL> toRemove = new ArrayList<>();
        for (URL url : urls) {
            if (url.toString().endsWith(".jnilib")) {
                toRemove.add(url);
            }
        }
        for (URL url : toRemove) {
            configuration.getUrls().remove(url);
        }

        reflections = new Reflections(configuration);
    }

    /**
     * Returns a set that contains all classes in the classpath that are annotated with the given annotation
     * @param annotation the annotation
     * @return the set of annotated classes
     */
    public synchronized Set<Class<?>> getTypesAnnotatedWith(final Class<? extends Annotation> annotation) {
        Assert.requireNonNull(annotation, "annotation");
        return reflections.getTypesAnnotatedWith(annotation);
    }
}

@@ -964,16 +964,73 @@
 Builder;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -1136,43 +1136,8 @@
 .*;%0A
-import java.util.stream.Collector;%0A
 impo
@@ -1496,24 +1496,119 @@
 hScanner %7B%0A%0A
+    private static final Logger LOG = LoggerFactory.getLogger(DefaultClasspathScanner.class);%0A%0A
     private 
@@ -2030,24 +2030,208 @@
 ackages) %7B%0A%0A
+        Assert.requireNonNull(rootPackages, %22rootPackages%22);%0A%0A        LOG.info(%22Scanning class path for root packages %7B%7D%22, Arrays.toString(rootPackages.toArray()));%0A%0A        //logging%0A
         Conf
@@ -2402,24 +2402,90 @@
 es(false);%0A%0A
+        if(rootPackages != null && rootPackages.size() %3E 0) %7B%0A    
         conf
@@ -2574,24 +2574,28 @@
 .size()%5D));%0A
+    
         conf
@@ -2768,24 +2768,28 @@
 toList()));%0A
+    
         conf
@@ -2916,24 +2916,34 @@
 .size()%5D)));
+%0A        %7D
 %0A%0A        //
@@ -3669,24 +3669,80 @@
 toRemove) %7B%0A
+            LOG.info(%22Url removed %7B%7D%22, url.toString());%0A
             
@@ -3774,24 +3774,24 @@
 emove(url);%0A
+
         %7D%0A%0A 
@@ -3780,33 +3780,32 @@
 url);%0A        %7D%0A
-%0A
         reflecti

Fix failing test