import com.contentful.vault.Space; //导入依赖的package包/类
private void parseSpace(TypeElement element, Map<TypeElement, SpaceInjection> spaces,
Map<TypeElement, ModelInjection> models) {
Space annotation = element.getAnnotation(Space.class);
String id = annotation.value();
if (id.isEmpty()) {
error(element, "@%s id may not be empty. (%s)",
Space.class.getSimpleName(),
element.getQualifiedName());
return;
}
TypeMirror spaceMirror = elementUtils.getTypeElement(Space.class.getName()).asType();
List<ModelInjection> includedModels = new ArrayList<>();
for (AnnotationMirror mirror : element.getAnnotationMirrors()) {
if (typeUtils.isSameType(mirror.getAnnotationType(), spaceMirror)) {
Set<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> items =
mirror.getElementValues().entrySet();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : items) {
if ("models".equals(entry.getKey().getSimpleName().toString())) {
List l = (List) entry.getValue().getValue();
if (l.size() == 0) {
error(element, "@%s models must not be empty. (%s)",
Space.class.getSimpleName(),
element.getQualifiedName());
return;
}
Set<String> modelIds = new LinkedHashSet<>();
for (Object model : l) {
TypeElement e = (TypeElement) ((Type) ((Attribute) model).getValue()).asElement();
ModelInjection modelInjection = models.get(e);
if (modelInjection == null) {
return;
} else {
String rid = modelInjection.remoteId;
if (!modelIds.add(rid)) {
error(element, "@%s includes multiple models with the same id \"%s\". (%s)",
Space.class.getSimpleName(), rid, element.getQualifiedName());
return;
}
includedModels.add(modelInjection);
}
}
}
}
}
}
List<String> locales = Arrays.asList(annotation.locales());
Set<String> checked = new HashSet<>();
for (int i = locales.size() - 1; i >= 0; i--) {
String code = locales.get(i);
if (!checked.add(code)) {
error(element, "@%s contains duplicate locale code '%s'. (%s)",
Space.class.getSimpleName(), code, element.getQualifiedName());
return;
} else if (code.contains(" ") || code.isEmpty()) {
error(element, "Invalid locale code '%s', must not be empty and may not contain spaces. (%s)",
code, element.getQualifiedName());
return;
}
}
if (checked.size() == 0) {
error(element, "@%s at least one locale must be configured. (%s)",
Space.class.getSimpleName(), element.getQualifiedName());
return;
}
ClassName injectionClassName = getInjectionClassName(element, SUFFIX_SPACE);
String dbName = "space_" + SqliteUtils.hashForId(id);
String copyPath = StringUtils.defaultIfBlank(annotation.copyPath(), null);
spaces.put(element, new SpaceInjection(id, injectionClassName, element, includedModels, dbName,
annotation.dbVersion(), copyPath, locales));
}
请发表评论