# HG changeset patch # User Jaroslav Tulach # Date 1451546405 -3600 # Node ID 3ef632633f36fe999b0f450ab142559da24e6fdc # Parent c1aabfb0bbb77d2d7ec109478016a983d3c14a10 #257348: Allocate @Model properties lazily to avoid StackOverFlowError diff -r c1aabfb0bbb7 -r 3ef632633f36 json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Sat Dec 26 06:31:49 2015 +0100 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Thu Dec 31 08:20:05 2015 +0100 @@ -292,7 +292,7 @@ boolean isPrimitive[] = {false}; String tn = checkType(p, isModel, isEnum, isPrimitive); if (isModel[0]) { - w.write(" prop_" + p.name() + " = new " + tn + "();\n"); + w.write(" prop_" + p.name() + " = this;\n"); } } } @@ -658,10 +658,18 @@ boolean isEnum[] = { false }; boolean isPrimitive[] = { false }; checkType(p, isModel, isEnum, isPrimitive); - w.write(" private " + tn + " prop_" + p.name() + ";\n"); + if (isModel[0]) { + w.write(" private /*" + tn + "*/Object prop_" + p.name() + ";\n"); + + } else { + w.write(" private " + tn + " prop_" + p.name() + ";\n"); + } w.write(" public " + tn + " " + gs[0] + "() {\n"); w.write(" proto.accessProperty(\"" + p.name() + "\");\n"); - w.write(" return prop_" + p.name() + ";\n"); + if (isModel[0]) { + w.write(" if (prop_" + p.name() + " == this) prop_" + p.name() + " = new " + tn +"();\n"); + } + w.write(" return (" + tn + ")prop_" + p.name() + ";\n"); w.write(" }\n"); w.write(" public void " + gs[1] + "(" + tn + " v) {\n"); w.write(" proto.verifyUnlocked();\n"); @@ -1712,7 +1720,7 @@ w.write(" ret.prop_" + p.name() + " = " + gs[0] + "();\n"); continue; } - w.write(" ret.prop_" + p.name() + " = " + gs[0] + "() == null ? null : prop_" + p.name() + ".clone();\n"); + w.write(" ret.prop_" + p.name() + " = " + gs[0] + "() == null ? null : " + gs[0] + "().clone();\n"); } else { w.write(" proto.cloneList(ret." + gs[0] + "(), ctx, prop_" + p.name() + ");\n"); } diff -r c1aabfb0bbb7 -r 3ef632633f36 json/src/test/java/org/netbeans/html/json/impl/InfinityTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/json/src/test/java/org/netbeans/html/json/impl/InfinityTest.java Thu Dec 31 08:20:05 2015 +0100 @@ -0,0 +1,92 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.json.impl; + +import net.java.html.json.Model; +import net.java.html.json.Property; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertSame; +import org.testng.annotations.Test; + +@Model(className = "Infinity", properties = { + @Property(name = "next", type = Infinity.class) +}) +public class InfinityTest { + @Test + public void atLeastThousandStepsDeep() { + Infinity infinity = new Infinity(); + int cnt = 0; + while (++cnt < 1000) { + infinity = infinity.getNext(); + } + assertNotNull(infinity); + assertEquals(cnt, 1000); + } + + @Test + public void afterInitializationRemainsTheSame() { + Infinity infinity = new Infinity(); + Infinity first = infinity.getNext(); + Infinity second = infinity.getNext(); + assertSame(first, second); + } + + @Test + public void nullRemains() { + Infinity infinity = new Infinity(); + infinity.setNext(null); + assertNull(infinity.getNext(), "Remains null"); + assertNull(infinity.getNext(), "Again"); + } + + @Test + public void ownValueRemains() { + Infinity infinity = new Infinity(); + Infinity n = new Infinity(); + infinity.setNext(n); + assertEquals(infinity.getNext(), n, "Remains n"); + assertEquals(infinity.getNext(), n, "Again n"); + } +} diff -r c1aabfb0bbb7 -r 3ef632633f36 src/main/javadoc/overview.html --- a/src/main/javadoc/overview.html Sat Dec 26 06:31:49 2015 +0100 +++ b/src/main/javadoc/overview.html Thu Dec 31 08:20:05 2015 +0100 @@ -121,7 +121,8 @@ enterprise OSGi ready. Switched to minified version 3.4.0 of knockout.js. - + Better support for + recursive @Model definitions.

What's Been Improved in Version 1.2.3?

One can control {@link net.java.html.json.OnReceive#headers() HTTP request headers}