Search in sources :

Example 1 with SkipNotAnnotatedClassWithAnnotatedParentException

use of com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException 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());
        }
    }
}
Also used : StorIOContentResolverTypeMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta) 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) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Example 2 with SkipNotAnnotatedClassWithAnnotatedParentException

use of com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException 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)2 SkipNotAnnotatedClassWithAnnotatedParentException (com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 TypeElement (javax.lang.model.element.TypeElement)2 StorIOContentResolverColumnMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta)1 StorIOContentResolverTypeMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta)1 StorIOSQLiteColumnMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta)1 StorIOSQLiteTypeMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta)1