vendredi 6 août 2021

Unconsistent behavior of NoSuchFieldError

Normally the NoSuchFieldError is thrown, when a requested field does not exist at runtime because (for example) another version of a library is used in production than in development.

But in my case, this is not true. I have a field entitySlices in a Chunk class. When I access it from my program, it will throw the NoSuchFieldError. But the field exists in the production environment, I even decompiled the corresponding .jar file and verified that .

What surprised me the most was that I could access the field via reflection. When I list all public fields of the Chunk class, it prints the entitySlices field and I can read out its value without any problems. So obviously, the field is existent and accessible, but why can I not access it the 'normal' way? Is there any reason for this behavior?

Here my reflection code:

    // read out the public fields of Chunk class via reflection
    Class<?> chunkClass = Chunk.class;
    for (Field field : chunkClass.getFields()) {
      System.out.println("field found: " + field.getName() + " | " + field.getType());
      if (!field.getName().equalsIgnoreCase("entitySlices")) {
        continue;
      }
      
      // prints out the value of 'entitySlices' without any problems
      Object sliceObj = ReflectionUtil.getValue(nmsChunk, field.getName());
      List<Entity>[] sliceList = (List<Entity>[]) sliceObj;
      System.out.println(Arrays.toString(sliceList));
    }
    
    // when this is called, the NoSuchFieldError is thrown and execution stops.
    for (EntitySlice<Entity> entitySlice : nmsChunk.entitySlices) {
      // do something with the entity slice...
    }





Aucun commentaire:

Enregistrer un commentaire