Bugfix/customproxy (#2738)

* BugFix #2737
* Added a CustomProxy class which overrides the invocation handler to return approproate values for different datatypes

(cherry picked from commit f68d2e880074277b75a60b730dd63254239a8971)

* Added JavaDocs for CustomProxy
This commit is contained in:
Ashish Kumar 2019-03-26 21:04:01 +05:30 committed by Vivek Maskara
parent c45b945526
commit f7f88d52b6
2 changed files with 47 additions and 13 deletions

View file

@ -2,15 +2,6 @@ package fr.free.nrw.commons.upload;
import android.annotation.SuppressLint;
import android.content.Context;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.category.CategoriesModel;
import fr.free.nrw.commons.contributions.Contribution;
@ -18,11 +9,17 @@ import fr.free.nrw.commons.filepicker.UploadableFile;
import fr.free.nrw.commons.kvstore.JsonKvStore;
import fr.free.nrw.commons.nearby.Place;
import fr.free.nrw.commons.settings.Prefs;
import fr.free.nrw.commons.utils.CustomProxy;
import fr.free.nrw.commons.utils.StringUtils;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import timber.log.Timber;
import static fr.free.nrw.commons.upload.UploadModel.UploadItem;
@ -38,12 +35,16 @@ import static fr.free.nrw.commons.utils.ImageUtils.getErrorMessageForResult;
@Singleton
public class UploadPresenter {
private static final UploadView DUMMY = (UploadView) Proxy.newProxyInstance(UploadView.class.getClassLoader(),
new Class[]{UploadView.class}, (proxy, method, methodArgs) -> null);
private static final UploadView DUMMY =
(UploadView) CustomProxy.newInstance(UploadView.class.getClassLoader(),
new Class[] { UploadView.class });
private UploadView view = DUMMY;
private static final SimilarImageInterface SIMILAR_IMAGE = (SimilarImageInterface) Proxy.newProxyInstance(SimilarImageInterface.class.getClassLoader(),
new Class[]{SimilarImageInterface.class}, (proxy, method, methodArgs) -> null);
private static final SimilarImageInterface SIMILAR_IMAGE =
(SimilarImageInterface) CustomProxy.newInstance(
SimilarImageInterface.class.getClassLoader(),
new Class[] { SimilarImageInterface.class });
private SimilarImageInterface similarImageInterface = SIMILAR_IMAGE;
@UploadView.UploadPage

View file

@ -0,0 +1,33 @@
package fr.free.nrw.commons.utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* Returns a new instance of proxy with overriden invocationhanlder() returning appropriate values
* for different datatypes
* See https://stackoverflow.com/questions/52083338/expected-to-unbox-a-string-primitive-type-but-was-returned-null
*/
public class CustomProxy extends Proxy {
protected CustomProxy(InvocationHandler h) {
super(h);
}
public static Object newInstance(ClassLoader loader, Class<?>[] interfaces) {
return Proxy.newProxyInstance(loader, interfaces, (o, method, objects) -> {
if (String.class == method.getReturnType()) {
return "";
} else if (Integer.class == method.getReturnType()) {
return Integer.valueOf(0);
} else if (int.class == method.getReturnType()) {
return 0;
} else if (Boolean.class == method.getReturnType()) {
return Boolean.FALSE;
} else if (boolean.class == method.getReturnType()) {
return false;
} else {
return null;
}
});
}
}