Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.0-RC.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
MrHadiSatrio authored Sep 11, 2017
2 parents c70b572 + 659e119 commit bf410f7
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,15 @@ private void generateFactory(TypeElement typeElement)

// Define the fields based on previously queried constructor params.
final List<FieldSpec> 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(
Expand All @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> someNumbers;

public AnotherDopeViewModel(Context context, Long fucksGiven) {
this(context, fucksGiven, "");
this(context, fucksGiven, "", Collections.<Integer>emptyList());
}

@Main
public AnotherDopeViewModel(Context context, Long fucksGiven, String whatNot) {
public AnotherDopeViewModel(Context context, Long fucksGiven, String whatNot, List<Integer> someNumbers) {
this.context = context;
this.fucksGiven = fucksGiven;
this.whatNot = whatNot;
this.someNumbers = someNumbers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class CustomViewModelFactory implements ViewModelProvider.Factory {

@Override
public <T extends ViewModel> T create(Class<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions gradle/versions.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bf410f7

Please sign in to comment.