下面列出了怎么用javax.servlet.annotation.HandlesTypes的API类实例代码及写法,或者点击链接到github查看源代码。
private void registerHandledTypes(ScanResult scanResult, Class<?> initializerClass, Map<Class<?>, Set<Class<?>>> map) {
HandlesTypes handledTypes = initializerClass.getAnnotation(HandlesTypes.class);
if (handledTypes != null) {
Function<Class<?>, ClassInfoList> classFinder = type -> {
if (type.isAnnotation()) {
return scanResult.getClassesWithAnnotation(type.getCanonicalName());
} else if (type.isInterface()) {
return scanResult.getClassesImplementing(type.getCanonicalName());
} else {
return scanResult.getSubclasses(type.getCanonicalName());
}
};
Set<Class<?>> classes = Stream.of(handledTypes.value())
.map(classFinder)
.flatMap(c -> c.loadClasses().stream())
.collect(Collectors.toSet());
if (!classes.isEmpty()) {
map.put(initializerClass, classes);
}
}
}
private Stream<Class<?>> getValidationAnnotations() {
return Stream.concat(
Stream.of(AnnotationValidator.class
.getAnnotation(HandlesTypes.class).value()),
Stream.of(WebComponentExporterAwareValidator.class
.getAnnotation(HandlesTypes.class).value()));
}
@Nullable
protected Set<Class<?>> performClasspathScan() {
HandlesTypes handlesTypes = AnnotationUtils.findAnnotation(getServletContainerInitializerClass(), HandlesTypes.class);
if (handlesTypes == null) {
return null;
}
Class<?>[] handledTypes = handlesTypes.value();
if (handledTypes.length == 0) {
return null;
}
StopWatch stopWatch = new StopWatch(getServletContainerInitializerClass().getName());
stopWatch.start("prepare");
ClassGraph classGraph = new ClassGraph()
.enableClassInfo();
// Only scan for Annotations if we have to
if (Arrays.stream(handledTypes).anyMatch(Class::isAnnotation)) {
classGraph = classGraph.enableAnnotationInfo();
}
classGraph = classGraph.enableExternalClasses()
.enableSystemJarsAndModules() // Find classes in javax.faces
.blacklistPackages("java", "jdk", "sun", "javafx", "oracle")
.blacklistPackages("javax.xml", "javax.el", "javax.persistence")
.blacklistModules("java.*", "jdk.*")
.filterClasspathElements(path -> {
log.debug("Path {}", path);
return true;
});
Set<Class<?>> classes = new HashSet<>();
stopWatch.stop();
stopWatch.start("classpath scan");
try (ScanResult scanResult = classGraph.scan()) {
stopWatch.stop();
stopWatch.start("collect results");
for (Class<?> handledType : handledTypes) {
ClassInfoList classInfos;
if (handledType.isAnnotation()) {
classInfos = scanResult.getClassesWithAnnotation(handledType.getName());
}
else if (handledType.isInterface()) {
classInfos = scanResult.getClassesImplementing(handledType.getName());
}
else {
classInfos = scanResult.getSubclasses(handledType.getName());
}
classes.addAll(classInfos.loadClasses());
}
handleScanResult(scanResult);
}
finally {
stopWatch.stop();
log.info("Resolving classes for {} took {}s", getServletContainerInitializerClass().getName(), stopWatch.getTotalTimeSeconds());
if (log.isDebugEnabled()) {
log.debug(stopWatch.prettyPrint());
}
}
return classes.isEmpty() ? null : classes;
}
@Override
public List<Class<?>> getAnnotations() {
return Arrays.asList(
this.getClass().getAnnotation(HandlesTypes.class).value());
}
private static Set<String> calculateApplicableClassNames() {
HandlesTypes handlesTypes = DevModeInitializer.class
.getAnnotation(HandlesTypes.class);
return Stream.of(handlesTypes.value()).map(Class::getName)
.collect(Collectors.toSet());
}
@Override
public List<Class<?>> getAnnotations() {
return Arrays.asList(
this.getClass().getAnnotation(HandlesTypes.class).value());
}
private Collection<Class<?>> getApplicableClasses() {
HandlesTypes handlesTypes = DevModeInitializer.class
.getAnnotation(HandlesTypes.class);
return Stream.of(handlesTypes.value()).collect(Collectors.toList());
}