Search in sources :

Example 1 with StorIOSQLiteTypeMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta in project storio by pushtorefresh.

the class StorIOSQLiteProcessor method validateAnnotatedClassesAndColumns.

@Override
protected void validateAnnotatedClassesAndColumns(@NotNull Map<TypeElement, StorIOSQLiteTypeMeta> annotatedClasses) {
    // Check that each annotated class has columns with at least one key column.
    for (final Map.Entry<TypeElement, StorIOSQLiteTypeMeta> annotatedType : annotatedClasses.entrySet()) {
        final StorIOSQLiteTypeMeta storIOSQLiteTypeMeta = annotatedType.getValue();
        if (storIOSQLiteTypeMeta.columns.size() == 0) {
            throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOSQLiteType.class.getSimpleName() + " annotation should have at least one field or method marked with " + StorIOSQLiteColumn.class.getSimpleName() + " annotation: " + storIOSQLiteTypeMeta.simpleName);
        }
        boolean hasAtLeastOneKeyColumn = false;
        for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
            if (columnMeta.storIOColumn.key()) {
                hasAtLeastOneKeyColumn = true;
                break;
            }
        }
        if (!hasAtLeastOneKeyColumn) {
            throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOSQLiteType.class.getSimpleName() + " annotation should have at least one KEY field or method marked with " + StorIOSQLiteColumn.class.getSimpleName() + " annotation: " + storIOSQLiteTypeMeta.simpleName);
        }
        if (storIOSQLiteTypeMeta.needCreator && storIOSQLiteTypeMeta.creator == null) {
            throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOSQLiteType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOSQLiteCreator.class.getSimpleName() + " annotation: " + storIOSQLiteTypeMeta.simpleName);
        }
        if (storIOSQLiteTypeMeta.needCreator && storIOSQLiteTypeMeta.creator.getParameters().size() != storIOSQLiteTypeMeta.columns.size()) {
            throw new ProcessingException(annotatedType.getKey(), "Class marked with " + StorIOSQLiteType.class.getSimpleName() + " annotation needs factory method or constructor marked with " + StorIOSQLiteCreator.class.getSimpleName() + " annotation with the same amount of parameters as the number of columns: " + storIOSQLiteTypeMeta.simpleName);
        }
    }
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) StorIOSQLiteTypeMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta) TypeElement(javax.lang.model.element.TypeElement) Map(java.util.Map) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Example 2 with StorIOSQLiteTypeMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta in project storio by pushtorefresh.

the class StorIOSQLiteProcessor method processAnnotatedClass.

/**
     * Processes annotated class.
     *
     * @param classElement type element annotated with {@link StorIOSQLiteType}
     * @param elementUtils utils for working with elementUtils
     * @return result of processing as {@link StorIOSQLiteTypeMeta}
     */
@NotNull
@Override
protected StorIOSQLiteTypeMeta processAnnotatedClass(@NotNull TypeElement classElement, @NotNull Elements elementUtils) {
    final StorIOSQLiteType storIOSQLiteType = classElement.getAnnotation(StorIOSQLiteType.class);
    final String tableName = storIOSQLiteType.table();
    if (tableName.length() == 0) {
        throw new ProcessingException(classElement, "Table name of " + classElement.getSimpleName() + " annotated with " + StorIOSQLiteType.class.getSimpleName() + " is empty");
    }
    final String simpleName = classElement.getSimpleName().toString();
    final String packageName = elementUtils.getPackageOf(classElement).getQualifiedName().toString();
    return new StorIOSQLiteTypeMeta(simpleName, packageName, storIOSQLiteType, classElement.getModifiers().contains(Modifier.ABSTRACT));
}
Also used : StorIOSQLiteTypeMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta) StorIOSQLiteType(com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with StorIOSQLiteTypeMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta 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());
        }
    }
}
Also used : StorIOSQLiteTypeMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta) StorIOSQLiteCreatorMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteCreatorMeta) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) StorIOSQLiteCreator(com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteCreator) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Example 4 with StorIOSQLiteTypeMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta 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());
        }
    }
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) StorIOSQLiteTypeMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) SkipNotAnnotatedClassWithAnnotatedParentException(com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Aggregations

ProcessingException (com.pushtorefresh.storio.common.annotations.processor.ProcessingException)4 StorIOSQLiteTypeMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta)4 TypeElement (javax.lang.model.element.TypeElement)3 StorIOSQLiteColumnMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 SkipNotAnnotatedClassWithAnnotatedParentException (com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException)1 StorIOSQLiteCreator (com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteCreator)1 StorIOSQLiteType (com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType)1 StorIOSQLiteCreatorMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteCreatorMeta)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1