0

I need to Delete a field In a Class file Using ASM, But im not able to Find anything that works,

for(FieldNode field : classNode.fields) {
    if(field.name.equals("max") && field.value.equals(30)) {
        classNode.fields.remove(field);
        System.out.println("***DELETED " + field.name + " ***");
    }
}

Its giving me a Exeption: DELETED max java.util.ConcurrentModificationException

Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
Jasper Smit
  • 11
  • 1
  • 2

1 Answers1

1

Deleting something from a Collection while iterating over it is a bad idea and will everytime throw a java.util.ConcurrentModificationException.

If you operate on Java 8 or newer, please consider using Collection::removeIf and give it a predicate lambda to select the items to remove.

In your case this might work:

classNode.fields.removeIf(field -> field.name.equals("max") && field.value.equals(30));
Alexiy
  • 1,966
  • 16
  • 18
fireandfuel
  • 732
  • 1
  • 9
  • 22