Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile testing for StorIOSQLiteAnnotationsProcessor #760

Merged
merged 11 commits into from
Feb 13, 2017
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ ext.libraries = [
privateConstructorChecker : 'com.pushtorefresh.java-private-constructor-checker:checker:1.1.0',
guava : 'com.google.guava:guava:18.0',
robolectric : 'org.robolectric:robolectric:3.1.4',
googleTestingCompile : 'com.google.testing.compile:compile-testing:0.10',

dagger : 'com.google.dagger:dagger:' + daggerVersion,
daggerCompiler : 'com.google.dagger:dagger-compiler:' + daggerVersion,
Expand Down
4 changes: 3 additions & 1 deletion ci.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash
# Please run it from root project directory
./gradlew clean build checkstyle -PdisablePreDex
# For some reason test for annotation processor are failing on a regular CI setup.
# So we had to exclude test task for it from the main build process and execute it as a separate command.
./gradlew clean build checkstyle -PdisablePreDex -x :storio-sqlite-annotations-processor-test:test && ./gradlew :storio-sqlite-annotations-processor-test:testDebugUnitTest
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include ':storio-content-resolver-annotations'

include ':storio-common-annotations-processor'
include ':storio-sqlite-annotations-processor'
include ':storio-sqlite-annotations-processor-test'
include ':storio-content-resolver-annotations-processor'

include ':storio-test-without-rxjava'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static javax.lang.model.element.ElementKind.CLASS;
import static javax.lang.model.element.ElementKind.FIELD;
import static javax.lang.model.element.ElementKind.METHOD;
import static javax.lang.model.element.ElementKind.PACKAGE;
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;
Expand Down Expand Up @@ -59,7 +60,8 @@ public abstract class StorIOAnnotationsProcessor
* @return non-null unmodifiable map(element, typeMeta)
*/
@NotNull
private Map<TypeElement, TypeMeta> processAnnotatedClasses(@NotNull final RoundEnvironment roundEnvironment, @NotNull final Elements elementUtils) {
private Map<TypeElement, TypeMeta> processAnnotatedClasses(@NotNull final RoundEnvironment roundEnvironment,
@NotNull final Elements elementUtils) {
final Set<? extends Element> elementsAnnotatedWithStorIOType
= roundEnvironment.getElementsAnnotatedWith(getTypeAnnotationClass());

Expand All @@ -86,10 +88,19 @@ private TypeElement validateAnnotatedClass(@NotNull final Element annotatedEleme
// We expect here that annotatedElement is Class, annotation requires that via @Target.
final TypeElement annotatedTypeElement = (TypeElement) annotatedElement;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (annotatedTypeElement.getModifiers().contains(PRIVATE)) {
throw new ProcessingException(
annotatedElement,
getTypeAnnotationClass().getSimpleName() + " can not be applied to private class: " + annotatedTypeElement.getQualifiedName()
if (annotatedTypeElement.getKind() != CLASS) {
throw new ProcessingException(annotatedElement,
getTypeAnnotationClass().getSimpleName()
+ " can be applied only to classes not to "
+ annotatedTypeElement.getSimpleName()
);
}

if (annotatedTypeElement.getEnclosingElement().getKind() != PACKAGE) {
throw new ProcessingException(annotatedElement,
getTypeAnnotationClass().getSimpleName()
+ " can't be applied to nested or inner classes: "
+ annotatedTypeElement.getSimpleName()
);
}

Expand All @@ -108,43 +119,53 @@ protected void validateAnnotatedFieldOrMethod(@NotNull final Element annotatedEl
final Element enclosingElement = annotatedElement.getEnclosingElement();

if (enclosingElement.getKind() != CLASS) {
throw new ProcessingException(
annotatedElement,
"Please apply " + getColumnAnnotationClass().getSimpleName() + " to fields or methods of class: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
"Please apply "
+ getColumnAnnotationClass().getSimpleName()
+ " only to members of class (fields or methods) - not to members of "
+ enclosingElement.getSimpleName()
);
}

if (enclosingElement.getAnnotation(getTypeAnnotationClass()) == null) {
Element superClass = typeUtils.asElement(((TypeElement) enclosingElement).getSuperclass());
if (superClass.getAnnotation(getTypeAnnotationClass()) != null) {
throw new SkipNotAnnotatedClassWithAnnotatedParentException("Fields of classes not annotated with" + getTypeAnnotationClass().getSimpleName() +
"which have parents annotated with" + getTypeAnnotationClass().getSimpleName() + "will be skipped (e.g. AutoValue case)");
throw new SkipNotAnnotatedClassWithAnnotatedParentException("Fields of classes not annotated with "
+ getTypeAnnotationClass().getSimpleName()
+ " which have parents annotated with "
+ getTypeAnnotationClass().getSimpleName()
+ " will be skipped (e.g. AutoValue case)");
} else {
throw new ProcessingException(
annotatedElement,
"Please annotate class " + enclosingElement.getSimpleName() + " with " + getTypeAnnotationClass().getSimpleName()
throw new ProcessingException(annotatedElement,
"Please annotate class "
+ enclosingElement.getSimpleName()
+ " with "
+ getTypeAnnotationClass().getSimpleName()
);
}
}

if (annotatedElement.getModifiers().contains(PRIVATE)) {
throw new ProcessingException(
annotatedElement,
getColumnAnnotationClass().getSimpleName() + " can not be applied to private field or method: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getColumnAnnotationClass().getSimpleName()
+ " can not be applied to private field or method: "
+ annotatedElement.getSimpleName()
);
}

if (annotatedElement.getKind() == FIELD && annotatedElement.getModifiers().contains(FINAL)) {
throw new ProcessingException(
annotatedElement,
getColumnAnnotationClass().getSimpleName() + " can not be applied to final field: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getColumnAnnotationClass().getSimpleName()
+ " can not be applied to final field: "
+ annotatedElement.getSimpleName()
);
}

if (annotatedElement.getKind() == METHOD && !((ExecutableElement) annotatedElement).getParameters().isEmpty()) {
throw new ProcessingException(
annotatedElement,
getColumnAnnotationClass().getSimpleName() + " can not be applied to method with parameters: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getColumnAnnotationClass().getSimpleName()
+ " can not be applied to method with parameters: "
+ annotatedElement.getSimpleName()
);
}
}
Expand All @@ -160,37 +181,44 @@ protected void validateAnnotatedExecutable(@NotNull final ExecutableElement anno
final Element enclosingElement = annotatedElement.getEnclosingElement();

if (enclosingElement.getKind() != CLASS) {
throw new ProcessingException(
annotatedElement,
"Please apply " + getCreatorAnnotationClass().getSimpleName() + " to constructor or factory method of class: " + enclosingElement.getSimpleName()
throw new ProcessingException(annotatedElement,
"Please apply "
+ getCreatorAnnotationClass().getSimpleName()
+ " to constructor or factory method of class - not to "
+ enclosingElement.getSimpleName()
);
}

if (enclosingElement.getAnnotation(getTypeAnnotationClass()) == null) {
throw new ProcessingException(
annotatedElement,
"Please annotate class " + enclosingElement.getSimpleName() + " with " + getTypeAnnotationClass().getSimpleName()
throw new ProcessingException(annotatedElement,
"Please annotate class "
+ enclosingElement.getSimpleName()
+ " with "
+ getTypeAnnotationClass().getSimpleName()
);
}

if (annotatedElement.getModifiers().contains(PRIVATE)) {
throw new ProcessingException(
annotatedElement,
getCreatorAnnotationClass().getSimpleName() + " can not be applied to private methods or constructors: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getCreatorAnnotationClass().getSimpleName()
+ " can not be applied to private methods or constructors: "
+ annotatedElement.getSimpleName()
);
}

if (annotatedElement.getKind() == METHOD && !annotatedElement.getModifiers().contains(STATIC)) {
throw new ProcessingException(
annotatedElement,
getCreatorAnnotationClass().getSimpleName() + " can not be applied to non-static methods: " + annotatedElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getCreatorAnnotationClass().getSimpleName()
+ " can not be applied to non-static methods: "
+ annotatedElement.getSimpleName()
);
}

if (annotatedElement.getKind() == METHOD && !annotatedElement.getReturnType().equals(enclosingElement.asType())) {
throw new ProcessingException(
annotatedElement,
getCreatorAnnotationClass().getSimpleName() + " can not be applied to method with different return type from: " + enclosingElement.getSimpleName()
throw new ProcessingException(annotatedElement,
getCreatorAnnotationClass().getSimpleName()
+ " can not be applied to method with return type different from "
+ enclosingElement.getSimpleName()
);
}
}
Expand Down Expand Up @@ -267,7 +295,8 @@ public boolean process(@Nullable final Set<? extends TypeElement> annotations, @
* @param roundEnvironment current processing environment
* @param annotatedClasses map of annotated classes
*/
protected abstract void processAnnotatedFieldsOrMethods(@NotNull final RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, TypeMeta> annotatedClasses);
protected abstract void processAnnotatedFieldsOrMethods(@NotNull final RoundEnvironment roundEnvironment,
@NotNull Map<TypeElement, TypeMeta> annotatedClasses);

/**
* Processes annotated field and returns result of processing or throws exception.
Expand All @@ -284,7 +313,8 @@ public boolean process(@Nullable final Set<? extends TypeElement> annotations, @
* @param roundEnvironment current processing environment
* @param annotatedClasses map of annotated classes
*/
protected abstract void processAnnotatedExecutables(@NotNull final RoundEnvironment roundEnvironment, @NotNull Map<TypeElement, TypeMeta> annotatedClasses);
protected abstract void processAnnotatedExecutables(@NotNull final RoundEnvironment roundEnvironment,
@NotNull Map<TypeElement, TypeMeta> annotatedClasses);

protected abstract void validateAnnotatedClassesAndColumns(@NotNull Map<TypeElement, TypeMeta> annotatedClasses);

Expand Down

This file was deleted.

This file was deleted.

Loading