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 all 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: 4 additions & 3 deletions auto-value-moshi-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ sourceCompatibility = versions.java
targetCompatibility = versions.java

dependencies {
compile libraries.moshi

annotationProcessor libraries.incap // https://github.com/google/auto/issues/615#issuecomment-438045951
annotationProcessor project(':auto-value-moshi')
compileOnly libraries.jsr305
compileOnly project(':auto-value-moshi')
apt project(':auto-value-moshi')

compile libraries.moshi

testCompile libraries.junit
testCompile libraries.assertJ
Expand Down
6 changes: 5 additions & 1 deletion auto-value-moshi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ sourceCompatibility = versions.java
targetCompatibility = versions.java

dependencies {
annotationProcessor libraries.incapProcessor
annotationProcessor libraries.autoService
compileOnly libraries.incap
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.

3 changes: 3 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ ext {
autoService : '1.0-rc4',
autoCommon : '0.10',
moshi : '1.7.0',
incapHelper : '0.2',
jsr305 : '3.0.2',
// For testing
junit : '4.12',
Expand All @@ -24,6 +25,8 @@ ext {
autoService : "com.google.auto.service:auto-service:$versions.autoService",
autoCommon : "com.google.auto:auto-common:$versions.autoCommon",
moshi : "com.squareup.moshi:moshi:$versions.moshi",
incap : "net.ltgt.gradle.incap:incap:${versions.incapHelper}",
incapProcessor : "net.ltgt.gradle.incap:incap-processor:${versions.incapHelper}",
jsr305 : "com.google.code.findbugs:jsr305:$versions.jsr305",

junit : "junit:junit:$versions.junit",
Expand Down