/*
 * Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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 c10n.guice;

import c10n.C10N;
import c10n.C10NMessages;
import com.google.inject.AbstractModule;
import org.reflections.Reflections;

import java.util.Set;

public class C10NModule extends AbstractModule {
  private final String[] packagePrefixes;

  public static C10NModule scanAllPackages() {
    return scanPackages("");
  }

  public static C10NModule scanPackages(String... packagePrefixes) {
    return new C10NModule(packagePrefixes);
  }

  private C10NModule(String[] packagePrefixes) {
    this.packagePrefixes = packagePrefixes;
  }

  @SuppressWarnings("unchecked")
  @Override
  protected void configure() {
    Set<Class<?>> c10nTypes = new Reflections(packagePrefixes)
            .getTypesAnnotatedWith(C10NMessages.class);
    for (Class<?> c10nType : c10nTypes) {
      bind((Class<Object>) c10nType)
              .toInstance(C10N.get(c10nType));
    }
  }
}

@@ -903,67 +903,374 @@
 gle.
-inject.AbstractModule;%0Aimport org.reflections.Reflections;%0A
+common.base.Function;%0Aimport com.google.common.collect.Iterables;%0Aimport com.google.common.collect.Sets;%0Aimport com.google.inject.AbstractModule;%0Aimport org.reflections.Reflections;%0Aimport org.reflections.util.ClasspathHelper;%0Aimport org.reflections.util.ConfigurationBuilder;%0Aimport org.reflections.util.FilterBuilder;%0A%0Aimport java.net.URL;%0Aimport java.util.Arrays;
 %0Aimp
@@ -1783,16 +1783,28 @@
 nTypes =
+%0A           
  new Ref
@@ -1812,31 +1812,108 @@
 ections(
-packagePrefixes
+new ConfigurationBuilder().filterInputsBy(getPackageInputFilter()).setUrls(getPackageURLs())
 )%0A      
@@ -2097,11 +2097,615 @@
    %7D%0A  %7D
+%0A%0A  private Set%3CURL%3E getPackageURLs() %7B%0A    Iterable%3CURL%3E packages = Iterables.concat(Iterables.transform(%0A            Arrays.asList(packagePrefixes), new Function%3CString, Set%3CURL%3E%3E() %7B%0A      @Override%0A      public Set%3CURL%3E apply(String prefix) %7B%0A        return ClasspathHelper.forPackage(prefix);%0A      %7D%0A    %7D));%0A%0A    return Sets.newHashSet(packages);%0A  %7D%0A%0A  private FilterBuilder getPackageInputFilter() %7B%0A    final FilterBuilder inputFilter = new FilterBuilder();%0A%0A    for (String prefix : packagePrefixes) %7B%0A      inputFilter.include(FilterBuilder.prefix(prefix));%0A    %7D%0A%0A    return inputFilter;%0A  %7D
 %0A%7D%0A

fix Reflection constructor for compatibility with 0.9.5 and 0.9.8 versions