use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedClass.
/**
* Processes annotated class
*
* @param classElement type element annotated with {@link StorIOContentResolverType}
* @param elementUtils utils for working with elementUtils
* @return result of processing as {@link StorIOContentResolverTypeMeta}
*/
@NotNull
@Override
protected StorIOContentResolverTypeMeta processAnnotatedClass(@NotNull TypeElement classElement, @NotNull Elements elementUtils) {
final StorIOContentResolverType storIOContentResolverType = classElement.getAnnotation(StorIOContentResolverType.class);
final String commonUri = storIOContentResolverType.uri();
final Map<String, String> urisForOperations = new HashMap<String, String>(3);
urisForOperations.put("insert", storIOContentResolverType.insertUri());
urisForOperations.put("update", storIOContentResolverType.updateUri());
urisForOperations.put("delete", storIOContentResolverType.deleteUri());
validateUris(classElement, commonUri, urisForOperations);
final String simpleName = classElement.getSimpleName().toString();
final String packageName = elementUtils.getPackageOf(classElement).getQualifiedName().toString();
return new StorIOContentResolverTypeMeta(simpleName, packageName, storIOContentResolverType);
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedExecutables.
/**
* Processes factory methods or constructors annotated with {@link StorIOContentResolverCreator}.
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOContentResolverType}
*/
@Override
protected void processAnnotatedExecutables(@NotNull RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOContentResolverCreator = roundEnvironment.getElementsAnnotatedWith(StorIOContentResolverCreator.class);
for (final Element annotatedElement : elementsAnnotatedWithStorIOContentResolverCreator) {
final ExecutableElement annotatedExecutableElement = (ExecutableElement) annotatedElement;
validateAnnotatedExecutable(annotatedExecutableElement);
final StorIOContentResolverCreatorMeta storIOContentResolverCreatorMeta = new StorIOContentResolverCreatorMeta(annotatedExecutableElement.getEnclosingElement(), annotatedExecutableElement, annotatedExecutableElement.getAnnotation(StorIOContentResolverCreator.class));
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedClasses.get(storIOContentResolverCreatorMeta.enclosingElement);
// If class already contains another creator -> throw exception.
if (storIOContentResolverTypeMeta.creator == null) {
storIOContentResolverTypeMeta.creator = annotatedExecutableElement;
} else {
throw new ProcessingException(annotatedExecutableElement, "Only one creator method or constructor is allowed: " + annotatedExecutableElement.getEnclosingElement().getSimpleName());
}
}
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedFieldsOrMethods.
/**
* Processes fields annotated with {@link StorIOContentResolverColumn}
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOContentResolverType}
*/
@Override
protected void processAnnotatedFieldsOrMethods(@NotNull final RoundEnvironment roundEnvironment, @NotNull final Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOContentResolverColumn = roundEnvironment.getElementsAnnotatedWith(StorIOContentResolverColumn.class);
for (final Element annotatedFieldElement : elementsAnnotatedWithStorIOContentResolverColumn) {
try {
validateAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOContentResolverColumnMeta storIOContentResolverColumnMeta = processAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedClasses.get(storIOContentResolverColumnMeta.enclosingElement);
// If class already contains column with same name -> throw an exception.
if (storIOContentResolverTypeMeta.columns.containsKey(storIOContentResolverColumnMeta.storIOColumn.name())) {
throw new ProcessingException(annotatedFieldElement, "Column name already used in this class: " + storIOContentResolverColumnMeta.storIOColumn.name());
}
// If field annotation applied to both fields and methods in a same class.
if ((storIOContentResolverTypeMeta.needCreator && !storIOContentResolverColumnMeta.isMethod()) || (!storIOContentResolverTypeMeta.needCreator && storIOContentResolverColumnMeta.isMethod() && !storIOContentResolverTypeMeta.columns.isEmpty())) {
throw new ProcessingException(annotatedFieldElement, "Can't apply " + StorIOContentResolverColumn.class.getSimpleName() + " annotation to both fields and methods in a same class: " + storIOContentResolverTypeMeta.simpleName);
}
// If column needs creator then enclosing class needs it as well.
if (!storIOContentResolverTypeMeta.needCreator && storIOContentResolverColumnMeta.isMethod()) {
storIOContentResolverTypeMeta.needCreator = true;
}
// Put meta column info.
storIOContentResolverTypeMeta.columns.put(storIOContentResolverColumnMeta.storIOColumn.name(), storIOContentResolverColumnMeta);
} catch (SkipNotAnnotatedClassWithAnnotatedParentException e) {
messager.printMessage(WARNING, e.getMessage());
}
}
}
use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method validateAnnotatedClassesAndColumns.
@Override
protected void validateAnnotatedClassesAndColumns(@NotNull final Map<TypeElement, StorIOContentResolverTypeMeta> annotatedClasses) {
// check that each annotated class has columns with at least one key column
for (Map.Entry<TypeElement, StorIOContentResolverTypeMeta> annotatedType : annotatedClasses.entrySet()) {
final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta = annotatedType.getValue();
if (storIOContentResolverTypeMeta.columns.isEmpty()) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation should have at least one field or method marked with " + StorIOContentResolverColumn.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
boolean hasAtLeastOneKeyColumn = false;
for (final StorIOContentResolverColumnMeta columnMeta : annotatedType.getValue().columns.values()) {
if (columnMeta.storIOColumn.key()) {
hasAtLeastOneKeyColumn = true;
break;
}
}
if (!hasAtLeastOneKeyColumn) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation should have at least one KEY field or method marked with " + StorIOContentResolverColumn.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
if (storIOContentResolverTypeMeta.needCreator && storIOContentResolverTypeMeta.creator == null) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOContentResolverCreator.class.getSimpleName() + " annotation: " + storIOContentResolverTypeMeta.simpleName);
}
if (storIOContentResolverTypeMeta.needCreator && storIOContentResolverTypeMeta.creator.getParameters().size() != storIOContentResolverTypeMeta.columns.size()) {
throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOContentResolverType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOContentResolverCreator.class.getSimpleName() + " annotation with the same amount of parameters as the number of columns: " + storIOContentResolverTypeMeta.simpleName);
}
}
}
Aggregations