org.reflections.util.ConfigurationBuilder Maven / Gradle / Ivy
Show all versions of swagger-all Show documentation
package org.reflections.util;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.collect.ObjectArrays;
import com.google.common.collect.Sets;
import org.reflections.Configuration;
import org.reflections.Reflections;
import org.reflections.ReflectionsException;
import org.reflections.adapters.JavaReflectionAdapter;
import org.reflections.adapters.JavassistAdapter;
import org.reflections.adapters.MetadataAdapter;
import org.reflections.scanners.Scanner;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import org.reflections.serializers.Serializer;
import org.reflections.serializers.XmlSerializer;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* a fluent builder for {@link org.reflections.Configuration}, to be used for constructing a {@link org.reflections.Reflections} instance
* usage:
*
* new Reflections(
* new ConfigurationBuilder()
* .filterInputsBy(new FilterBuilder().include("your project's common package prefix here..."))
* .setUrls(ClasspathHelper.forClassLoader())
* .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(myClassAnnotationsFilter)));
*
*
{@link #executorService} is used optionally used for parallel scanning. if value is null then scanning is done in a simple for loop
* defaults: accept all for {@link #inputsFilter},
* {@link #executorService} is null,
* {@link #serializer} is {@link org.reflections.serializers.XmlSerializer}
*/
public class ConfigurationBuilder implements Configuration {
@Nonnull private Set scanners;
@Nonnull private Set urls;
/*lazy*/ protected MetadataAdapter metadataAdapter;
@Nullable private Predicate inputsFilter;
/*lazy*/ private Serializer serializer;
@Nullable private ExecutorService executorService;
@Nullable private ClassLoader[] classLoaders;
public ConfigurationBuilder() {
scanners = Sets.newHashSet(new TypeAnnotationsScanner(), new SubTypesScanner());
urls = Sets.newHashSet();
}
/** constructs a {@link ConfigurationBuilder} using the given parameters, in a non statically typed way. that is, each element in {@code params} is
* guessed by it's type and populated into the configuration.
*
* - {@link String} - add urls using {@link ClasspathHelper#forPackage(String, ClassLoader...)} ()}
* - {@link Class} - add urls using {@link ClasspathHelper#forClass(Class, ClassLoader...)}
* - {@link ClassLoader} - use these classloaders in order to find urls in ClasspathHelper.forPackage(), ClasspathHelper.forClass() and for resolving types
* - {@link Scanner} - use given scanner, overriding the default scanners
* - {@link URL} - add the given url for scanning
* - {@code Object[]} - flatten and use each element as above
*
*
* use any parameter type in any order. this constructor uses instanceof on each param and instantiate a {@link ConfigurationBuilder} appropriately.
* */
@SuppressWarnings("unchecked")
public static ConfigurationBuilder build(final @Nullable Object... params) {
ConfigurationBuilder builder = new ConfigurationBuilder();
//flatten
List