Search in sources :

Example 1 with TypeAnnotationsScanner

use of org.reflections.scanners.TypeAnnotationsScanner 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 2 with TypeAnnotationsScanner

use of org.reflections.scanners.TypeAnnotationsScanner in project swagger-core by swagger-api.

the class BeanConfig method classes.

@Override
public Set<Class<?>> classes() {
    ConfigurationBuilder config = new ConfigurationBuilder();
    Set<String> acceptablePackages = new HashSet<String>();
    boolean allowAllPackages = false;
    if (resourcePackage != null && !"".equals(resourcePackage)) {
        String[] parts = resourcePackage.split(",");
        for (String pkg : parts) {
            if (!"".equals(pkg)) {
                acceptablePackages.add(pkg);
                config.addUrls(ClasspathHelper.forPackage(pkg));
            }
        }
    } else {
        allowAllPackages = true;
    }
    config.setScanners(new ResourcesScanner(), new TypeAnnotationsScanner(), new SubTypesScanner());
    final Reflections reflections = new Reflections(config);
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(javax.ws.rs.Path.class);
    Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(SwaggerDefinition.class);
    classes.addAll(typesAnnotatedWith);
    /*
         * Find concrete types annotated with @Api, but with a supertype annotated with @Path.
         * This would handle split resources where the interface has jax-rs annotations
         * and the implementing class has Swagger annotations 
         */
    for (Class<?> cls : reflections.getTypesAnnotatedWith(Api.class)) {
        for (Class<?> intfc : TypeToken.of(cls).getTypes().interfaces().rawTypes()) {
            Annotation ann = intfc.getAnnotation(javax.ws.rs.Path.class);
            if (ann != null) {
                classes.add(cls);
                break;
            }
        }
    }
    Set<Class<?>> output = new HashSet<Class<?>>();
    for (Class<?> cls : classes) {
        if (allowAllPackages) {
            output.add(cls);
        } else {
            for (String pkg : acceptablePackages) {
                if (cls.getPackage().getName().startsWith(pkg)) {
                    output.add(cls);
                }
            }
        }
    }
    return output;
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) ResourcesScanner(org.reflections.scanners.ResourcesScanner) Annotation(java.lang.annotation.Annotation) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) HashSet(java.util.HashSet) Reflections(org.reflections.Reflections)

Example 3 with TypeAnnotationsScanner

use of org.reflections.scanners.TypeAnnotationsScanner in project motech by motech.

the class ReflectionsUtil method getClasses.

/**
 * Looks for classes annotated with a given annotation.
 *
 * @param annotation an annotation to look for
 * @param bundle a bundle to look in.
 * @return A list of classes, annotated with the given annotation
 */
public static Set<Class<?>> getClasses(Class<? extends Annotation> annotation, Bundle bundle) {
    LOGGER.debug("Scanning bundle: {}", bundle.getSymbolicName());
    LOGGER.debug("Searching for classes with annotations: {}", annotation.getName());
    Reflections reflections = configureReflection(bundle, new PristineBundleClassLoader(bundle), new TypeAnnotationsScanner(), new SubTypesScanner());
    Set<Class<?>> classes = reflections.getTypesAnnotatedWith(annotation);
    // in order to prevent processing of user defined or auto generated fields
    // we have to load the bytecode from the jar and define the class in a temporary
    // classLoader
    PristineBundleClassLoader pristineBundleClassLoader = new PristineBundleClassLoader(bundle);
    Set<Class<?>> result = new HashSet<>();
    for (Class clazz : classes) {
        try {
            result.add(pristineBundleClassLoader.loadClass(clazz.getName()));
        } catch (ClassNotFoundException e) {
            LOGGER.error("Could not find class", e);
        }
    }
    LOGGER.debug("Searched for classes with annotations: {}", annotation.getName());
    LOGGER.trace("Found {} classes with annotations: {}", result.size(), annotation.getName());
    return result;
}
Also used : SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 4 with TypeAnnotationsScanner

use of org.reflections.scanners.TypeAnnotationsScanner in project pwm by pwm-project.

the class ControlledPwmServletTest method getClassAndMethods.

private Map<Class<? extends ControlledPwmServlet>, Map<String, Method>> getClassAndMethods() {
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("password.pwm")).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()));
    Set<Class<? extends ControlledPwmServlet>> classes = reflections.getSubTypesOf(ControlledPwmServlet.class);
    final Map<Class<? extends ControlledPwmServlet>, Map<String, Method>> returnMap = new HashMap<>();
    for (final Class<? extends ControlledPwmServlet> controlledPwmServlet : classes) {
        if (!Modifier.isAbstract(controlledPwmServlet.getModifiers())) {
            final Map<String, Method> annotatedMethods = new HashMap<>();
            for (Method method : JavaHelper.getAllMethodsForClass(controlledPwmServlet)) {
                if (method.getAnnotation(ControlledPwmServlet.ActionHandler.class) != null) {
                    final String actionName = method.getAnnotation(ControlledPwmServlet.ActionHandler.class).action();
                    annotatedMethods.put(actionName, method);
                }
            }
            returnMap.put(controlledPwmServlet, Collections.unmodifiableMap(annotatedMethods));
        }
    }
    return Collections.unmodifiableMap(returnMap);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) HashMap(java.util.HashMap) Map(java.util.Map) Reflections(org.reflections.Reflections)

Example 5 with TypeAnnotationsScanner

use of org.reflections.scanners.TypeAnnotationsScanner in project pwm by pwm-project.

the class RestServletTest method getClasses.

private Set<Class<? extends RestServlet>> getClasses() {
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("password.pwm")).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()));
    Set<Class<? extends RestServlet>> classes = reflections.getSubTypesOf(RestServlet.class);
    return Collections.unmodifiableSet(classes);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) RestServlet(password.pwm.ws.server.RestServlet) Reflections(org.reflections.Reflections)

Aggregations

Reflections (org.reflections.Reflections)18 SubTypesScanner (org.reflections.scanners.SubTypesScanner)18 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)18 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)13 URL (java.net.URL)7 HashSet (java.util.HashSet)6 ArrayList (java.util.ArrayList)4 FieldAnnotationsScanner (org.reflections.scanners.FieldAnnotationsScanner)4 MethodAnnotationsScanner (org.reflections.scanners.MethodAnnotationsScanner)4 File (java.io.File)3 ResourcesScanner (org.reflections.scanners.ResourcesScanner)3 IOException (java.io.IOException)2 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 URLClassLoader (java.net.URLClassLoader)2 DTO (org.eclipse.che.dto.shared.DTO)2 MethodParameterScanner (org.reflections.scanners.MethodParameterScanner)2 FilterBuilder (org.reflections.util.FilterBuilder)2 Resources (com.google.common.io.Resources)1 AggregateRoot (io.muoncore.newton.AggregateRoot)1