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' } ``` 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..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,10 +222,15 @@ 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++) { - 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( @@ -236,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); } @@ -269,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/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/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/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); diff --git a/gradle/versions.gradle b/gradle/versions.gradle index 72ab178..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 @@ -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