vendredi 11 novembre 2016

Java reflection: Can not set static final java.util.Map field .. to java.util.HashMap

I get the error:
"java.lang.IllegalAccessException: Can not set static final java.util.Map field net.minecraft.server.v1_8_R3.EntityTypes.c to java.util.HashMap" When adding a Map to a Field

protected static Field mapStringToClassField;
...
Map mapStringToClass = (Map) mapStringToClassField.get(null);
mapStringToClassField.set(null, mapStringToClass); //-> here appears the error

Here is my class:

import java.lang.reflect.Field;
import java.util.Map;
...
protected static Field mapStringToClassField, mapClassToStringField, mapClassToIdField,
        mapStringToIdField;

static {
    try {
        mapStringToClassField = net.minecraft.server.v1_8_R3.EntityTypes.class
                .getDeclaredField("c");
        mapClassToStringField = net.minecraft.server.v1_8_R3.EntityTypes.class
                .getDeclaredField("d");
        // mapIdtoClassField = net.minecraft.server.v1_8_R3.EntityTypes.class.getDeclaredField("e");
        mapClassToIdField = net.minecraft.server.v1_8_R3.EntityTypes.class
                .getDeclaredField("f");
        mapStringToIdField = net.minecraft.server.v1_8_R3.EntityTypes.class
                .getDeclaredField("g");

        mapStringToClassField.setAccessible(true);
        mapClassToStringField.setAccessible(true);
        // mapIdToClassField.setAccessible(true);
        mapClassToIdField.setAccessible(true);
        mapStringToIdField.setAccessible(true);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void registerCustomEntity(Class entityClass, String name, int id) {
    if (mapStringToClassField == null || mapStringToIdField == null
            || mapClassToStringField == null || mapClassToIdField == null) {
        throw new RuntimeException(new IllegalStateException(
                "The required variables are not set!"));
    } else {
        try {
            Map mapStringToClass = (Map) mapStringToClassField.get(null);
            Map mapStringToId = (Map) mapStringToIdField.get(null);
            Map mapClasstoString = (Map) mapClassToStringField.get(null);
            Map mapClassToId = (Map) mapClassToIdField.get(null);

            mapStringToClass.put(name, entityClass);
            mapStringToId.put(name, Integer.valueOf(id));
            mapClasstoString.put(entityClass, name);
            mapClassToId.put(entityClass, Integer.valueOf(id));

            mapStringToClassField.set(null, mapStringToClass);
            mapStringToIdField.set(null, mapStringToId);
            mapClassToStringField.set(null, mapClasstoString);
            mapClassToIdField.set(null, mapClassToId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

Sorry my explanation isn´t very detailed, I don´t know how to explain it otherwise.

Cheers!





Aucun commentaire:

Enregistrer un commentaire