#5930: Second argument of @OnReceive method can be a java.util.List
authorJaroslav Tulach <jtulach@netbeans.org>
Fri, 04 Apr 2014 17:16:46 +0200
changeset 647989ce2017405
parent 646 a838ef015f5f
child 648 18272c3f60c4
#5930: Second argument of @OnReceive method can be a java.util.List
json/src/main/java/net/java/html/json/OnReceive.java
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/Compile.java
json/src/test/java/net/java/html/json/ModelProcessorTest.java
json/src/test/java/org/netbeans/html/json/impl/EmployeeImpl.java
src/main/javadoc/overview.html
     1.1 --- a/json/src/main/java/net/java/html/json/OnReceive.java	Mon Mar 31 12:01:32 2014 +0200
     1.2 +++ b/json/src/main/java/net/java/html/json/OnReceive.java	Fri Apr 04 17:16:46 2014 +0200
     1.3 @@ -50,9 +50,13 @@
     1.4  /** Static methods in classes annotated by {@link Model}
     1.5   * can be marked by this annotation to establish a 
     1.6   * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
     1.7 - * communication point.
     1.8 + * communication point. The first argument should be the
     1.9 + * associated {@link Model} class. The second argument can
    1.10 + * be another class generated by {@link Model} annotation,
    1.11 + * or array of such classes or (since 0.8.1 version) a
    1.12 + * {@link java.util.List} of such classes.
    1.13   * The associated model class then gets new method to invoke a network
    1.14 - * connection. Example follows:
    1.15 + * connection asynchronously. Example follows:
    1.16   * 
    1.17   * <pre>
    1.18   * {@link Model @Model}(className="MyModel", properties={
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Mon Mar 31 12:01:32 2014 +0200
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Fri Apr 04 17:16:46 2014 +0200
     2.3 @@ -1008,12 +1008,14 @@
     2.4                  return false;
     2.5              }
     2.6              String modelClass = null;
     2.7 -            boolean expectsList = false;
     2.8 +            int expectsList = 0;
     2.9              List<String> args = new ArrayList<String>();
    2.10              {
    2.11 +                final Types tu = processingEnv.getTypeUtils();
    2.12                  for (VariableElement ve : e.getParameters()) {
    2.13                      TypeMirror modelType = null;
    2.14                      final TypeMirror type = ve.asType();
    2.15 +                    TypeMirror ert = tu.erasure(type);
    2.16                      CharSequence simpleName;
    2.17                      if (type.getKind() == TypeKind.DECLARED) {
    2.18                          simpleName = ((DeclaredType)type).asElement().getSimpleName();
    2.19 @@ -1026,7 +1028,13 @@
    2.20                          modelType = ve.asType();
    2.21                      } else if (ve.asType().getKind() == TypeKind.ARRAY) {
    2.22                          modelType = ((ArrayType)ve.asType()).getComponentType();
    2.23 -                        expectsList = true;
    2.24 +                        expectsList = 1;
    2.25 +                    } else if ("java.util.List".equals(fqn(ert, e))) {
    2.26 +                        List<? extends TypeMirror> typeArgs = ((DeclaredType)ve.asType()).getTypeArguments();
    2.27 +                        if (typeArgs.size() == 1) {
    2.28 +                            modelType = typeArgs.get(0);
    2.29 +                            expectsList = 2;
    2.30 +                        }
    2.31                      } else if (ve.asType().toString().equals("java.lang.String")) {
    2.32                          modelType = ve.asType();
    2.33                      }
    2.34 @@ -1035,8 +1043,10 @@
    2.35                              error("There can be only one model class among arguments", e);
    2.36                          } else {
    2.37                              modelClass = modelType.toString();
    2.38 -                            if (expectsList) {
    2.39 +                            if (expectsList == 1) {
    2.40                                  args.add("arr");
    2.41 +                            } else if (expectsList == 2) {
    2.42 +                                args.add("java.util.Arrays.asList(arr)");
    2.43                              } else {
    2.44                                  args.add("arr[0]");
    2.45                              }
    2.46 @@ -1084,13 +1094,13 @@
    2.47              body.append(") {\n");
    2.48              boolean webSocket = onR.method().equals("WebSocket");
    2.49              if (webSocket) {
    2.50 -                if (generateWSReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList, modelClass, n, args, urlBefore, jsonpVarName, urlAfter, dataMirror)) {
    2.51 +                if (generateWSReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList != 0, modelClass, n, args, urlBefore, jsonpVarName, urlAfter, dataMirror)) {
    2.52                      return false;
    2.53                  }
    2.54                  body.append("  }\n");
    2.55                  body.append("  private Object ws_" + e.getSimpleName() + ";\n");
    2.56              } else {
    2.57 -                if (generateJSONReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList, modelClass, n, args, urlBefore, jsonpVarName, urlAfter, dataMirror)) {
    2.58 +                if (generateJSONReceiveBody(index++, body, inType, onR, e, clazz, className, expectsList != 0, modelClass, n, args, urlBefore, jsonpVarName, urlAfter, dataMirror)) {
    2.59                      return false;
    2.60                  }
    2.61                  body.append("  }\n");
     3.1 --- a/json/src/test/java/net/java/html/json/Compile.java	Mon Mar 31 12:01:32 2014 +0200
     3.2 +++ b/json/src/test/java/net/java/html/json/Compile.java	Fri Apr 04 17:16:46 2014 +0200
     3.3 @@ -68,8 +68,7 @@
     3.4  import javax.tools.StandardJavaFileManager;
     3.5  import javax.tools.StandardLocation;
     3.6  import javax.tools.ToolProvider;
     3.7 -import static org.testng.Assert.assertFalse;
     3.8 -import static org.testng.Assert.fail;
     3.9 +import static org.testng.Assert.*;
    3.10  
    3.11  /**
    3.12   *
    3.13 @@ -269,6 +268,9 @@
    3.14      void assertErrors() {
    3.15          assertFalse(getErrors().isEmpty(), "There are supposed to be some errors");
    3.16      }
    3.17 +    void assertNoErrors() {
    3.18 +        assertTrue(getErrors().isEmpty(), "There are supposed to be no errors: " + getErrors());
    3.19 +    }
    3.20  
    3.21      void assertError(String expMsg) {
    3.22          StringBuilder sb = new StringBuilder();
     4.1 --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Mon Mar 31 12:01:32 2014 +0200
     4.2 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Fri Apr 04 17:16:46 2014 +0200
     4.3 @@ -278,6 +278,25 @@
     4.4          res.assertErrors();
     4.5          res.assertError("not find doesNotExist");
     4.6      }
     4.7 +    
     4.8 +    @Test public void usingListIsOK() throws IOException {
     4.9 +        Compile res = Compile.create("", "package x;\n"
    4.10 +            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
    4.11 +            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
    4.12 +            + "})\n"
    4.13 +            + "class UseOnReceive {\n"
    4.14 +            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\")\n"
    4.15 +            + "  static void onMessage(MyModel model, java.util.List<MyData> value) {\n"
    4.16 +            + "  }\n"
    4.17 +            + "\n"
    4.18 +            + "  @net.java.html.json.Model(className=\"MyData\", properties={\n"
    4.19 +            + "  })\n"
    4.20 +            + "  static class MyDataModel {\n"
    4.21 +            + "  }\n"
    4.22 +            + "}\n"
    4.23 +        );
    4.24 +        res.assertNoErrors();
    4.25 +    }
    4.26  
    4.27      @Test public void onErrorWouldHaveToBeStatic() throws IOException {
    4.28          Compile res = Compile.create("", "package x;\n"
     5.1 --- a/json/src/test/java/org/netbeans/html/json/impl/EmployeeImpl.java	Mon Mar 31 12:01:32 2014 +0200
     5.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/EmployeeImpl.java	Fri Apr 04 17:16:46 2014 +0200
     5.3 @@ -42,6 +42,7 @@
     5.4   */
     5.5  package org.netbeans.html.json.impl;
     5.6  
     5.7 +import java.util.List;
     5.8  import net.java.html.json.Model;
     5.9  import net.java.html.json.OnReceive;
    5.10  import net.java.html.json.Person;
    5.11 @@ -60,4 +61,9 @@
    5.12      static void changePersonality(Employee e, Person p) {
    5.13          e.setPerson(p);
    5.14      }
    5.15 +
    5.16 +    @OnReceive(url = "some/other/url")
    5.17 +    static void changePersonalities(Employee e, List<Person> p) {
    5.18 +        e.setPerson(p.get(0));
    5.19 +    }
    5.20  }
     6.1 --- a/src/main/javadoc/overview.html	Mon Mar 31 12:01:32 2014 +0200
     6.2 +++ b/src/main/javadoc/overview.html	Fri Apr 04 17:16:46 2014 +0200
     6.3 @@ -78,6 +78,15 @@
     6.4          <h3>What's New in Version 0.8?</h3>
     6.5          
     6.6          <p>
     6.7 +            {@link net.java.html.json.OnReceive} annotation now accepts
     6.8 +            {@link java.util.List} of data values as second argument
     6.9 +            (previously required an array).
    6.10 +        </p>
    6.11 +        
    6.12 +        
    6.13 +        <h3>What's New in Older Versions?</h3>
    6.14 +        
    6.15 +        <p>
    6.16              {@link net.java.html.js.JavaScriptBody} annotation has new attribute
    6.17              {@link net.java.html.js.JavaScriptBody#wait4js()} which allows
    6.18              asynchronous execution. Libraries using