use of io.swagger.jaxrs.config.ReaderListener in project swagger-core by swagger-api.
the class Reader method read.
/**
* Scans a set of classes for both ReaderListeners and Swagger annotations. All found listeners will
* be instantiated before any of the classes are scanned for Swagger annotations - so they can be invoked
* accordingly.
*
* @param classes a set of classes to scan
* @return the generated Swagger definition
*/
public Swagger read(Set<Class<?>> classes) {
Set<Class<?>> sortedClasses = new TreeSet<Class<?>>(new Comparator<Class<?>>() {
@Override
public int compare(Class<?> class1, Class<?> class2) {
if (class1.equals(class2)) {
return 0;
} else if (class1.isAssignableFrom(class2)) {
return -1;
} else if (class2.isAssignableFrom(class1)) {
return 1;
}
return class1.getName().compareTo(class2.getName());
}
});
sortedClasses.addAll(classes);
Map<Class<?>, ReaderListener> listeners = new HashMap<Class<?>, ReaderListener>();
for (Class<?> cls : sortedClasses) {
if (ReaderListener.class.isAssignableFrom(cls) && !listeners.containsKey(cls)) {
try {
listeners.put(cls, (ReaderListener) cls.newInstance());
} catch (Exception e) {
LOGGER.error("Failed to create ReaderListener", e);
}
}
}
for (ReaderListener listener : listeners.values()) {
try {
listener.beforeScan(this, swagger);
} catch (Exception e) {
LOGGER.error("Unexpected error invoking beforeScan listener [" + listener.getClass().getName() + "]", e);
}
}
// process SwaggerDefinitions first - so we get tags in desired order
for (Class<?> cls : sortedClasses) {
SwaggerDefinition swaggerDefinition = cls.getAnnotation(SwaggerDefinition.class);
if (swaggerDefinition != null) {
readSwaggerConfig(cls, swaggerDefinition);
}
}
for (Class<?> cls : sortedClasses) {
read(cls, "", null, false, new String[0], new String[0], new LinkedHashMap<String, Tag>(), new ArrayList<Parameter>(), new HashSet<Class<?>>());
}
for (ReaderListener listener : listeners.values()) {
try {
listener.afterScan(this, swagger);
} catch (Exception e) {
LOGGER.error("Unexpected error invoking afterScan listener [" + listener.getClass().getName() + "]", e);
}
}
return swagger;
}
Aggregations