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

Switch to incap helper #114

Merged
merged 10 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion auto-value-moshi/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
apply plugin: 'net.ltgt.apt'
Copy link

Choose a reason for hiding this comment

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

You should revert that change, and use annotationProcessor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So when I use that, it fails on CI due to not finding generated classes - https://travis-ci.org/rharter/auto-value-moshi/builds/456470849?utm_source=github_status&utm_medium=notification

I tried this just in case, and sure enough it did seem to fix it. Any sense on why it only happens in CI?

Copy link

Choose a reason for hiding this comment

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

That was because auto-service wasn't in the processor path anymore at that commit.

At a minimum you could revert the annotationProcessorapt change, whether using net.ltgt.apt or not (it will setup options.annotationProcessorGeneratedSourcesDirectory, but this is useless here as those processors only generate resources; see the project's README to determine whether it's useful for you or not, in this specific case, it's not).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceCompatibility = versions.java
targetCompatibility = versions.java

dependencies {
apt "net.ltgt.gradle.incap:incap-processor:0.2"
Copy link

Choose a reason for hiding this comment

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

Move to dependencies.gradle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Will do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

apt libraries.autoService
compileOnly "net.ltgt.gradle.incap:incap:0.2"
compileOnly libraries.autoService

compile project(':auto-value-moshi-annotations')
compile libraries.javaPoet
compile libraries.autoValue
compile libraries.autoValueAnnotations
compileOnly libraries.autoService
compile libraries.autoCommon
compile libraries.moshi

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import net.ltgt.gradle.incap.IncrementalAnnotationProcessor;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
Expand Down Expand Up @@ -46,13 +48,15 @@
import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PUBLIC;
import static javax.tools.Diagnostic.Kind.ERROR;
import static net.ltgt.gradle.incap.IncrementalAnnotationProcessorType.AGGREGATING;

/**
* Annotation Processor responsible for the generation of the {@link JsonAdapter.Factory} class
* annotated with {@link MoshiAdapterFactory}.
*
* @author rharter
*/
@IncrementalAnnotationProcessor(AGGREGATING)
@AutoService(Processor.class)
public class AutoValueMoshiAdapterFactoryProcessor extends AbstractProcessor {
private static final ParameterSpec TYPE_SPEC = ParameterSpec.builder(Type.class, "type").build();
Expand Down Expand Up @@ -113,7 +117,10 @@ public class AutoValueMoshiAdapterFactoryProcessor extends AbstractProcessor {
MoshiAdapterFactory annotation = element.getAnnotation(MoshiAdapterFactory.class);
boolean requestNullSafeAdapters = annotation.nullSafe();

TypeSpec jsonAdapterFactory = createJsonAdapterFactory(elements, packageName, adapterName,
TypeSpec jsonAdapterFactory = createJsonAdapterFactory(type,
elements,
packageName,
adapterName,
requestNullSafeAdapters);
JavaFile file = JavaFile.builder(packageName, jsonAdapterFactory).build();
try {
Expand All @@ -129,10 +136,14 @@ public class AutoValueMoshiAdapterFactoryProcessor extends AbstractProcessor {
return false;
}

private TypeSpec createJsonAdapterFactory(List<Element> elements, String packageName,
String factoryName, boolean requestNullSafeAdapters) {
private TypeSpec createJsonAdapterFactory(TypeElement sourceElement,
List<Element> elements,
String packageName,
String factoryName,
boolean requestNullSafeAdapters) {
TypeSpec.Builder factory =
TypeSpec.classBuilder(ClassName.get(packageName, "AutoValueMoshi_" + factoryName));
factory.addOriginatingElement(sourceElement);
factory.addModifiers(PUBLIC, FINAL);
factory.superclass(ClassName.get(packageName, factoryName));

Expand All @@ -157,6 +168,7 @@ private TypeSpec createJsonAdapterFactory(List<Element> elements, String package

for (int i = 0; i < elements.size(); i++) {
Element element = elements.get(i);
factory.addOriginatingElement(element);
TypeName elementTypeName = TypeName.get(element.asType());

if (elementTypeName instanceof ParameterizedTypeName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.squareup.moshi.Types;

import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
Expand Down Expand Up @@ -112,6 +113,11 @@ private ImmutableSet<String> buildAnnotations(ExecutableElement element) {
}
}

@Override
public IncrementalExtensionType incrementalType(ProcessingEnvironment processingEnvironment) {
return IncrementalExtensionType.ISOLATING;
}

@Override public boolean applicable(Context context) {
// check that the class contains a public static method returning a JsonAdapter
TypeElement type = context.autoValueClass();
Expand Down

This file was deleted.