I’m trying to find a way to extract a HashMap from a private static field within another class via Java.
eg.
Inside FooClass there is a static field that looks like this:
private Map entityRenderMap;
Then in its construct it has:
entityRenderMap = new HashMap();
How do you get the values within entityRenderMap via Reflection in Java? I’ve tried this but get errors:
cl = RenderManager.class.getDeclaredField("entityRenderMap");
cl.setAccessible(true);
Object foo = cl.get(this.entityRenderMap);
Mod.log(cl.getName());
The error I get is:
java.lang.IllegalArgumentException: Can not set java.util.Map field RenderManager.entityRenderMap to java.util.HashMap
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source
First, your code doesn’t match your explanation. Is it really a static field or is it not (your code says it’s not)?
If it is static, you should pass
nullas argument tocl.get()(you don’t need an instance to access static members).However, I suspect that your field is actually not static, and your passing the wrong instance to
cl.get(). The JavaDocs toField.get()state it would throw anIllegalArgumentExceptionin this case. You need to pass aRenderManagerinstance to this method. Your code looks like your passing aMap(theentityRenderMap).And last, is this code inside your
RenderManagerclass? I suspect this, because your accessing a field withthiswith the same name as the field you want to set. In this case, don’t use reflection at all!