1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Tue Jan 28 15:14:14 2014 +0100
1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Tue Jan 28 16:18:33 2014 +0100
1.3 @@ -217,7 +217,7 @@
1.4 if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
1.5 ok = false;
1.6 }
1.7 - if (!generateProperties(e, body, className, props, propsGetSet, propsDeps, functionDeps)) {
1.8 + if (!generateProperties(e, body, className, props, propsGetSet, null, propsDeps, functionDeps)) {
1.9 ok = false;
1.10 }
1.11 if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
1.12 @@ -236,7 +236,7 @@
1.13 w.append("import net.java.html.json.*;\n");
1.14 w.append("public final ");
1.15 generateClassBody(w, className, body, e, props,
1.16 - functionDeps, propsDeps, propsGetSet,
1.17 + functionDeps, propsDeps, propsGetSet, null,
1.18 functions, onReceiveType
1.19 );
1.20 } finally {
1.21 @@ -255,6 +255,7 @@
1.22 Map<String, Collection<String>> functionDeps,
1.23 Map<String, Collection<String>> propsDeps,
1.24 List<PropInfo> propsGetSet,
1.25 + Collection<PropInfo> taken,
1.26 List<String> functions,
1.27 StringBuilder onReceiveType
1.28 ) throws IOException {
1.29 @@ -567,7 +568,7 @@
1.30 if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
1.31 ok = false;
1.32 }
1.33 - if (!generateProperties(e, body, className, props, propsGetSet, propsDeps, functionDeps)) {
1.34 + if (!generateProperties(e, body, className, props, propsGetSet, null, propsDeps, functionDeps)) {
1.35 ok = false;
1.36 }
1.37 if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
1.38 @@ -919,6 +920,14 @@
1.39 }
1.40 w.write(" return null;\n");
1.41 w.write(" }\n");
1.42 + ArrayList<PropInfo> sharedProps = new ArrayList<PropInfo>(propsGetSet);
1.43 + final PropInfo switchProp = new PropInfo(e.getSimpleName().toString(), null, null, null, null);
1.44 + if (sharedProps.contains(switchProp)) {
1.45 + ok = false;
1.46 + error("Duplicated property " + switchProp.p0 + " due to enum switch", e);
1.47 + } else {
1.48 + sharedProps.add(switchProp);
1.49 + }
1.50 for (Element ec : e.getEnclosedElements()) {
1.51 if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
1.52 continue;
1.53 @@ -935,7 +944,7 @@
1.54 w.append(" this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class), union, null);\n");
1.55 w.write(" }\n");
1.56 }
1.57 - generateEnumConstantModel(w, ec);
1.58 + generateEnumConstantModel(w, ec, sharedProps);
1.59 }
1.60 // end of enum
1.61 w.write("}\n");
1.62 @@ -949,7 +958,7 @@
1.63 return ok;
1.64 }
1.65
1.66 - private boolean generateEnumConstantModel(Writer w, Element ec) throws IOException {
1.67 + private boolean generateEnumConstantModel(Writer w, Element ec, Collection<PropInfo> taken) throws IOException {
1.68 Model m = ec.getAnnotation(Model.class);
1.69 if (m == null) {
1.70 error("Each field in an enum needs to be annotated by @Model", ec);
1.71 @@ -973,7 +982,7 @@
1.72 // if (!generateOnChange(e, propsDeps, props, className, functionDeps)) {
1.73 // ok = false;
1.74 // }
1.75 - if (!generateProperties(ec, body, className, props, propsGetSet, propsDeps, functionDeps)) {
1.76 + if (!generateProperties(ec, body, className, props, propsGetSet, taken, propsDeps, functionDeps)) {
1.77 ok = false;
1.78 }
1.79 // if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) {
1.80 @@ -989,14 +998,17 @@
1.81 // ok = false;
1.82 // }
1.83 w.write(" public static final ");
1.84 - generateClassBody(w, className, body, ec, props, functionDeps, propsDeps, propsGetSet, functions, onReceiveType);
1.85 + generateClassBody(w, className, body, ec, props,
1.86 + functionDeps, propsDeps, propsGetSet, taken,
1.87 + functions, onReceiveType
1.88 + );
1.89 return ok;
1.90 }
1.91
1.92 private boolean generateProperties(
1.93 Element where,
1.94 Writer w, String className, Prprt[] properties,
1.95 - Collection<PropInfo> props,
1.96 + Collection<PropInfo> props, Collection<PropInfo> taken,
1.97 Map<String,Collection<String>> deps,
1.98 Map<String,Collection<String>> functionDeps
1.99 ) throws IOException {
1.100 @@ -1049,7 +1061,7 @@
1.101 gs[0],
1.102 castTo
1.103 );
1.104 - if (props.contains(pi)) {
1.105 + if (props.contains(pi) || (taken != null && taken.contains(pi))) {
1.106 ok = false;
1.107 error("Duplicated property " + p.name(), where);
1.108 }
2.1 --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java Tue Jan 28 15:14:14 2014 +0100
2.2 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java Tue Jan 28 16:18:33 2014 +0100
2.3 @@ -207,6 +207,39 @@
2.4 fail("Should contain warning about already defined 'type' property:" + msgs);
2.5 }
2.6 }
2.7 +
2.8 + @Test public void duplicatedByEnumTypePropertyIsSubType() throws IOException {
2.9 + String html = "<html><body>"
2.10 + + "</body></html>";
2.11 + String code = "package x.y.z;\n"
2.12 + + "import net.java.html.json.Model;\n"
2.13 + + "import net.java.html.json.Property;\n"
2.14 + + "@Model(className=\"XModel\", properties={\n"
2.15 + + " @Property(name=\"neco\", type=String.class)\n"
2.16 + + "})\n"
2.17 + + "enum type {\n"
2.18 + + " @Model(className=\"EModel\", properties={\n"
2.19 + + " @Property(name=\"prop\", type=int.class),\n"
2.20 + + " @Property(name=\"type\", type=String.class)\n"
2.21 + + " })\n"
2.22 + + " E\n"
2.23 + + "}\n";
2.24 +
2.25 + Compile c = Compile.create(html, code);
2.26 + assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
2.27 + boolean ok = false;
2.28 + StringBuilder msgs = new StringBuilder();
2.29 + for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
2.30 + String msg = e.getMessage(Locale.ENGLISH);
2.31 + if (msg.contains("uplicated prop")) {
2.32 + ok = true;
2.33 + }
2.34 + msgs.append("\n").append(msg);
2.35 + }
2.36 + if (!ok) {
2.37 + fail("Should contain warning about already defined 'type' property:" + msgs);
2.38 + }
2.39 + }
2.40
2.41 @Test public void warnOnNonStatic() throws IOException {
2.42 String html = "<html><body>"