I am trying to make a proxy between my WebApp and storage on my disk. I know everything works fine for the WebApp and it even saves stuff to the disk, so I know that the Invoke method on my InvocationHandler gets called, but the proxy itself seems to be Null, so nothing on the WebApp gets updated.
Creation of the proxy:
public class Storage {
public static final Storage INSTANCE = new Storage();
private Storage() {
}
//private final Map<String, Note> tmpStorage = new HashMap<>();
public Map<String, Note> getStorageForUser(Credentials credentials) {
String location = "./storage/"+credentials.getUsername();
return (Map<String, Note>) Proxy.newProxyInstance(
Map.class.getClassLoader(),
new Class<?>[]{Map.class},
new FileStorageMap(location, new HashMap<>()));
//return tmpStorage;
}
}
InvocationHandler:
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName() == "post" || method.getName() == "put"){
FileUtils.writeStringToFile(new File(folder.getPath()+"/"+args[0]), (new NoteToJson((Note)args[1])).getJson().toString(), "UTF-8");
}
else{
new File(folder.getPath()+"/"+args[0]).delete();
}
return method.invoke(proxiedMap, args);
}
I have checked and in creation of my InvocationHandler called "FileStorageMap", folder and proxiedMap are datafields and they are both not Null, those are both correctly filled in. Yet for the proxy itself, it returns a NullPointerException. Can anyone tell me why this is?
The mistake lied in the fact that the method names were wrong and more stuff was going to the else statement than I had intended. It wasn't really a problem with the proxy itself.