Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Fixed backwards incompatibility, fixed nested classes always defaulti…
Browse files Browse the repository at this point in the history
…ng to fieldoption.all
  • Loading branch information
anthonycr committed Jan 30, 2017
1 parent 1efd1d9 commit 88ab69f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 19 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ allprojects {

subprojects {
group = 'com.vimeo.stag'
version = '2.0.0'
version = '2.0.1'
}
1 change: 1 addition & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
apt {
arguments {
stagGeneratedPackageName "com.vimeo.sample.stag.generated"
stagDebug true
}
}

Expand Down
29 changes: 28 additions & 1 deletion sample/src/main/java/com/vimeo/sample/model/NestedClass.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
package com.vimeo.sample.model;

import com.google.gson.annotations.SerializedName;
import com.vimeo.stag.UseStag;
import com.vimeo.stag.UseStag.FieldOption;

@UseStag
public class NestedClass {

public static class Nested extends NestedClass {}
/**
* This class should have a TypeAdapter created
* for it with FieldOption.ALL
*/
public static class Nested extends NestedClass {

}

@UseStag(FieldOption.SERIALIZED_NAME)
public static class NestedWithAnnotation {

/**
* This class should have a TypeAdapter created
* for it with FieldOption.SERIALIZED_NAME
*/
public static class NestedNestedWithoutAnnotation {

// won't be picked up by stag because it inherits the parent annotation
protected String field;

@SerializedName("name")
protected String field1;

}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.vimeo.sample.model;

import com.vimeo.stag.GsonAdapterKey;

/**
* This class should have a TypeAdapter created for it
* for backwards compatibility.
* <p>
* Created by restainoa on 1/30/17.
*/
public class NoUseStagAnnotation {

@GsonAdapterKey
protected String field;

}
17 changes: 17 additions & 0 deletions sample/src/test/java/com/vimeo/sample/NoUseStagAnnotationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vimeo.sample;

import com.vimeo.sample.model.NoUseStagAnnotation;

import org.junit.Test;

/**
* Created by restainoa on 1/30/17.
*/
public class NoUseStagAnnotationTest {

@Test
public void typeAdapterWasNotGenerated() throws Exception {
Utils.verifyNoTypeAdapterGeneration(NoUseStagAnnotation.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package com.vimeo.sample;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.vimeo.sample.model.OuterClassWithInnerModel;
import com.vimeo.sample.stag.generated.Stag;

import org.junit.Test;

import static org.junit.Assert.assertNotNull;

public class OuterClassWithInnerModelTest {

@Test
public void typeAdapterWasGenerated() throws Exception {
Gson gson = new Gson();
Stag.Factory factory = new Stag.Factory();
TypeToken<OuterClassWithInnerModel.InnerModel> innerModelType =
TypeToken.get(OuterClassWithInnerModel.InnerModel.class);
TypeAdapter<OuterClassWithInnerModel.InnerModel> adapter = factory.create(gson, innerModelType);
assertNotNull("Type adapter was not generated by Stag", adapter);
public void innerTypeAdapterWasGenerated() throws Exception {
Utils.verifyTypeAdapterGeneration(OuterClassWithInnerModel.InnerModel.class);
}

@Test
public void outerTypeAdapterWasNotGenerated() throws Exception {
Utils.verifyNoTypeAdapterGeneration(OuterClassWithInnerModel.class);
}

}
54 changes: 54 additions & 0 deletions sample/src/test/java/com/vimeo/sample/Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.vimeo.sample;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.vimeo.sample.stag.generated.Stag;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
* Utils class for testing type adapter generation.
*
* Created by restainoa on 1/30/17.
*/
public final class Utils {

private Utils() {
}

private static <T> TypeAdapter<T> getTypeAdapter(@NonNull Class<T> clazz) {
Gson gson = new Gson();
Stag.Factory factory = new Stag.Factory();
TypeToken<T> innerModelType = TypeToken.get(clazz);
return factory.create(gson, innerModelType);
}

/**
* Verifies that a TypeAdapter was generated for the specified class.
*
* @param clazz the class to check.
* @param <T> the type of the class, used internally.
* @throws Exception throws an exception if an adapter was not generated.
*/
public static <T> void verifyTypeAdapterGeneration(@NonNull Class<T> clazz) throws Exception {
TypeAdapter<T> typeAdapter = getTypeAdapter(clazz);
assertNotNull("Type adapter was not generated by Stag", typeAdapter);
}

/**
* Verifies that a TypeAdapter was NOT generated for the specified class.
*
* @param clazz the class to check.
* @param <T> the type of the class, used internally.
* @throws Exception throws an exception if an adapter was generated.
*/
public static <T> void verifyNoTypeAdapterGeneration(@NonNull Class<T> clazz) throws Exception {
TypeAdapter<T> typeAdapter = getTypeAdapter(clazz);
assertNull("Type adapter was not generated by Stag", typeAdapter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.auto.service.AutoService;
import com.squareup.javapoet.JavaFile;
import com.vimeo.stag.GsonAdapterKey;
import com.vimeo.stag.UseStag;
import com.vimeo.stag.processor.generators.AdapterGenerator;
import com.vimeo.stag.processor.generators.EnumTypeAdapterGenerator;
Expand Down Expand Up @@ -54,6 +55,7 @@
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;


Expand Down Expand Up @@ -106,14 +108,27 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

mHasBeenProcessed = true;

// Pick up the classes annotated with @UseStag
Set<? extends Element> useStagElements = roundEnv.getElementsAnnotatedWith(UseStag.class);
for (Element useStagElement : useStagElements)
{
for (Element useStagElement : useStagElements) {
TypeMirror rootType = useStagElement.asType();
DebugLog.log("Annotated type: " + rootType + "\n");
SupportedTypesModel.getInstance().getSupportedType(rootType);
}

// Pick up classes that contain @GsonAdapterKey annotations for backwards compatibility
// TODO: Remove this code when we remove @GsonAdapterKey support 1/30/17 [AR]
Set<? extends Element> gsonAdapterKeyElements =
roundEnv.getElementsAnnotatedWith(GsonAdapterKey.class);
for (Element gsonAdapterKeyElement : gsonAdapterKeyElements) {
final VariableElement variableElement = (VariableElement) gsonAdapterKeyElement;

Element enclosingClassElement = variableElement.getEnclosingElement();
TypeMirror enclosingClass = enclosingClassElement.asType();
DebugLog.log("Annotated type: " + enclosingClass + "\n");
SupportedTypesModel.getInstance().getSupportedType(enclosingClass);
}

Filer filer = processingEnv.getFiler();
try {
Set<TypeMirror> mSupportedTypes = SupportedTypesModel.getInstance().getSupportedTypesMirror();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.vimeo.stag.processor.utils.TypeUtils;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -68,10 +69,15 @@ public class AnnotatedClass {
TypeMirror inheritedType = TypeUtils.getInheritedType(element);

UseStag useStag = element.getAnnotation(UseStag.class);
FieldOption fieldOption = useStag == null ? FieldOption.ALL : useStag.value();

mMemberVariables = new LinkedHashMap<>();
FieldOption fieldOption = useStag != null ? useStag.value() : null;
if (fieldOption == null) {
useStag = findParentUseStagAnnotation(element);
// TODO: Do not default to FieldOption.SERIALIZED_NAME, instead set to null and fail after removing @GsonAdapterKey 1/30/17 [AR]
fieldOption = useStag != null ? useStag.value() : FieldOption.SERIALIZED_NAME;
}

mMemberVariables = new LinkedHashMap<>();

if (inheritedType != null) {
if (StagProcessor.DEBUG) {
Expand All @@ -97,6 +103,19 @@ public class AnnotatedClass {

}

@Nullable
private static UseStag findParentUseStagAnnotation(@NotNull Element element) {
Element parent = element.getEnclosingElement();
if (parent == null) {
return null;
}
UseStag useStag = parent.getAnnotation(UseStag.class);
if (useStag != null) {
return useStag;
}
return findParentUseStagAnnotation(parent);
}

private void addMemberVariable(@NotNull Element element, @NotNull TypeMirror typeMirror,
@NotNull Map<String, Element> variableNames) {
Element previousElement = variableNames.put(element.getSimpleName().toString(), element);
Expand Down

0 comments on commit 88ab69f

Please sign in to comment.