Skip to content

Commit f7f88d5

Browse files
ashishkumar468maskaravivek
authored andcommitted
Bugfix/customproxy (commons-app#2738)
* BugFix commons-app#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
1 parent c45b945 commit f7f88d5

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

app/src/main/java/fr/free/nrw/commons/upload/UploadPresenter.java

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22

33
import android.annotation.SuppressLint;
44
import android.content.Context;
5-
6-
import java.lang.reflect.Proxy;
7-
import java.util.ArrayList;
8-
import java.util.List;
9-
10-
import javax.inject.Inject;
11-
import javax.inject.Named;
12-
import javax.inject.Singleton;
13-
145
import fr.free.nrw.commons.R;
156
import fr.free.nrw.commons.category.CategoriesModel;
167
import fr.free.nrw.commons.contributions.Contribution;
178
import fr.free.nrw.commons.filepicker.UploadableFile;
189
import fr.free.nrw.commons.kvstore.JsonKvStore;
1910
import fr.free.nrw.commons.nearby.Place;
2011
import fr.free.nrw.commons.settings.Prefs;
12+
import fr.free.nrw.commons.utils.CustomProxy;
2113
import fr.free.nrw.commons.utils.StringUtils;
2214
import io.reactivex.Observable;
2315
import io.reactivex.android.schedulers.AndroidSchedulers;
2416
import io.reactivex.disposables.CompositeDisposable;
2517
import io.reactivex.schedulers.Schedulers;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import javax.inject.Inject;
21+
import javax.inject.Named;
22+
import javax.inject.Singleton;
2623
import timber.log.Timber;
2724

2825
import static fr.free.nrw.commons.upload.UploadModel.UploadItem;
@@ -38,12 +35,16 @@
3835
@Singleton
3936
public class UploadPresenter {
4037

41-
private static final UploadView DUMMY = (UploadView) Proxy.newProxyInstance(UploadView.class.getClassLoader(),
42-
new Class[]{UploadView.class}, (proxy, method, methodArgs) -> null);
38+
private static final UploadView DUMMY =
39+
(UploadView) CustomProxy.newInstance(UploadView.class.getClassLoader(),
40+
new Class[] { UploadView.class });
41+
4342
private UploadView view = DUMMY;
4443

45-
private static final SimilarImageInterface SIMILAR_IMAGE = (SimilarImageInterface) Proxy.newProxyInstance(SimilarImageInterface.class.getClassLoader(),
46-
new Class[]{SimilarImageInterface.class}, (proxy, method, methodArgs) -> null);
44+
private static final SimilarImageInterface SIMILAR_IMAGE =
45+
(SimilarImageInterface) CustomProxy.newInstance(
46+
SimilarImageInterface.class.getClassLoader(),
47+
new Class[] { SimilarImageInterface.class });
4748
private SimilarImageInterface similarImageInterface = SIMILAR_IMAGE;
4849

4950
@UploadView.UploadPage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Proxy;
5+
6+
/**
7+
* Returns a new instance of proxy with overriden invocationhanlder() returning appropriate values
8+
* for different datatypes
9+
* See https://stackoverflow.com/questions/52083338/expected-to-unbox-a-string-primitive-type-but-was-returned-null
10+
*/
11+
public class CustomProxy extends Proxy {
12+
protected CustomProxy(InvocationHandler h) {
13+
super(h);
14+
}
15+
16+
public static Object newInstance(ClassLoader loader, Class<?>[] interfaces) {
17+
return Proxy.newProxyInstance(loader, interfaces, (o, method, objects) -> {
18+
if (String.class == method.getReturnType()) {
19+
return "";
20+
} else if (Integer.class == method.getReturnType()) {
21+
return Integer.valueOf(0);
22+
} else if (int.class == method.getReturnType()) {
23+
return 0;
24+
} else if (Boolean.class == method.getReturnType()) {
25+
return Boolean.FALSE;
26+
} else if (boolean.class == method.getReturnType()) {
27+
return false;
28+
} else {
29+
return null;
30+
}
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)