Search in sources :

Example 1 with ObservableProperty

use of com.github.javaparser.ast.observer.ObservableProperty in project javaparser by javaparser.

the class NodeTest method registerWithJustNodeMode.

@Test
public void registerWithJustNodeMode() {
    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.JUST_THIS_NODE);
    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"), changes);
    cu.getClassByName("MyCoolClass").get().getMethodsByName("foo").get(0).getParameterByName("p").get().setName("myParam");
    assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass"), changes);
    cu.getClassByName("MyCoolClass").get().addField("int", "bar").getVariables().get(0).setInitializer("0");
    assertEquals(Arrays.asList("ClassOrInterfaceDeclaration.name changed from A to MyCoolClass"), changes);
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) AstObserver(com.github.javaparser.ast.observer.AstObserver) Test(org.junit.Test)

Example 2 with ObservableProperty

use of com.github.javaparser.ast.observer.ObservableProperty in project javaparser by javaparser.

the class NodeTest method registerSubTree.

@Test
public void registerSubTree() {
    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.registerForSubtree(observer);
    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);
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) AstObserver(com.github.javaparser.ast.observer.AstObserver) Test(org.junit.Test)

Example 3 with ObservableProperty

use of com.github.javaparser.ast.observer.ObservableProperty in project javaparser by javaparser.

the class NodeTest method deleteMethodBodyDoesTriggerNotifications.

@Test
public void deleteMethodBodyDoesTriggerNotifications() {
    String code = "class A { void foo(int p) { }}";
    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("setting [" + property + "] to " + newValue);
        }

        @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);
    assertEquals(true, cu.getClassByName("A").get().getMethodsByName("foo").get(0).getBody().get().remove());
    assertEquals(Arrays.asList("setting [BODY] to null"), changes);
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) AstObserver(com.github.javaparser.ast.observer.AstObserver) Test(org.junit.Test)

Example 4 with ObservableProperty

use of com.github.javaparser.ast.observer.ObservableProperty in project javaparser by javaparser.

the class NodeWithModifiersTest method addModifierTriggerNotification.

@Test
public void addModifierTriggerNotification() {
    List<String> changes = new LinkedList<>();
    ClassOrInterfaceDeclaration decl = new ClassOrInterfaceDeclaration(EnumSet.noneOf(Modifier.class), false, "Foo");
    decl.register(new AstObserverAdapter() {

        @Override
        public void propertyChange(Node observedNode, ObservableProperty property, Object oldValue, Object newValue) {
            changes.add("property " + property.name() + " is changed to " + newValue);
        }
    });
    decl.addModifier(Modifier.PUBLIC);
    assertEquals(1, changes.size());
    assertEquals("property MODIFIERS is changed to [PUBLIC]", changes.get(0));
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Node(com.github.javaparser.ast.Node) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) Modifier(com.github.javaparser.ast.Modifier) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with ObservableProperty

use of com.github.javaparser.ast.observer.ObservableProperty 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);
}
Also used : ObservableProperty(com.github.javaparser.ast.observer.ObservableProperty) AstObserverAdapter(com.github.javaparser.ast.observer.AstObserverAdapter) PrimitiveType(com.github.javaparser.ast.type.PrimitiveType) AstObserver(com.github.javaparser.ast.observer.AstObserver) Test(org.junit.Test)

Aggregations

AstObserverAdapter (com.github.javaparser.ast.observer.AstObserverAdapter)6 ObservableProperty (com.github.javaparser.ast.observer.ObservableProperty)6 Test (org.junit.Test)6 AstObserver (com.github.javaparser.ast.observer.AstObserver)5 PrimitiveType (com.github.javaparser.ast.type.PrimitiveType)4 Modifier (com.github.javaparser.ast.Modifier)1 Node (com.github.javaparser.ast.Node)1 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 LinkedList (java.util.LinkedList)1