Replacing model really needs to change the reference, as they are usually mutable and even if they are equal at a moment, they may start to be different in the future.
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 04 Jul 2015 22:41:17 +0200
changeset 950b171b7ec2f04
parent 949 2f712d0ef17e
child 951 5ce0aab2c03c
Replacing model really needs to change the reference, as they are usually mutable and even if they are equal at a moment, they may start to be different in the future.
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/org/netbeans/html/json/impl/DeepChangeTest.java
     1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Fri Jul 03 18:27:00 2015 +0200
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Sat Jul 04 22:41:17 2015 +0200
     1.3 @@ -576,6 +576,10 @@
     1.4                  w.write("  }\n");
     1.5              } else {
     1.6                  castTo = tn;
     1.7 +                boolean isModel[] = { false };
     1.8 +                boolean isEnum[] = { false };
     1.9 +                boolean isPrimitive[] = { false };
    1.10 +                checkType(p, isModel, isEnum, isPrimitive);
    1.11                  w.write("  private " + tn + " prop_" + p.name() + ";\n");
    1.12                  w.write("  public " + tn + " " + gs[0] + "() {\n");
    1.13                  w.write("    proto.accessProperty(\"" + p.name() + "\");\n");
    1.14 @@ -583,9 +587,14 @@
    1.15                  w.write("  }\n");
    1.16                  w.write("  public void " + gs[1] + "(" + tn + " v) {\n");
    1.17                  w.write("    proto.verifyUnlocked();\n");
    1.18 -                w.write("    if (TYPE.isSame(prop_" + p.name() + ", v)) return;\n");
    1.19                  w.write("    Object o = prop_" + p.name() + ";\n");
    1.20 -                w.write("    prop_" + p.name() + " = v;\n");
    1.21 +                if (isModel[0]) {
    1.22 +                    w.write("    prop_" + p.name() + " = v;\n");
    1.23 +                    w.write("    if (TYPE.isSame(prop_" + p.name() + ", v)) return;\n");
    1.24 +                } else {
    1.25 +                    w.write("    if (TYPE.isSame(prop_" + p.name() + ", v)) return;\n");
    1.26 +                    w.write("    prop_" + p.name() + " = v;\n");
    1.27 +                }
    1.28                  w.write("    proto.valueHasMutated(\"" + p.name() + "\", o, v);\n");
    1.29                  {
    1.30                      Collection<String[]> dependants = deps.get(p.name());
     2.1 --- a/json/src/test/java/org/netbeans/html/json/impl/DeepChangeTest.java	Fri Jul 03 18:27:00 2015 +0200
     2.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/DeepChangeTest.java	Sat Jul 04 22:41:17 2015 +0200
     2.3 @@ -412,6 +412,17 @@
     2.4          assertEquals(o.get(), "Nazdar");
     2.5          assertEquals(o.changes, 1, "One change so far");
     2.6      }
     2.7 +    
     2.8 +    @Test
     2.9 +    public void rebindReplacesTheInstance() throws Exception {
    2.10 +        BrwsrCtx ctx = Contexts.newBuilder().build();
    2.11 +        MyX x = new MyX();
    2.12 +        
    2.13 +        MyY y = Models.bind(new MyY(), ctx);
    2.14 +        x.setOne(y);
    2.15 +        
    2.16 +        assertSame(x.getOne(), y);
    2.17 +    }
    2.18  
    2.19      @Test
    2.20      public void mixingWithCloneIsOK() throws Exception {