use of org.reflections.scanners.TypeAnnotationsScanner in project cosmic by MissionCriticalCloud.
the class ReflectUtil method getClassesWithAnnotation.
// Gets all classes with some annotation from a package
public static Set<Class<?>> getClassesWithAnnotation(final Class<? extends Annotation> annotation, final String[] packageNames) {
final Reflections reflections;
final Set<Class<?>> classes = new HashSet<>();
final ConfigurationBuilder builder = new ConfigurationBuilder();
for (final String packageName : packageNames) {
builder.addUrls(ClasspathHelper.forPackage(packageName));
}
builder.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
reflections = new Reflections(builder);
classes.addAll(reflections.getTypesAnnotatedWith(annotation));
return classes;
}
use of org.reflections.scanners.TypeAnnotationsScanner in project Shadbot by Shadorc.
the class CommandManager method init.
public static boolean init() {
LogUtils.infof("Initializing commands...");
Reflections reflections = new Reflections(Shadbot.class.getPackage().getName(), new SubTypesScanner(), new TypeAnnotationsScanner());
for (Class<?> cmdClass : reflections.getTypesAnnotatedWith(Command.class)) {
if (!AbstractCommand.class.isAssignableFrom(cmdClass)) {
LogUtils.error(String.format("An error occurred while generating command, %s cannot be cast to AbstractCommand.", cmdClass.getSimpleName()));
continue;
}
try {
AbstractCommand cmd = (AbstractCommand) cmdClass.getConstructor().newInstance();
List<String> names = cmd.getNames();
if (!cmd.getAlias().isEmpty()) {
names.add(cmd.getAlias());
}
for (String name : names) {
if (COMMANDS_MAP.putIfAbsent(name, cmd) != null) {
LogUtils.error(String.format("Command name collision between %s and %s", cmdClass.getSimpleName(), COMMANDS_MAP.get(name).getClass().getSimpleName()));
continue;
}
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException err) {
LogUtils.error(err, String.format("An error occurred while initializing command %s.", cmdClass.getDeclaringClass().getSimpleName()));
return false;
}
}
LogUtils.infof("%s initialized.", StringUtils.pluralOf((int) COMMANDS_MAP.values().stream().distinct().count(), "command"));
return true;
}
use of org.reflections.scanners.TypeAnnotationsScanner in project cloudstack by apache.
the class ReflectUtil method getClassesWithAnnotation.
// Gets all classes with some annotation from a package
public static Set<Class<?>> getClassesWithAnnotation(Class<? extends Annotation> annotation, String[] packageNames) {
Reflections reflections;
Set<Class<?>> classes = new HashSet<Class<?>>();
ConfigurationBuilder builder = new ConfigurationBuilder();
for (String packageName : packageNames) {
builder.addUrls(ClasspathHelper.forPackage(packageName));
}
builder.setScanners(new SubTypesScanner(), new TypeAnnotationsScanner());
reflections = new Reflections(builder);
classes.addAll(reflections.getTypesAnnotatedWith(annotation));
return classes;
}
Aggregations