From 52f5f1f235e782790095c981d63ad4b6369a6d98 Mon Sep 17 00:00:00 2001 From: Hadi Satrio Date: Thu, 7 Sep 2017 16:02:27 +0700 Subject: [PATCH 1/5] Desalinate parameterized types when generating factories --- .../viewmodelprovider/GeneratedProviderProcessor.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java b/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java index b8530f7..587e46c 100644 --- a/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java +++ b/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java @@ -225,7 +225,13 @@ private void generateFactory(TypeElement typeElement) final StringBuilder fieldTypesCsv = new StringBuilder(); final StringBuilder fieldNamesCsv = new StringBuilder(); for (int i = 0; i < ctorParams.size(); i++) { - final TypeName paramType = TypeName.get(ctorParams.get(i).getLeft()); + TypeName paramType = TypeName.get(ctorParams.get(i).getLeft()); + + // Type-erasure. Not doing this will break factory-generation for + // targets with parameterized type constructor params. + if (paramType instanceof ParameterizedTypeName) { + paramType = ((ParameterizedTypeName) paramType).rawType; + } fieldSpecs.add( FieldSpec.builder( From 155fa0f75ee42f8d6d30ea8566738fa30687c0aa Mon Sep 17 00:00:00 2001 From: Hadi Satrio Date: Thu, 7 Sep 2017 16:06:12 +0700 Subject: [PATCH 2/5] Include parameterized types usage scenario in sample --- .../apps/android/alfreddemo/AnotherDopeViewModel.java | 9 +++++++-- .../apps/android/alfreddemo/SeeAlfredInAction.java | 4 +++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/AnotherDopeViewModel.java b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/AnotherDopeViewModel.java index 8e184a4..bbc6458 100644 --- a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/AnotherDopeViewModel.java +++ b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/AnotherDopeViewModel.java @@ -22,21 +22,26 @@ import com.hadisatrio.libs.android.viewmodelprovider.GeneratedProvider; import com.hadisatrio.libs.android.viewmodelprovider.Main; +import java.util.Collections; +import java.util.List; + @GeneratedProvider public final class AnotherDopeViewModel extends ViewModel { private final Context context; private final Long fucksGiven; private final String whatNot; + private final List someNumbers; public AnotherDopeViewModel(Context context, Long fucksGiven) { - this(context, fucksGiven, ""); + this(context, fucksGiven, "", Collections.emptyList()); } @Main - public AnotherDopeViewModel(Context context, Long fucksGiven, String whatNot) { + public AnotherDopeViewModel(Context context, Long fucksGiven, String whatNot, List someNumbers) { this.context = context; this.fucksGiven = fucksGiven; this.whatNot = whatNot; + this.someNumbers = someNumbers; } } diff --git a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/SeeAlfredInAction.java b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/SeeAlfredInAction.java index d9c2fb6..55e9220 100644 --- a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/SeeAlfredInAction.java +++ b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/SeeAlfredInAction.java @@ -20,6 +20,8 @@ import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import java.util.Arrays; + public final class SeeAlfredInAction extends AppCompatActivity { private DopeViewModel dopeViewModel; @@ -35,7 +37,7 @@ protected void onCreate(Bundle savedInstanceState) { // These are dope... dopeViewModel = DopeViewModelProvider.get(this, this, 0L); - anotherDopeViewModel = AnotherDopeViewModelProvider.get(this, this, 0L, ""); + anotherDopeViewModel = AnotherDopeViewModelProvider.get(this, this, 0L, "", Arrays.asList(1, 2, 3)); // ...this is lame.. lameViewModel = ViewModelProviders.of(this, new CustomViewModelFactory(this, 0L)).get(LameViewModel.class); From f7195a1811e771d4ee9ae0e6cb6f8108c9db8434 Mon Sep 17 00:00:00 2001 From: Ruben Gees Date: Sun, 10 Sep 2017 22:17:26 +0200 Subject: [PATCH 3/5] Make the generated ViewModelFactories more performant --- .../GeneratedProviderProcessor.java | 14 +------------- .../android/alfreddemo/CustomViewModelFactory.java | 2 +- gradle/versions.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 4 files changed, 4 insertions(+), 16 deletions(-) diff --git a/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java b/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java index 587e46c..a058016 100644 --- a/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java +++ b/compiler/src/main/java/com/hadisatrio/libs/android/viewmodelprovider/GeneratedProviderProcessor.java @@ -222,7 +222,6 @@ private void generateFactory(TypeElement typeElement) // Define the fields based on previously queried constructor params. final List fieldSpecs = new ArrayList<>(); - final StringBuilder fieldTypesCsv = new StringBuilder(); final StringBuilder fieldNamesCsv = new StringBuilder(); for (int i = 0; i < ctorParams.size(); i++) { TypeName paramType = TypeName.get(ctorParams.get(i).getLeft()); @@ -242,9 +241,6 @@ private void generateFactory(TypeElement typeElement) ).build() ); - if (fieldTypesCsv.length() > 0) fieldTypesCsv.append(','); - fieldTypesCsv.append(paramType).append(".class"); - if (fieldNamesCsv.length() > 0) fieldNamesCsv.append(','); fieldNamesCsv.append(VARIABLE_PREFIX).append(i); } @@ -275,15 +271,7 @@ private void generateFactory(TypeElement typeElement) .addTypeVariable(typeVariableName) .returns(typeVariableName) .addParameter(ParameterizedTypeName.get(ClassName.get(Class.class), typeVariableName), "modelClass") - .beginControlFlow("if ($T.class.isAssignableFrom($L))", typeElement, "modelClass") - .beginControlFlow("try") - .addStatement("return $L.getConstructor($L).newInstance($L)", "modelClass", fieldTypesCsv, fieldNamesCsv) - .nextControlFlow("catch ($T | $T | $T | $T e)", NoSuchMethodException.class, IllegalAccessException.class, InstantiationException.class, InvocationTargetException.class) - .addStatement("throw new $T(\"Couldn't create an instance of $T\", e)", RuntimeException.class, typeElement) - .endControlFlow() - .nextControlFlow("else") - .addStatement("throw new $T(\"Couldn't create an instance of $T\")", RuntimeException.class, typeElement) - .endControlFlow() + .addStatement("return (T) new $T($L)", typeElement, fieldNamesCsv) .build(); // Define the class using the previously defined specs. diff --git a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/CustomViewModelFactory.java b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/CustomViewModelFactory.java index c8b54cd..1744179 100644 --- a/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/CustomViewModelFactory.java +++ b/demo/src/main/java/com/hadisatrio/apps/android/alfreddemo/CustomViewModelFactory.java @@ -34,7 +34,7 @@ public final class CustomViewModelFactory implements ViewModelProvider.Factory { @Override public T create(Class modelClass) { - if (DopeViewModel.class.isAssignableFrom(modelClass)) { + if (LameViewModel.class.isAssignableFrom(modelClass)) { try { return modelClass.getConstructor(android.content.Context.class, java.lang.Long.class) .newInstance(context, fucksGiven); diff --git a/gradle/versions.gradle b/gradle/versions.gradle index 72ab178..02c5159 100644 --- a/gradle/versions.gradle +++ b/gradle/versions.gradle @@ -17,7 +17,7 @@ ext { targetCompatibilityVersion = "1.7" // JavaVersion.VERSION_1_7 // Plugins / Build-Level Versions - androidGradleVersion = "3.0.0-beta2" + androidGradleVersion = "3.0.0-beta5" // 1st Party Android Library Versions supportLibraryVersion = "26.0.1" diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 957b5db..d1e7580 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -17,4 +17,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip From 858a1a32115f031a2b9ac5bd4ac1d64d07cdb05a Mon Sep 17 00:00:00 2001 From: Hadi Satrio Date: Mon, 11 Sep 2017 13:00:05 +0700 Subject: [PATCH 4/5] Bump versions --- gradle/versions.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/versions.gradle b/gradle/versions.gradle index 02c5159..d6da877 100644 --- a/gradle/versions.gradle +++ b/gradle/versions.gradle @@ -1,8 +1,8 @@ ext { // Alfred Versions - alfredAnnotationsVersion = "1.0.0-RC.2" - alfredCompilerVersion = "1.0.0-RC.2" + alfredAnnotationsVersion = "1.0.0-RC.3" + alfredCompilerVersion = "1.0.0-RC.3" // Demo App-Related Versions demoVersionCode = 2 From 659e119c6456620c15fa7fc76806fc99cd727a53 Mon Sep 17 00:00:00 2001 From: Hadi Satrio Date: Mon, 11 Sep 2017 13:01:00 +0700 Subject: [PATCH 5/5] Update version numbers in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e88f2d5..58335bb 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,8 @@ Include `Alfred` to your Gradle project by adding it as a dependency in your `bu } dependencies { - compile 'com.hadisatrio.Alfred:annotations:v1.0.0-RC.2' - apt 'com.hadisatrio.Alfred:compiler:v1.0.0-RC.2' + compile 'com.hadisatrio.Alfred:annotations:v1.0.0-RC.3' + apt 'com.hadisatrio.Alfred:compiler:v1.0.0-RC.3' } ```