@ComputedProperty methods can't access the model model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 21 Jan 2013 11:55:27 +0100
branchmodel
changeset 500f9e80d48e9b4
parent 499 af027874f93e
child 505 4198be34b516
@ComputedProperty methods can't access the model
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Mon Jan 21 10:06:42 2013 +0100
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Mon Jan 21 11:55:27 2013 +0100
     1.3 @@ -95,6 +95,7 @@
     1.4                      w.append("package " + pkg + ";\n");
     1.5                      w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
     1.6                      w.append("class ").append(className).append(" {\n");
     1.7 +                    w.append("  private static boolean locked;\n");
     1.8                      w.append("  private ").append(className).append("() {}\n");
     1.9                      for (String id : pp.ids()) {
    1.10                          String tag = pp.tagNameForId(id);
    1.11 @@ -272,9 +273,11 @@
    1.12  
    1.13              w.write("private static " + tn + " prop_" + p.name() + ";\n");
    1.14              w.write("public static " + tn + " " + gs[0] + "() {\n");
    1.15 +            w.write("  if (locked) throw new IllegalStateException();\n");
    1.16              w.write("  return prop_" + p.name() + ";\n");
    1.17              w.write("}\n");
    1.18              w.write("public static void " + gs[1] + "(" + tn + " v) {\n");
    1.19 +            w.write("  if (locked) throw new IllegalStateException();\n");
    1.20              w.write("  prop_" + p.name() + " = v;\n");
    1.21              w.write("  if (ko != null) {\n");
    1.22              w.write("    ko.valueHasMutated(\"" + p.name() + "\");\n");
    1.23 @@ -310,14 +313,14 @@
    1.24              String[] gs = toGetSet(sn, tn);
    1.25              
    1.26              w.write("public static " + tn + " " + gs[0] + "() {\n");
    1.27 -            w.write("  return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
    1.28 -            String sep = "";
    1.29 +            w.write("  if (locked) throw new IllegalStateException();\n");
    1.30 +            int arg = 0;
    1.31              for (VariableElement pe : ee.getParameters()) {
    1.32                  final String dn = pe.getSimpleName().toString();
    1.33 -                String[] call = toGetSet(dn, pe.asType().toString());
    1.34 -                w.write(sep);
    1.35 -                w.write(call[0] + "()");
    1.36 -                sep = ", ";
    1.37 +                final String dt = pe.asType().toString();
    1.38 +                String[] call = toGetSet(dn, dt);
    1.39 +                w.write("  " + dt + " arg" + (++arg) + " = ");
    1.40 +                w.write(call[0] + "();\n");
    1.41                  
    1.42                  Collection<String> depends = deps.get(dn);
    1.43                  if (depends == null) {
    1.44 @@ -326,7 +329,19 @@
    1.45                  }
    1.46                  depends.add(sn);
    1.47              }
    1.48 +            w.write("  try {\n");
    1.49 +            w.write("    locked = true;\n");
    1.50 +            w.write("    return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
    1.51 +            String sep = "";
    1.52 +            for (int i = 1; i <= arg; i++) {
    1.53 +                w.write(sep);
    1.54 +                w.write("arg" + i);
    1.55 +                sep = ", ";
    1.56 +            }
    1.57              w.write(");\n");
    1.58 +            w.write("  } finally {\n");
    1.59 +            w.write("    locked = false;\n");
    1.60 +            w.write("  }\n");
    1.61              w.write("}\n");
    1.62              
    1.63              props.add(e.getSimpleName().toString());
     2.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java	Mon Jan 21 10:06:42 2013 +0100
     2.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java	Mon Jan 21 11:55:27 2013 +0100
     2.3 @@ -65,11 +65,40 @@
     2.4          assertTrue(my.mutated.contains("unrelated"), "Its name is unrelated");
     2.5      }
     2.6      
     2.7 +    @Test public void computedPropertyCannotWriteToModel() {
     2.8 +        try {
     2.9 +            String res = Model.getNotAllowedWrite();
    2.10 +            fail("We should not be allowed to write to the model: " + res);
    2.11 +        } catch (IllegalStateException ex) {
    2.12 +            // OK, we can't read
    2.13 +        }
    2.14 +    }
    2.15 +
    2.16 +    @Test public void computedPropertyCannotReadToModel() {
    2.17 +        try {
    2.18 +            String res = Model.getNotAllowedRead();
    2.19 +            fail("We should not be allowed to read from the model: " + res);
    2.20 +        } catch (IllegalStateException ex) {
    2.21 +            // OK, we can't read
    2.22 +        }
    2.23 +    }
    2.24 +    
    2.25      @ComputedProperty
    2.26      static int powerValue(int value) {
    2.27          return value * value;
    2.28      }
    2.29      
    2.30 +    @ComputedProperty
    2.31 +    static String notAllowedRead() {
    2.32 +        return "Not allowed callback: " + Model.getUnrelated();
    2.33 +    }
    2.34 +
    2.35 +    @ComputedProperty
    2.36 +    static String notAllowedWrite() {
    2.37 +        Model.setUnrelated(11);
    2.38 +        return "Not allowed callback!";
    2.39 +    }
    2.40 +    
    2.41      static class MockKnockout extends Knockout {
    2.42          List<String> mutated = new ArrayList<String>();
    2.43