mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
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:
parent
c45b945526
commit
f7f88d52b6
2 changed files with 47 additions and 13 deletions
33
app/src/main/java/fr/free/nrw/commons/utils/CustomProxy.java
Normal file
33
app/src/main/java/fr/free/nrw/commons/utils/CustomProxy.java
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue