#256178: Only generate the constructor with arguments if it can be compiled - e.g. if the number of arguments in not higher than 255.
1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Sat Oct 24 20:44:25 2015 +0200
1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Wed Oct 28 19:29:17 2015 +0100
1.3 @@ -270,9 +270,11 @@
1.4 }
1.5 w.append(" };\n");
1.6 if (props.length > 0) {
1.7 - w.append(" public ").append(className).append("(");
1.8 + StringBuilder constructorWithArguments = new StringBuilder();
1.9 + constructorWithArguments.append(" public ").append(className).append("(");
1.10 Prprt firstArray = null;
1.11 String sep = "";
1.12 + int parameterCount = 0;
1.13 for (Prprt p : props) {
1.14 if (p.array()) {
1.15 if (firstArray == null) {
1.16 @@ -281,11 +283,12 @@
1.17 continue;
1.18 }
1.19 String tn = typeName(p);
1.20 - w.write(sep);
1.21 - w.write(tn);
1.22 + constructorWithArguments.append(sep);
1.23 + constructorWithArguments.append(tn);
1.24 String[] third = toGetSet(p.name(), tn, false);
1.25 - w.write(" " + third[2]);
1.26 + constructorWithArguments.append(" ").append(third[2]);
1.27 sep = ", ";
1.28 + parameterCount++;
1.29 }
1.30 if (firstArray != null) {
1.31 String tn;
1.32 @@ -293,25 +296,29 @@
1.33 boolean[] isEnum = {false};
1.34 boolean isPrimitive[] = {false};
1.35 tn = checkType(firstArray, isModel, isEnum, isPrimitive);
1.36 - w.write(sep);
1.37 - w.write(tn);
1.38 + constructorWithArguments.append(sep);
1.39 + constructorWithArguments.append(tn);
1.40 String[] third = toGetSet(firstArray.name(), tn, true);
1.41 - w.write("... " + third[2]);
1.42 + constructorWithArguments.append("... ").append(third[2]);
1.43 + parameterCount++;
1.44 }
1.45 - w.append(") {\n");
1.46 - w.append(" this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n");
1.47 + constructorWithArguments.append(") {\n");
1.48 + constructorWithArguments.append(" this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n");
1.49 for (Prprt p : props) {
1.50 if (p.array()) {
1.51 continue;
1.52 }
1.53 String[] third = toGetSet(p.name(), null, false);
1.54 - w.write(" this.prop_" + p.name() + " = " + third[2] + ";\n");
1.55 + constructorWithArguments.append(" this.prop_" + p.name() + " = " + third[2] + ";\n");
1.56 }
1.57 if (firstArray != null) {
1.58 String[] third = toGetSet(firstArray.name(), null, true);
1.59 - w.write(" proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n");
1.60 + constructorWithArguments.append(" proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n");
1.61 }
1.62 - w.append(" };\n");
1.63 + constructorWithArguments.append(" };\n");
1.64 + if (parameterCount < 255) {
1.65 + w.write(constructorWithArguments.toString());
1.66 + }
1.67 }
1.68 w.append(" private static class Html4JavaType extends org.netbeans.html.json.spi.Proto.Type<").append(className).append("> {\n");
1.69 w.append(" private Html4JavaType() {\n super(").append(className).append(".class, ").
2.1 --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java Sat Oct 24 20:44:25 2015 +0200
2.2 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java Wed Oct 28 19:29:17 2015 +0100
2.3 @@ -144,6 +144,68 @@
2.4 }
2.5 }
2.6
2.7 + @Test public void tooManyProperties() throws IOException {
2.8 + manyProperties(255, false, 0);
2.9 + }
2.10 +
2.11 + @Test public void tooManyArrayPropertiesIsOK() throws IOException {
2.12 + manyProperties(0, true, 300);
2.13 + }
2.14 +
2.15 + @Test public void justEnoughProperties() throws IOException {
2.16 + manyProperties(254, true, 0);
2.17 + }
2.18 +
2.19 + @Test public void justEnoughPropertiesWithArrayOne() throws IOException {
2.20 + manyProperties(253, true, 300);
2.21 + }
2.22 +
2.23 + @Test public void justEnoughPropertiesButOneArrayOne() throws IOException {
2.24 + manyProperties(254, false, 300);
2.25 + }
2.26 +
2.27 + private void manyProperties(
2.28 + int cnt, boolean constructorWithParams, int arrayCnt
2.29 + ) throws IOException {
2.30 + String html = "<html><body>"
2.31 + + "</body></html>";
2.32 + StringBuilder code = new StringBuilder();
2.33 + code.append("package x.y.z;\n"
2.34 + + "import net.java.html.json.Model;\n"
2.35 + + "import net.java.html.json.Property;\n"
2.36 + + "@Model(className=\"XModel\", properties={\n"
2.37 + );
2.38 + for (int i = 1; i <= cnt; i++) {
2.39 + code.append(" @Property(name=\"prop").append(i).append("\", ");
2.40 + code.append("type=int.class),\n");
2.41 + }
2.42 + for (int i = 1; i <= arrayCnt; i++) {
2.43 + code.append(" @Property(name=\"array").append(i).append("\", ");
2.44 + code.append("array=true, ");
2.45 + code.append("type=int.class),\n");
2.46 + }
2.47 + code.append(""
2.48 + + "})\n"
2.49 + + "class X {\n"
2.50 + + " static {\n"
2.51 + + " new XModel();\n"
2.52 + + " new XModel("
2.53 + );
2.54 + if (constructorWithParams) {
2.55 + code.append("0");
2.56 + for (int i = 1; i < cnt; i++) {
2.57 + code.append(",\n").append(i);
2.58 + }
2.59 + }
2.60 + code.append(");\n"
2.61 + + " }\n"
2.62 + + "}\n"
2.63 + );
2.64 +
2.65 + Compile c = Compile.create(html, code.toString());
2.66 + assertTrue(c.getErrors().isEmpty(), "Compiles OK: " + c.getErrors());
2.67 + }
2.68 +
2.69 @Test public void writeableComputedPropertyMissingWrite() throws IOException {
2.70 String html = "<html><body>"
2.71 + "</body></html>";