Skip to content

Commit

Permalink
fix the remove modifier implementation
Browse files Browse the repository at this point in the history
The current one throws a ConcurrentModificationException if the modifier
that should be removed exists on the node
  • Loading branch information
signed committed Jan 2, 2019
1 parent 8e88daa commit e9194d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
Expand Up @@ -21,6 +21,7 @@


package com.github.javaparser.ast.nodeTypes; package com.github.javaparser.ast.nodeTypes;


import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node; import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
Expand All @@ -31,7 +32,10 @@
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;


import static com.github.javaparser.ast.Modifier.Keyword.PRIVATE;
import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC; import static com.github.javaparser.ast.Modifier.Keyword.PUBLIC;
import static com.github.javaparser.ast.Modifier.Keyword.STATIC;
import static com.github.javaparser.ast.Modifier.Keyword.SYNCHRONIZED;
import static com.github.javaparser.ast.Modifier.createModifierList; import static com.github.javaparser.ast.Modifier.createModifierList;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;


Expand Down Expand Up @@ -61,4 +65,32 @@ public void propertyChange(Node observedNode, ObservableProperty property, Objec
assertEquals("property MODIFIERS is changed to [public ]", changes.get(0)); assertEquals("property MODIFIERS is changed to [public ]", changes.get(0));
} }


@Test
void removeExistingModifier() {
NodeWithModifiers node = anythingWithModifiers(PUBLIC);
node.removeModifier(PUBLIC);
assertEquals(0, node.getModifiers().size());
}

@Test
void ignoreNotExistingModifiersOnRemove() {
NodeWithModifiers node = anythingWithModifiers(PUBLIC);
node.removeModifier(PRIVATE);

assertEquals(createModifierList(PUBLIC), node.getModifiers());
}

@Test
void keepModifiersThatShouldNotBeRemoved() {
NodeWithModifiers node = anythingWithModifiers(PUBLIC, STATIC, SYNCHRONIZED);
node.removeModifier(PUBLIC, PRIVATE, STATIC);

assertEquals(createModifierList(SYNCHRONIZED), node.getModifiers());
}

private NodeWithModifiers anythingWithModifiers(Modifier.Keyword ... keywords) {
ClassOrInterfaceDeclaration foo = new ClassOrInterfaceDeclaration(new NodeList<>(), false, "Foo");
foo.addModifier(keywords);
return foo;
}
} }
Expand Up @@ -26,6 +26,9 @@
import com.github.javaparser.ast.NodeList; import com.github.javaparser.ast.NodeList;


import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;


import static com.github.javaparser.ast.NodeList.toNodeList; import static com.github.javaparser.ast.NodeList.toNodeList;


Expand Down Expand Up @@ -61,15 +64,11 @@ default N addModifier(Modifier.Keyword... newModifiers) {


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
default N removeModifier(Modifier.Keyword... modifiersToRemove) { default N removeModifier(Modifier.Keyword... modifiersToRemove) {
NodeList<Modifier> existingModifiers = new NodeList<>(getModifiers()); List<Modifier.Keyword> modifiersToRemoveAsList = Arrays.asList(modifiersToRemove);
for (Modifier.Keyword modifierToRemove : modifiersToRemove) { NodeList<Modifier> remaining = getModifiers().stream()
for (Modifier existingModifier : existingModifiers) { .filter(existingModifier -> !modifiersToRemoveAsList.contains(existingModifier.getKeyword()))
if (existingModifier.getKeyword() == modifierToRemove) { .collect(toNodeList());
existingModifiers.remove(existingModifier); setModifiers(remaining);
}
}
}
setModifiers(existingModifiers);
return (N) this; return (N) this;
} }


Expand Down

0 comments on commit e9194d2

Please sign in to comment.