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

Commit

Permalink
Merge pull request #98 from vimeo/dev
Browse files Browse the repository at this point in the history
Release 2.1.3
  • Loading branch information
anthonycr authored May 2, 2017
2 parents b985f57 + 05c81b6 commit b555eb3
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 127 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

Version 2.1.3 *(2017-05-01)*
----------------------------
- Improved performance of generic type adapter instantiation.
- Fixed bug where the type adapter for a parameterized type containing a parameterized type (e.g. `Map<T, List<T>>`) wasn't being generated.

Version 2.1.2 *(2017-04-12)*
----------------------------
- Fixed a bug where `Object` fields caused type adapter generation to fail.
Expand Down
75 changes: 37 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,10 @@ The Stag library solves this problem. It leverages annotations to automatically

#### 1. Add the Stag dependencies

from jCenter
```groovy
dependencies {
compile 'com.vimeo.stag:stag-library:2.1.2'
apt 'com.vimeo.stag:stag-library-compiler:2.1.2'
}
```

or as a submodule
```groovy
dependencies {
compile project(':stag-library')
apt project(':stag-library-compiler')
}
```
All jar dependencies are available on jcenter.

#### 2. Add the Annotation Processor Plugin
### Java Gradle

In a Java project (see below for Android), apply the 'apt' plugin in your module-level `build.gradle`:
```groovy
buildscript {
repositories {
Expand All @@ -58,40 +43,54 @@ buildscript {
}
apply plugin: 'net.ltgt.apt'
dependencies {
compile 'com.vimeo.stag:stag-library:2.1.3'
apt 'com.vimeo.stag:stag-library-compiler:2.1.3'
}
// Optional annotation processor arguments (see below)
apt {
arguments {
stagGeneratedPackageName "com.vimeo.sample.stag.generated"
stagDebug true
}
}
```

In an Android project, apply the 'android-apt' plugin in your module-level `build.gradle`:
### Android Gradle

```groovy
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
dependencies {
compile 'com.vimeo.stag:stag-library:2.1.3'
annotationProcessor 'com.vimeo.stag:stag-library-compiler:2.1.3'
}
apply plugin: 'com.neenbedankt.android-apt'
android {
...
defaultConfig {
...
// Optional annotation processor arguments (see below)
javaCompileOptions {
annotationProcessorOptions {
arguments = [
stagGeneratedPackageName: 'com.vimeo.sample.stag.generated',
stagDebug : 'true'
]
}
}
}
}
```

#### 3. Provide optional compiler arguments to Stag
#### 2. Provide optional compiler arguments to Stag
- `stagGeneratedPackageName`: Pass package name as an argument for the generated files. By default, the files will be in generated
in `com.vimeo.sample.stag.generated` package. But, you can specify your own package for the generated files
by passing it as an argument to the apt compiler.
- `stagDebug`: Turn on debugging in Stag. This will cause Stag to spit out a lot of output into the gradle console.
This can aid you in figuring out what class is giving you trouble, if the exception gradle prints out
isn't sufficient.

```groovy
apt {
arguments {
stagGeneratedPackageName "com.vimeo.sample.stag.generated"
stagDebug true
}
}
```

## Features

#### 1. Class Level Annotation
Expand Down Expand Up @@ -155,7 +154,7 @@ public class Herd {

@NonNull // add NonNull annotation to throw an exception if the field is null
@SerializedName("data_list")
ArrayList<Deer> data; // data_list = json value with key "data_list"
ArrayList<Deer> data; // data = json value with key "data_list"

List<Deer> data_list_copy; // data_list_copy = json value with key "data_list_copy"

Expand Down
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.1.2'
version = '2.1.3'
}
23 changes: 10 additions & 13 deletions sample-model/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

apply plugin: 'com.neenbedankt.android-apt'

android {
// Please update the ".travis.yml" file "android.components" section
compileSdkVersion 25
Expand All @@ -21,6 +16,15 @@ android {
targetSdkVersion 25
versionCode 1
versionName "1.0"

javaCompileOptions {
annotationProcessorOptions {
arguments = [
stagGeneratedPackageName: 'com.vimeo.sample_model.stag.generated',
stagDebug : 'true'
]
}
}
}
buildTypes {
release {
Expand All @@ -37,17 +41,10 @@ dependencies {
compile 'com.android.support:support-annotations:25.3.1'

compile project(':stag-library')
apt project(':stag-library-compiler')
annotationProcessor project(':stag-library-compiler')
compile 'com.google.code.gson:gson:2.8.0'
}

apt {
arguments {
stagGeneratedPackageName "com.vimeo.sample_model.stag.generated"
stagDebug true
}
}

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:all,-deprecation,-serial,-processing" << "-Werror"
Expand Down
4 changes: 2 additions & 2 deletions sample-model/src/test/java/com/vimeo/sample_model/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static <T> TypeAdapter<T> getTypeAdapter(@NonNull Class<T> clazz) {
*/
public static <T> void verifyTypeAdapterGeneration(@NonNull Class<T> clazz) throws Exception {
TypeAdapter<T> typeAdapter = getTypeAdapter(clazz);
assertNotNull("Type adapter shouldn't have been generated by Stag", typeAdapter);
assertNotNull("Type adapter should have been generated by Stag", typeAdapter);
}

/**
Expand All @@ -50,7 +50,7 @@ public static <T> void verifyTypeAdapterGeneration(@NonNull Class<T> clazz) thro
*/
public static <T> void verifyNoTypeAdapterGeneration(@NonNull Class<T> clazz) throws Exception {
TypeAdapter<T> typeAdapter = getTypeAdapter(clazz);
assertNull("Type adapter should have been generated by Stag", typeAdapter);
assertNull("Type adapter should not have been generated by Stag", typeAdapter);
}

}
23 changes: 10 additions & 13 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,8 @@ buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}

apply plugin: 'com.neenbedankt.android-apt'

android {
// Please update the ".travis.yml" file "android.components" section
compileSdkVersion 25
Expand All @@ -23,6 +18,15 @@ android {
targetSdkVersion 25
versionCode 1
versionName "1.0"

javaCompileOptions {
annotationProcessorOptions {
arguments = [
stagGeneratedPackageName: 'com.vimeo.sample.stag.generated',
stagDebug : 'true'
]
}
}
}
buildTypes {
release {
Expand All @@ -42,17 +46,10 @@ dependencies {
compile project(':stag-library')
compile project(':sample-model')
compile project(':sample-java-model')
apt project(':stag-library-compiler')
annotationProcessor project(':stag-library-compiler')
compile 'com.google.code.gson:gson:2.8.0'
}

apt {
arguments {
stagGeneratedPackageName "com.vimeo.sample.stag.generated"
stagDebug true
}
}

gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:all,-deprecation,-serial,-processing" << "-Werror"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* The MIT License (MIT)
* <p/>
* Copyright (c) 2016 Vimeo
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p/>
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* <p/>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.vimeo.sample.model;


import com.vimeo.stag.UseStag;

/**
* Complex generic class which specifies the parametrised type in the base class
*
*/
@UseStag
public class ComplexGenericClassExtended extends ComplexGenericClass<Video> {

}
3 changes: 2 additions & 1 deletion sample/src/main/java/com/vimeo/sample/model1/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.google.gson.annotations.SerializedName;

/**
* This model does not use stag
* This model does not use stag.
* As it is not of paramtereized type, this will use TypeToken.get()
*/
public class Data {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.vimeo.sample.model1;

import com.google.gson.annotations.SerializedName;

import java.util.List;

/**
* Since this class is not annotated with @UseStag, this will use TypeToken for its adapter generation.
* As it is of paramtereized type, this will use new TypeToken<>(){} instead of TypeToken.get()
* @param <T>
*/
public class ParameterizedData<T> {

@SerializedName("list")
public List<T> list;
}
3 changes: 3 additions & 0 deletions sample/src/main/java/com/vimeo/sample/model1/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ public class Video {
@NonNull
@SerializedName("mData")
public Data mData;

@SerializedName("mParameterizedData")
public ParameterizedData<String> mParameterizedData;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.vimeo.sample.model;

import com.vimeo.sample.Utils;

import org.junit.Test;

public class ComplexGenericClassExtendedTest {

@Test
public void typeAdapterWasGenerated() throws Exception {
Utils.verifyTypeAdapterGeneration(ComplexGenericClassExtended.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,21 @@ private TypeSpec getAdapterFactorySpec() {
if (null != knownTypeAdapterForType) {
fieldName += knownTypeAdapterForType;
} else {
getAdapterMethodBuilder.addStatement(
fieldName + " = gson.getAdapter(new TypeToken<" + classInfo.getType().toString() +
">(){})");
String typeTokenCode;
if (!TypeUtils.isParameterizedType(classInfo.getType())) {
/*
* If the type is not of parameterized type, use TypeToken.get() call for creating a typetoken
* object. This method call avoids calling the getSuperClass calls which uses reflection
*/
typeTokenCode = "TypeToken.get(" + classInfo.getType().toString() + ".class)";
} else {
/*
* If the type is of parameterized type, use the normal way of creating typetokens
*/
typeTokenCode = "new TypeToken<" + classInfo.getType().toString() +
">(){}";
}
getAdapterMethodBuilder.addStatement(fieldName + " = gson.getAdapter(" + typeTokenCode + ")");
}
getAdapterMethodBuilder.endControlFlow();
getAdapterMethodBuilder.addStatement("return " + fieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private static String getTypeTokenCodeForGenericType(@NotNull TypeMirror fieldTy

String result = null;
if (fieldType.getKind() == TypeKind.TYPEVAR) {
result = " com.google.gson.reflect.TypeToken.get(" + typeVarsMap.get(fieldType) + ")";
result = "com.google.gson.reflect.TypeToken.get(" + typeVarsMap.get(fieldType) + ")";
} else if (fieldType instanceof DeclaredType) {
/*
* If it is of ParameterizedType, {@link com.vimeo.stag.utils.ParameterizedTypeUtil} is used to get the
Expand Down Expand Up @@ -332,15 +332,18 @@ private static void runIfAnnotationSupported(@NotNull List<? extends AnnotationM
}
}

private static String getFieldAccessorForKnownJsonAdapterType(@NotNull ExecutableElement adapterType, @NotNull TypeSpec.Builder adapterBuilder,
private static String getFieldAccessorForKnownJsonAdapterType(@NotNull ExecutableElement adapterType,
@NotNull TypeSpec.Builder adapterBuilder,
@NotNull MethodSpec.Builder constructorBuilder,
@NotNull TypeMirror fieldType,
@NotNull TypeUtils.JsonAdapterType jsonAdapterType, @NotNull AdapterFieldInfo adapterFieldInfo, boolean isNullSafe,
@NotNull TypeUtils.JsonAdapterType jsonAdapterType,
@NotNull AdapterFieldInfo adapterFieldInfo,
boolean isNullSafe,
@NotNull String keyFieldName) {
String fieldAdapterAccessor = "new " + FileGenUtils.escapeStringForCodeBlock(adapterType.getEnclosingElement().toString());
if (jsonAdapterType == TypeUtils.JsonAdapterType.TYPE_ADAPTER) {
ArrayList<String> constructorParameters = new ArrayList<>();
if (adapterType.getParameters().size() > 0) {
if (!adapterType.getParameters().isEmpty()) {
for (VariableElement parameter : adapterType.getParameters()) {
if (parameter.asType().toString().equals(TypeUtils.className(Gson.class))) {
constructorParameters.add("gson");
Expand Down
Loading

0 comments on commit b555eb3

Please sign in to comment.