Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug with newest dagger version #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RealWorldAppKotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

buildscript {
repositories {
jcenter()
mavenCentral()
google()
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ android {

defaultConfig {
applicationId "it.cosenonjaviste.daggeroverride"
minSdkVersion 14
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
Expand Down
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.61'
ext.kotlin_version = '1.3.11'
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -34,6 +34,7 @@ buildscript {
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
google()
}
Expand Down
6 changes: 3 additions & 3 deletions daggermock-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ apply plugin: 'com.github.dcendents.android-maven'

group='com.github.fabioCollini.daggermock'

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compileOnly "org.mockito:mockito-core:$MOCKITO_VERSION"
compileOnly 'junit:junit:4.12'
compileOnly "com.google.dagger:dagger:$DAGGER_VERSION"

compile project(':daggermock')
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

task sourcesJar(type: Jar, dependsOn: classes) {
Expand Down
5 changes: 3 additions & 2 deletions daggermock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ apply plugin: 'com.github.dcendents.android-maven'

group='com.github.fabioCollini.daggermock'

sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
compileOnly 'com.android.support:support-annotations:28.0.0'
compileOnly "org.mockito:mockito-core:$MOCKITO_VERSION"
compileOnly 'junit:junit:4.12'
compileOnly "com.google.dagger:dagger:$DAGGER_VERSION"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;

import javax.inject.Provider;
Expand Down Expand Up @@ -172,11 +173,24 @@ public Field getField(Class<?> fieldClass) {
return null;
}

@android.support.annotation.RequiresApi(api = 24)
public ObjectWrapper<T> invokeBuilderSetter(Class<?> parameterClass, Object parameter) {
try {
Method setMethod = getSetterMethod(obj, parameterClass);
setMethod.setAccessible(true);
return new ObjectWrapper<T>((T) setMethod.invoke(obj, parameter));
Class<?> aClass = obj.getClass();
Method[] methods = aClass.getMethods();
String name = parameterClass.getSimpleName();
Method method = Arrays.stream(methods).filter(m -> m.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
if(method != null) {
method.setAccessible(true);
return new ObjectWrapper<T>((T) method.invoke(obj, parameter));
} else {
Field[] declaredFields = aClass.getDeclaredFields();
Field field = Arrays.stream(declaredFields).filter(m -> m.getName().equalsIgnoreCase(name)).findFirst().orElse(null);
if(field == null) throw new Exception("Method or field not found");
field.setAccessible(true);
field.set(obj, parameter);
return new ObjectWrapper<>(obj);
}
} catch (Exception e) {
throw new RuntimeException("Error invoking setter with parameter " + parameterClass + " on object " + obj, e);
}
Expand Down