# HG changeset patch # User Jaroslav Tulach # Date 1446056957 -3600 # Node ID 267ca1bfeb6f0fb6f09c8203494e5156a9815bca # Parent c7cfc27280470c53dceeff6ada346d5159ef3cbf #256178: Only generate the constructor with arguments if it can be compiled - e.g. if the number of arguments in not higher than 255. diff -r c7cfc2728047 -r 267ca1bfeb6f json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Sat Oct 24 20:44:25 2015 +0200 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Wed Oct 28 19:29:17 2015 +0100 @@ -270,9 +270,11 @@ } w.append(" };\n"); if (props.length > 0) { - w.append(" public ").append(className).append("("); + StringBuilder constructorWithArguments = new StringBuilder(); + constructorWithArguments.append(" public ").append(className).append("("); Prprt firstArray = null; String sep = ""; + int parameterCount = 0; for (Prprt p : props) { if (p.array()) { if (firstArray == null) { @@ -281,11 +283,12 @@ continue; } String tn = typeName(p); - w.write(sep); - w.write(tn); + constructorWithArguments.append(sep); + constructorWithArguments.append(tn); String[] third = toGetSet(p.name(), tn, false); - w.write(" " + third[2]); + constructorWithArguments.append(" ").append(third[2]); sep = ", "; + parameterCount++; } if (firstArray != null) { String tn; @@ -293,25 +296,29 @@ boolean[] isEnum = {false}; boolean isPrimitive[] = {false}; tn = checkType(firstArray, isModel, isEnum, isPrimitive); - w.write(sep); - w.write(tn); + constructorWithArguments.append(sep); + constructorWithArguments.append(tn); String[] third = toGetSet(firstArray.name(), tn, true); - w.write("... " + third[2]); + constructorWithArguments.append("... ").append(third[2]); + parameterCount++; } - w.append(") {\n"); - w.append(" this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n"); + constructorWithArguments.append(") {\n"); + constructorWithArguments.append(" this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n"); for (Prprt p : props) { if (p.array()) { continue; } String[] third = toGetSet(p.name(), null, false); - w.write(" this.prop_" + p.name() + " = " + third[2] + ";\n"); + constructorWithArguments.append(" this.prop_" + p.name() + " = " + third[2] + ";\n"); } if (firstArray != null) { String[] third = toGetSet(firstArray.name(), null, true); - w.write(" proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n"); + constructorWithArguments.append(" proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n"); } - w.append(" };\n"); + constructorWithArguments.append(" };\n"); + if (parameterCount < 255) { + w.write(constructorWithArguments.toString()); + } } w.append(" private static class Html4JavaType extends org.netbeans.html.json.spi.Proto.Type<").append(className).append("> {\n"); w.append(" private Html4JavaType() {\n super(").append(className).append(".class, "). diff -r c7cfc2728047 -r 267ca1bfeb6f json/src/test/java/net/java/html/json/ModelProcessorTest.java --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java Sat Oct 24 20:44:25 2015 +0200 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java Wed Oct 28 19:29:17 2015 +0100 @@ -144,6 +144,68 @@ } } + @Test public void tooManyProperties() throws IOException { + manyProperties(255, false, 0); + } + + @Test public void tooManyArrayPropertiesIsOK() throws IOException { + manyProperties(0, true, 300); + } + + @Test public void justEnoughProperties() throws IOException { + manyProperties(254, true, 0); + } + + @Test public void justEnoughPropertiesWithArrayOne() throws IOException { + manyProperties(253, true, 300); + } + + @Test public void justEnoughPropertiesButOneArrayOne() throws IOException { + manyProperties(254, false, 300); + } + + private void manyProperties( + int cnt, boolean constructorWithParams, int arrayCnt + ) throws IOException { + String html = "" + + ""; + StringBuilder code = new StringBuilder(); + code.append("package x.y.z;\n" + + "import net.java.html.json.Model;\n" + + "import net.java.html.json.Property;\n" + + "@Model(className=\"XModel\", properties={\n" + ); + for (int i = 1; i <= cnt; i++) { + code.append(" @Property(name=\"prop").append(i).append("\", "); + code.append("type=int.class),\n"); + } + for (int i = 1; i <= arrayCnt; i++) { + code.append(" @Property(name=\"array").append(i).append("\", "); + code.append("array=true, "); + code.append("type=int.class),\n"); + } + code.append("" + + "})\n" + + "class X {\n" + + " static {\n" + + " new XModel();\n" + + " new XModel(" + ); + if (constructorWithParams) { + code.append("0"); + for (int i = 1; i < cnt; i++) { + code.append(",\n").append(i); + } + } + code.append(");\n" + + " }\n" + + "}\n" + ); + + Compile c = Compile.create(html, code.toString()); + assertTrue(c.getErrors().isEmpty(), "Compiles OK: " + c.getErrors()); + } + @Test public void writeableComputedPropertyMissingWrite() throws IOException { String html = "" + "";