use of com.pushtorefresh.storio.common.annotations.processor.ProcessingException 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);
}
}
}
use of com.pushtorefresh.storio.common.annotations.processor.ProcessingException in project storio by pushtorefresh.
the class StorIOContentResolverProcessor method processAnnotatedFieldOrMethod.
/**
* Processes annotated field and returns result of processing or throws exception
*
* @param annotatedField field that was annotated with {@link StorIOContentResolverColumn}
* @return non-null {@link StorIOContentResolverColumnMeta} with meta information about field
*/
@NotNull
@Override
protected StorIOContentResolverColumnMeta processAnnotatedFieldOrMethod(@NotNull final Element annotatedField) {
final JavaType javaType;
try {
javaType = JavaType.from(annotatedField.getKind() == ElementKind.FIELD ? annotatedField.asType() : ((ExecutableElement) annotatedField).getReturnType());
} catch (Exception e) {
throw new ProcessingException(annotatedField, "Unsupported type of field or method for " + StorIOContentResolverColumn.class.getSimpleName() + " annotation, if you need to serialize/deserialize field of that type " + "-> please write your own resolver: " + e.getMessage());
}
final StorIOContentResolverColumn storIOContentResolverColumn = annotatedField.getAnnotation(StorIOContentResolverColumn.class);
if (storIOContentResolverColumn.ignoreNull() && annotatedField.asType().getKind().isPrimitive()) {
throw new ProcessingException(annotatedField, "ignoreNull should not be used for primitive type: " + annotatedField.getSimpleName());
}
final String columnName = storIOContentResolverColumn.name();
if (columnName.length() == 0) {
throw new ProcessingException(annotatedField, "Column name is empty: " + annotatedField.getSimpleName());
}
return new StorIOContentResolverColumnMeta(annotatedField.getEnclosingElement(), annotatedField, annotatedField.getSimpleName().toString(), javaType, storIOContentResolverColumn);
}
use of com.pushtorefresh.storio.common.annotations.processor.ProcessingException in project storio by pushtorefresh.
the class StorIOSQLiteProcessor method processAnnotatedExecutables.
/**
* Processes factory methods or constructors annotated with {@link StorIOSQLiteCreator}.
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOSQLiteType}
*/
@Override
protected void processAnnotatedExecutables(@NotNull RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, StorIOSQLiteTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOSQLiteCreator = roundEnvironment.getElementsAnnotatedWith(StorIOSQLiteCreator.class);
for (final Element annotatedElement : elementsAnnotatedWithStorIOSQLiteCreator) {
final ExecutableElement annotatedExecutableElement = (ExecutableElement) annotatedElement;
validateAnnotatedExecutable(annotatedExecutableElement);
final StorIOSQLiteCreatorMeta storIOSQLiteCreatorMeta = new StorIOSQLiteCreatorMeta(annotatedExecutableElement.getEnclosingElement(), annotatedExecutableElement, annotatedExecutableElement.getAnnotation(StorIOSQLiteCreator.class));
final StorIOSQLiteTypeMeta storIOSQLiteTypeMeta = annotatedClasses.get(storIOSQLiteCreatorMeta.enclosingElement);
// If class already contains another creator -> throw exception.
if (storIOSQLiteTypeMeta.creator == null) {
storIOSQLiteTypeMeta.creator = annotatedExecutableElement;
} else {
throw new ProcessingException(annotatedExecutableElement, "Only one creator method or constructor is allowed: " + annotatedExecutableElement.getEnclosingElement().getSimpleName());
}
}
}
use of com.pushtorefresh.storio.common.annotations.processor.ProcessingException in project storio by pushtorefresh.
the class StorIOSQLiteProcessor method processAnnotatedFieldsOrMethods.
/**
* Processes fields annotated with {@link StorIOSQLiteColumn}.
*
* @param roundEnvironment current processing environment
* @param annotatedClasses map of classes annotated with {@link StorIOSQLiteType}
*/
@Override
protected void processAnnotatedFieldsOrMethods(@NotNull final RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, StorIOSQLiteTypeMeta> annotatedClasses) {
final Set<? extends Element> elementsAnnotatedWithStorIOSQLiteColumn = roundEnvironment.getElementsAnnotatedWith(StorIOSQLiteColumn.class);
for (final Element annotatedFieldElement : elementsAnnotatedWithStorIOSQLiteColumn) {
try {
validateAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOSQLiteColumnMeta storIOSQLiteColumnMeta = processAnnotatedFieldOrMethod(annotatedFieldElement);
final StorIOSQLiteTypeMeta storIOSQLiteTypeMeta = annotatedClasses.get(storIOSQLiteColumnMeta.enclosingElement);
// If class already contains column with same name -> throw an exception.
if (storIOSQLiteTypeMeta.columns.containsKey(storIOSQLiteColumnMeta.storIOColumn.name())) {
throw new ProcessingException(annotatedFieldElement, "Column name already used in this class: " + storIOSQLiteColumnMeta.storIOColumn.name());
}
// If field annotation applied to both fields and methods in a same class.
if ((storIOSQLiteTypeMeta.needCreator && !storIOSQLiteColumnMeta.isMethod()) || (!storIOSQLiteTypeMeta.needCreator && storIOSQLiteColumnMeta.isMethod() && !storIOSQLiteTypeMeta.columns.isEmpty())) {
throw new ProcessingException(annotatedFieldElement, "Can't apply " + StorIOSQLiteColumn.class.getSimpleName() + " annotation to both fields and methods in a same class: " + storIOSQLiteTypeMeta.simpleName);
}
// If column needs creator then enclosing class needs it as well.
if (!storIOSQLiteTypeMeta.needCreator && storIOSQLiteColumnMeta.isMethod()) {
storIOSQLiteTypeMeta.needCreator = true;
}
// Put meta column info.
storIOSQLiteTypeMeta.columns.put(storIOSQLiteColumnMeta.storIOColumn.name(), storIOSQLiteColumnMeta);
} catch (SkipNotAnnotatedClassWithAnnotatedParentException e) {
messager.printMessage(WARNING, e.getMessage());
}
}
}
Aggregations