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

@ -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;
}
});
}
}