Search in sources :

Example 1 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project kafka by apache.

the class ConnectorFactory method getConnectorClass.

@SuppressWarnings("unchecked")
private static Class<? extends Connector> getConnectorClass(String connectorClassOrAlias) {
    // Avoid the classpath scan if the full class name was provided
    try {
        Class<?> clazz = Class.forName(connectorClassOrAlias);
        if (!Connector.class.isAssignableFrom(clazz))
            throw new ConnectException("Class " + connectorClassOrAlias + " does not implement Connector");
        return (Class<? extends Connector>) clazz;
    } catch (ClassNotFoundException e) {
    // Fall through to scan for the alias
    }
    // Iterate over our entire classpath to find all the connectors and hopefully one of them matches the alias from the connector configration
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forJavaClassPath()));
    Set<Class<? extends Connector>> connectors = reflections.getSubTypesOf(Connector.class);
    List<Class<? extends Connector>> results = new ArrayList<>();
    for (Class<? extends Connector> connector : connectors) {
        // Configuration included the class name but not package
        if (connector.getSimpleName().equals(connectorClassOrAlias))
            results.add(connector);
        // Configuration included a short version of the name (i.e. FileStreamSink instead of FileStreamSinkConnector)
        if (connector.getSimpleName().equals(connectorClassOrAlias + "Connector"))
            results.add(connector);
    }
    if (results.isEmpty())
        throw new ConnectException("Failed to find any class that implements Connector and which name matches " + connectorClassOrAlias + ", available connectors are: " + connectorNames(connectors));
    if (results.size() > 1) {
        throw new ConnectException("More than one connector matches alias " + connectorClassOrAlias + ". Please use full package and class name instead. Classes found: " + connectorNames(results));
    }
    // We just validated that we have exactly one result, so this is safe
    return results.get(0);
}
Also used : Connector(org.apache.kafka.connect.connector.Connector) ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) ConnectException(org.apache.kafka.connect.errors.ConnectException) Reflections(org.reflections.Reflections)

Example 2 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project che by eclipse.

the class DynaProviderGenerator method findDynaObjects.

private void findDynaObjects() throws IOException {
    ConfigurationBuilder configuration = new ConfigurationBuilder();
    List<URL> urls = new ArrayList<>();
    for (String element : classpath) {
        urls.add(new File(element).toURI().toURL());
    }
    ClassLoader contextClassLoader = URLClassLoader.newInstance(urls.toArray(new URL[urls.size()]), Thread.currentThread().getContextClassLoader());
    Thread.currentThread().setContextClassLoader(contextClassLoader);
    configuration.setUrls(ClasspathHelper.forClassLoader(contextClassLoader));
    configuration.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
    Reflections reflection = new Reflections(configuration);
    Set<Class<?>> classes = reflection.getTypesAnnotatedWith(DynaObject.class);
    for (Class clazz : classes) {
        //accept only classes
        if (clazz.isEnum() || clazz.isInterface() || clazz.isAnnotation()) {
            continue;
        }
        dynaClasses.add(new ClassModel(clazz));
        System.out.println(String.format("New Dyna Object Found: %s", clazz.getCanonicalName()));
    }
    System.out.println(String.format("Found: %d Dyna Objects", dynaClasses.size()));
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) URL(java.net.URL) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) URLClassLoader(java.net.URLClassLoader) File(java.io.File) Reflections(org.reflections.Reflections)

Example 3 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project deeplearning4j by deeplearning4j.

the class NeuralNetConfiguration method registerSubtypes.

private static synchronized void registerSubtypes(ObjectMapper mapper) {
    //Register concrete subtypes for JSON serialization
    List<Class<?>> classes = Arrays.<Class<?>>asList(InputPreProcessor.class, ILossFunction.class, IActivation.class, Layer.class, GraphVertex.class, ReconstructionDistribution.class);
    List<String> classNames = new ArrayList<>(6);
    for (Class<?> c : classes) classNames.add(c.getName());
    // First: scan the classpath and find all instances of the 'baseClasses' classes
    if (subtypesClassCache == null) {
        //Check system property:
        String prop = System.getProperty(CUSTOM_FUNCTIONALITY);
        if (prop != null && !Boolean.parseBoolean(prop)) {
            subtypesClassCache = Collections.emptySet();
        } else {
            List<Class<?>> interfaces = Arrays.<Class<?>>asList(InputPreProcessor.class, ILossFunction.class, IActivation.class, ReconstructionDistribution.class);
            List<Class<?>> classesList = Arrays.<Class<?>>asList(Layer.class, GraphVertex.class);
            Collection<URL> urls = ClasspathHelper.forClassLoader();
            List<URL> scanUrls = new ArrayList<>();
            for (URL u : urls) {
                String path = u.getPath();
                if (!path.matches(".*/jre/lib/.*jar")) {
                    //Skip JRE/JDK JARs
                    scanUrls.add(u);
                }
            }
            Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(new FilterBuilder().exclude(//Consider only .class files (to avoid debug messages etc. on .dlls, etc
            "^(?!.*\\.class$).*$").exclude("^org.nd4j.*").exclude("^org.datavec.*").exclude(//JavaCPP
            "^org.bytedeco.*").exclude(//Jackson
            "^com.fasterxml.*").exclude(//Apache commons, Spark, log4j etc
            "^org.apache.*").exclude("^org.projectlombok.*").exclude("^com.twelvemonkeys.*").exclude("^org.joda.*").exclude("^org.slf4j.*").exclude("^com.google.*").exclude("^org.reflections.*").exclude(//Logback
            "^ch.qos.*")).addUrls(scanUrls).setScanners(new DL4JSubTypesScanner(interfaces, classesList)));
            org.reflections.Store store = reflections.getStore();
            Iterable<String> subtypesByName = store.getAll(DL4JSubTypesScanner.class.getSimpleName(), classNames);
            Set<? extends Class<?>> subtypeClasses = Sets.newHashSet(ReflectionUtils.forNames(subtypesByName));
            subtypesClassCache = new HashSet<>();
            for (Class<?> c : subtypeClasses) {
                if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
                    //log.info("Skipping abstract/interface: {}",c);
                    continue;
                }
                subtypesClassCache.add(c);
            }
        }
    }
    //Second: get all currently registered subtypes for this mapper
    Set<Class<?>> registeredSubtypes = new HashSet<>();
    for (Class<?> c : classes) {
        AnnotatedClass ac = AnnotatedClass.construct(c, mapper.getSerializationConfig().getAnnotationIntrospector(), null);
        Collection<NamedType> types = mapper.getSubtypeResolver().collectAndResolveSubtypes(ac, mapper.getSerializationConfig(), mapper.getSerializationConfig().getAnnotationIntrospector());
        for (NamedType nt : types) {
            registeredSubtypes.add(nt.getType());
        }
    }
    //Third: register all _concrete_ subtypes that are not already registered
    List<NamedType> toRegister = new ArrayList<>();
    for (Class<?> c : subtypesClassCache) {
        //Check if it's concrete or abstract...
        if (Modifier.isAbstract(c.getModifiers()) || Modifier.isInterface(c.getModifiers())) {
            //log.info("Skipping abstract/interface: {}",c);
            continue;
        }
        if (!registeredSubtypes.contains(c)) {
            String name;
            if (ClassUtils.isInnerClass(c)) {
                Class<?> c2 = c.getDeclaringClass();
                name = c2.getSimpleName() + "$" + c.getSimpleName();
            } else {
                name = c.getSimpleName();
            }
            toRegister.add(new NamedType(c, name));
            if (log.isDebugEnabled()) {
                for (Class<?> baseClass : classes) {
                    if (baseClass.isAssignableFrom(c)) {
                        log.debug("Registering class for JSON serialization: {} as subtype of {}", c.getName(), baseClass.getName());
                        break;
                    }
                }
            }
        }
    }
    mapper.registerSubtypes(toRegister.toArray(new NamedType[toRegister.size()]));
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) NamedType(org.nd4j.shade.jackson.databind.jsontype.NamedType) URL(java.net.URL) AnnotatedClass(org.nd4j.shade.jackson.databind.introspect.AnnotatedClass) FilterBuilder(org.reflections.util.FilterBuilder) DL4JSubTypesScanner(org.deeplearning4j.util.reflections.DL4JSubTypesScanner) AnnotatedClass(org.nd4j.shade.jackson.databind.introspect.AnnotatedClass) Reflections(org.reflections.Reflections)

Example 4 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project deeplearning4j by deeplearning4j.

the class DefaultI18N method loadLanguageResources.

private synchronized void loadLanguageResources(String languageCode) {
    if (loadedLanguages.contains(languageCode))
        return;
    //Scan classpath for resources in the /dl4j_i18n/ directory...
    URL url = this.getClass().getResource("/" + DEFAULT_I8N_RESOURCES_DIR + "/");
    Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()).setUrls(url));
    String pattern = ".*" + languageCode;
    Set<String> resources = reflections.getResources(Pattern.compile(pattern));
    Map<String, String> messages = new HashMap<>();
    for (String s : resources) {
        if (!s.endsWith(languageCode))
            continue;
        log.trace("Attempting to parse file: {}", s);
        parseFile(s, messages);
    }
    messagesByLanguage.put(languageCode, messages);
    loadedLanguages.add(languageCode);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) URL(java.net.URL) Reflections(org.reflections.Reflections)

Example 5 with ConfigurationBuilder

use of org.reflections.util.ConfigurationBuilder in project disconf by knightliao.

the class ReflectionScanStatic method getReflection.

/**
     * 通过扫描,获取反射对象
     */
private Reflections getReflection(List<String> packNameList) {
    //
    // filter
    //
    FilterBuilder filterBuilder = new FilterBuilder().includePackage(Constants.DISCONF_PACK_NAME);
    for (String packName : packNameList) {
        filterBuilder = filterBuilder.includePackage(packName);
    }
    Predicate<String> filter = filterBuilder;
    //
    // urls
    //
    Collection<URL> urlTotals = new ArrayList<URL>();
    for (String packName : packNameList) {
        Set<URL> urls = ClasspathHelper.forPackage(packName);
        urlTotals.addAll(urls);
    }
    //
    Reflections reflections = new Reflections(new ConfigurationBuilder().filterInputsBy(filter).setScanners(new SubTypesScanner().filterResultsBy(filter), new TypeAnnotationsScanner().filterResultsBy(filter), new FieldAnnotationsScanner().filterResultsBy(filter), new MethodAnnotationsScanner().filterResultsBy(filter), new MethodParameterScanner()).setUrls(urlTotals));
    return reflections;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ArrayList(java.util.ArrayList) URL(java.net.URL) MethodParameterScanner(org.reflections.scanners.MethodParameterScanner) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) MethodAnnotationsScanner(org.reflections.scanners.MethodAnnotationsScanner) FilterBuilder(org.reflections.util.FilterBuilder) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections)

Aggregations

ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)23 Reflections (org.reflections.Reflections)20 SubTypesScanner (org.reflections.scanners.SubTypesScanner)10 URL (java.net.URL)7 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)7 ArrayList (java.util.ArrayList)6 FilterBuilder (org.reflections.util.FilterBuilder)6 ResourcesScanner (org.reflections.scanners.ResourcesScanner)4 HashSet (java.util.HashSet)3 Test (org.junit.Test)3 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)3 File (java.io.File)2 IOException (java.io.IOException)2 DTO (org.eclipse.che.dto.shared.DTO)2 BeforeClass (org.junit.BeforeClass)2 FieldAnnotationsScanner (org.reflections.scanners.FieldAnnotationsScanner)2 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)2 Stopwatch (com.google.common.base.Stopwatch)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Resources (com.google.common.io.Resources)1