use of com.github.javaparser.ast.observer.AstObserverAdapter in project javaparser by javaparser.
the class NodeTest method deleteClassNameDoesNotTriggerNotifications.
@Test
public void deleteClassNameDoesNotTriggerNotifications() {
String code = "class A { void foo(int p) { }}";
CompilationUnit cu = parse(code);
List<String> changes = new ArrayList<>();
AstObserver observer = new AstObserverAdapter() {
@Override
public void listChange(NodeList observedNode, ListChangeType type, int index, Node nodeAddedOrRemoved) {
changes.add("removing [" + nodeAddedOrRemoved + "] from index " + index);
}
};
cu.register(observer, Node.ObserverRegistrationMode.SELF_PROPAGATING);
// I cannot remove the name of a type
assertEquals(false, cu.getClassByName("A").get().getName().remove());
assertEquals(Arrays.asList(), changes);
}
use of com.github.javaparser.ast.observer.AstObserverAdapter in project javaparser by javaparser.
the class NodeTest method registerWithSelfPropagatingMode.
@Test
public void registerWithSelfPropagatingMode() {
String code = "class A { int f; void foo(int p) { return 'z'; }}";
CompilationUnit cu = parse(code);
List<String> changes = new ArrayList<>();
AstObserver observer = new AstObserverAdapter() {
@Override
public void propertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
changes.add(String.format("%s.%s changed from %s to %s", observedNode.getClass().getSimpleName(), property.name().toLowerCase(), oldValue, newValue));
}
};
cu.getClassByName("A").get().register(observer, Node.ObserverRegistrationMode.SELF_PROPAGATING);
assertEquals(Arrays.asList(), changes);
cu.getClassByName("A").get().setName("MyCoolClass");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass"), changes);
cu.getClassByName("MyCoolClass").get().getFieldByName("f").get().getVariable(0).setType(new PrimitiveType(PrimitiveType.Primitive.BOOLEAN));
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean"), changes);
cu.getClassByName("MyCoolClass").get().getMethodsByName("foo").get(0).getParameterByName("p").get().setName("myParam");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean", "Parameter.name changed from p to myParam"), changes);
cu.getClassByName("MyCoolClass").get().addField("int", "bar").getVariables().get(0).setInitializer("0");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean", "Parameter.name changed from p to myParam", "VariableDeclarator.initializer changed from null to 0"), changes);
}
use of com.github.javaparser.ast.observer.AstObserverAdapter in project javaparser by javaparser.
the class NodeTest method registerWithNodeAndExistingDescendantsMode.
@Test
public void registerWithNodeAndExistingDescendantsMode() {
String code = "class A { int f; void foo(int p) { return 'z'; }}";
CompilationUnit cu = parse(code);
List<String> changes = new ArrayList<>();
AstObserver observer = new AstObserverAdapter() {
@Override
public void propertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
changes.add(String.format("%s.%s changed from %s to %s", observedNode.getClass().getSimpleName(), property.name().toLowerCase(), oldValue, newValue));
}
};
cu.getClassByName("A").get().register(observer, Node.ObserverRegistrationMode.THIS_NODE_AND_EXISTING_DESCENDANTS);
assertEquals(Arrays.asList(), changes);
cu.getClassByName("A").get().setName("MyCoolClass");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass"), changes);
cu.getClassByName("MyCoolClass").get().getFieldByName("f").get().getVariable(0).setType(new PrimitiveType(PrimitiveType.Primitive.BOOLEAN));
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean"), changes);
cu.getClassByName("MyCoolClass").get().getMethodsByName("foo").get(0).getParameterByName("p").get().setName("myParam");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean", "Parameter.name changed from p to myParam"), changes);
cu.getClassByName("MyCoolClass").get().addField("int", "bar").getVariables().get(0).setInitializer("0");
assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass", "FieldDeclaration.maximum_common_type changed from int to boolean", "VariableDeclarator.type changed from int to boolean", "Parameter.name changed from p to myParam"), changes);
}
Aggregations