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()));
}
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;
}
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;
}
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);
}
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);
}
Aggregations