1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java Thu May 09 10:32:08 2013 +0200
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java Thu May 09 12:47:26 2013 +0200
1.3 @@ -20,6 +20,7 @@
1.4 */
1.5 package net.java.html.json.tests;
1.6
1.7 +import net.java.html.json.Context;
1.8 import net.java.html.json.Model;
1.9 import net.java.html.json.OnReceive;
1.10 import net.java.html.json.Property;
1.11 @@ -144,6 +145,41 @@
1.12 assert orig == now : "The set of elements is unchanged. Delta: " + (now - orig);
1.13 }
1.14
1.15 +
1.16 +
1.17 + @OnReceive(url="{url}", method = "PUT", data = Person.class)
1.18 + static void putPerson(JSONik model, String reply) {
1.19 + model.setFetchedCount(1);
1.20 + }
1.21 + /*
1.22 + @Http(@Http.Resource(
1.23 + content = "",
1.24 + path="/person.json",
1.25 + mimeType = "text/plain",
1.26 + parameters = { }
1.27 + ))
1.28 + @BrwsrTest public void putPeople() throws InterruptedException, Exception {
1.29 + if (js == null) {
1.30 + orig = scriptElements();
1.31 + assert orig > 0 : "There should be some scripts on the page";
1.32 +
1.33 + js = new JSONik(Utils.newContext());
1.34 + js.applyBindings();
1.35 +
1.36 + Person p = new Person(Context.EMPTY);
1.37 + p.setFirstName("Jarda");
1.38 + js.putPerson("person.json", p);
1.39 + }
1.40 +
1.41 + int cnt = js.getFetchedCount();
1.42 + if (cnt == 0) {
1.43 + throw new InterruptedException();
1.44 + }
1.45 +
1.46 + org.testng.Assert.fail("OK");
1.47 + }
1.48 + */
1.49 +
1.50 private static int scriptElements() throws Exception {
1.51 return ((Number)Utils.executeScript("return window.document.getElementsByTagName('script').length;")).intValue();
1.52 }
2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java Thu May 09 10:32:08 2013 +0200
2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java Thu May 09 12:47:26 2013 +0200
2.3 @@ -726,11 +726,12 @@
2.4 error("@OnReceive method should return void", e);
2.5 return false;
2.6 }
2.7 - if ("PUT".equals(onR.method()) && !isDataSpecified(onR)) {
2.8 + TypeMirror dataMirror = findDataSpecified(e, onR);
2.9 + if ("PUT".equals(onR.method()) && dataMirror == null) {
2.10 error("PUT method needs to specify a data() class", e);
2.11 return false;
2.12 }
2.13 - if ("POST".equals(onR.method()) && !isDataSpecified(onR)) {
2.14 + if ("POST".equals(onR.method()) && dataMirror == null) {
2.15 error("POST method needs to specify a data() class", e);
2.16 return false;
2.17 }
2.18 @@ -754,6 +755,8 @@
2.19 } else if (ve.asType().getKind() == TypeKind.ARRAY) {
2.20 modelType = ((ArrayType)ve.asType()).getComponentType();
2.21 expectsList = true;
2.22 + } else if (ve.asType().toString().equals("java.lang.String")) {
2.23 + modelType = ve.asType();
2.24 }
2.25 if (modelType != null) {
2.26 if (modelClass != null) {
2.27 @@ -1178,11 +1181,41 @@
2.28 }
2.29 }
2.30
2.31 - private boolean isDataSpecified(OnReceive onR) {
2.32 + private TypeMirror findDataSpecified(ExecutableElement e, OnReceive onR) {
2.33 + AnnotationMirror found = null;
2.34 + for (AnnotationMirror am : e.getAnnotationMirrors()) {
2.35 + if (am.getAnnotationType().toString().equals(OnReceive.class.getName())) {
2.36 + found = am;
2.37 + }
2.38 + }
2.39 + if (found == null) {
2.40 + return null;
2.41 + }
2.42 +
2.43 + for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : found.getElementValues().entrySet()) {
2.44 + ExecutableElement ee = entry.getKey();
2.45 + AnnotationValue av = entry.getValue();
2.46 + if (ee.getSimpleName().contentEquals("data")) {
2.47 + List<? extends Object> values = getAnnoValues(processingEnv, ee, found);
2.48 + return ee.asType();
2.49 + }
2.50 + }
2.51 + return null;
2.52 + }
2.53 +
2.54 + static List<? extends Object> getAnnoValues(ProcessingEnvironment pe, Element e, AnnotationMirror am) {
2.55 try {
2.56 - return onR.data() != Object.class;
2.57 - } catch (MirroredTypeException ex) {
2.58 - return !ex.getTypeMirror().toString().equals("java.lang.Object"); // NOI18N
2.59 + Class<?> trees = Class.forName("com.sun.tools.javac.api.JavacTrees");
2.60 + Method m = trees.getMethod("instance", ProcessingEnvironment.class);
2.61 + Object instance = m.invoke(null, pe);
2.62 + m = instance.getClass().getMethod("getPath", Element.class, AnnotationMirror.class);
2.63 + Object path = m.invoke(instance, e, am);
2.64 + m = path.getClass().getMethod("getLeaf");
2.65 + Object leaf = m.invoke(path);
2.66 + m = leaf.getClass().getMethod("getArguments");
2.67 + return (List) m.invoke(leaf);
2.68 + } catch (Exception ex) {
2.69 + return Collections.emptyList();
2.70 }
2.71 }
2.72
2.73 @@ -1209,7 +1242,7 @@
2.74 try {
2.75 return p.type().getName();
2.76 } catch (IncompleteAnnotationException | AnnotationTypeMismatchException ex) {
2.77 - for (Object v : getAnnoValues(env)) {
2.78 + for (Object v : getAnnoValues(env, e, tm)) {
2.79 String s = v.toString().replace(" ", "");
2.80 if (s.startsWith("type=") && s.endsWith(".class")) {
2.81 return s.substring(5, s.length() - 6);
2.82 @@ -1250,22 +1283,6 @@
2.83 }
2.84 return ret;
2.85 }
2.86 -
2.87 - private List<? extends Object> getAnnoValues(ProcessingEnvironment pe) {
2.88 - try {
2.89 - Class<?> trees = Class.forName("com.sun.tools.javac.api.JavacTrees");
2.90 - Method m = trees.getMethod("instance", ProcessingEnvironment.class);
2.91 - Object instance = m.invoke(null, pe);
2.92 - m = instance.getClass().getMethod("getPath", Element.class, AnnotationMirror.class);
2.93 - Object path = m.invoke(instance, e, tm);
2.94 - m = path.getClass().getMethod("getLeaf");
2.95 - Object leaf = m.invoke(path);
2.96 - m = leaf.getClass().getMethod("getArguments");
2.97 - return (List)m.invoke(leaf);
2.98 - } catch (Exception ex) {
2.99 - return Collections.emptyList();
2.100 - }
2.101 - }
2.102 }
2.103
2.104 @Override
3.1 --- a/json/src/test/java/net/java/html/json/ModelTest.java Thu May 09 10:32:08 2013 +0200
3.2 +++ b/json/src/test/java/net/java/html/json/ModelTest.java Thu May 09 12:47:26 2013 +0200
3.3 @@ -162,7 +162,7 @@
3.4 }
3.5 }
3.6
3.7 - @OnReceive(url = "{protocol}://{host}?query={query}")
3.8 + @OnReceive(url = "{protocol}://{host}?query={query}", data = Person.class)
3.9 static void loadPeople(Modelik thiz, People p) {
3.10 Modelik m = null;
3.11 m.applyBindings();