Search in sources :

Example 1 with StorIOContentResolverTypeMeta

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);
}
Also used : StorIOContentResolverTypeMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta) HashMap(java.util.HashMap) StorIOContentResolverType(com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with StorIOContentResolverTypeMeta

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());
        }
    }
}
Also used : StorIOContentResolverTypeMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta) StorIOContentResolverCreatorMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverCreatorMeta) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) ExecutableElement(javax.lang.model.element.ExecutableElement) StorIOContentResolverCreator(com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverCreator) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Example 3 with StorIOContentResolverTypeMeta

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());
        }
    }
}
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 4 with StorIOContentResolverTypeMeta

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);
        }
    }
}
Also used : StorIOContentResolverTypeMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta) TypeElement(javax.lang.model.element.TypeElement) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) HashMap(java.util.HashMap) Map(java.util.Map) ProcessingException(com.pushtorefresh.storio.common.annotations.processor.ProcessingException)

Aggregations

StorIOContentResolverTypeMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta)4 ProcessingException (com.pushtorefresh.storio.common.annotations.processor.ProcessingException)3 TypeElement (javax.lang.model.element.TypeElement)3 StorIOContentResolverColumnMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta)2 HashMap (java.util.HashMap)2 Element (javax.lang.model.element.Element)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2 SkipNotAnnotatedClassWithAnnotatedParentException (com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException)1 StorIOContentResolverCreator (com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverCreator)1 StorIOContentResolverType (com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverType)1 StorIOContentResolverCreatorMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverCreatorMeta)1 Map (java.util.Map)1 NotNull (org.jetbrains.annotations.NotNull)1