#257348: Allocate @Model properties lazily to avoid StackOverFlowError
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 31 Dec 2015 08:20:05 +0100
changeset 10393ef632633f36
parent 1038 c1aabfb0bbb7
child 1040 6429e051b1de
#257348: Allocate @Model properties lazily to avoid StackOverFlowError
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/org/netbeans/html/json/impl/InfinityTest.java
src/main/javadoc/overview.html
     1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Sat Dec 26 06:31:49 2015 +0100
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Thu Dec 31 08:20:05 2015 +0100
     1.3 @@ -292,7 +292,7 @@
     1.4                          boolean isPrimitive[] = {false};
     1.5                          String tn = checkType(p, isModel, isEnum, isPrimitive);
     1.6                          if (isModel[0]) {
     1.7 -                            w.write("    prop_" + p.name() + " = new " + tn + "();\n");
     1.8 +                            w.write("    prop_" + p.name() + " = this;\n");
     1.9                          }
    1.10                      }
    1.11                  }
    1.12 @@ -658,10 +658,18 @@
    1.13                  boolean isEnum[] = { false };
    1.14                  boolean isPrimitive[] = { false };
    1.15                  checkType(p, isModel, isEnum, isPrimitive);
    1.16 -                w.write("  private " + tn + " prop_" + p.name() + ";\n");
    1.17 +                if (isModel[0]) {
    1.18 +                    w.write("  private /*" + tn + "*/Object prop_" + p.name() + ";\n");
    1.19 +
    1.20 +                } else {
    1.21 +                    w.write("  private " + tn + " prop_" + p.name() + ";\n");
    1.22 +                }
    1.23                  w.write("  public " + tn + " " + gs[0] + "() {\n");
    1.24                  w.write("    proto.accessProperty(\"" + p.name() + "\");\n");
    1.25 -                w.write("    return prop_" + p.name() + ";\n");
    1.26 +                if (isModel[0]) {
    1.27 +                    w.write("    if (prop_" + p.name() + " == this) prop_" + p.name() + " = new " + tn +"();\n");
    1.28 +                }
    1.29 +                w.write("    return (" + tn + ")prop_" + p.name() + ";\n");
    1.30                  w.write("  }\n");
    1.31                  w.write("  public void " + gs[1] + "(" + tn + " v) {\n");
    1.32                  w.write("    proto.verifyUnlocked();\n");
    1.33 @@ -1712,7 +1720,7 @@
    1.34                      w.write("    ret.prop_" + p.name() + " = " + gs[0] + "();\n");
    1.35                      continue;
    1.36                  }
    1.37 -                w.write("    ret.prop_" + p.name() + " =  " + gs[0] + "()  == null ? null : prop_" + p.name() + ".clone();\n");
    1.38 +                w.write("    ret.prop_" + p.name() + " =  " + gs[0] + "()  == null ? null : " + gs[0] + "().clone();\n");
    1.39              } else {
    1.40                  w.write("    proto.cloneList(ret." + gs[0] + "(), ctx, prop_" + p.name() + ");\n");
    1.41              }
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/InfinityTest.java	Thu Dec 31 08:20:05 2015 +0100
     2.3 @@ -0,0 +1,92 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     2.8 + *
     2.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    2.10 + * Other names may be trademarks of their respective owners.
    2.11 + *
    2.12 + * The contents of this file are subject to the terms of either the GNU
    2.13 + * General Public License Version 2 only ("GPL") or the Common
    2.14 + * Development and Distribution License("CDDL") (collectively, the
    2.15 + * "License"). You may not use this file except in compliance with the
    2.16 + * License. You can obtain a copy of the License at
    2.17 + * http://www.netbeans.org/cddl-gplv2.html
    2.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    2.19 + * specific language governing permissions and limitations under the
    2.20 + * License.  When distributing the software, include this License Header
    2.21 + * Notice in each file and include the License file at
    2.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    2.23 + * particular file as subject to the "Classpath" exception as provided
    2.24 + * by Oracle in the GPL Version 2 section of the License file that
    2.25 + * accompanied this code. If applicable, add the following below the
    2.26 + * License Header, with the fields enclosed by brackets [] replaced by
    2.27 + * your own identifying information:
    2.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    2.29 + *
    2.30 + * Contributor(s):
    2.31 + *
    2.32 + * The Original Software is NetBeans. The Initial Developer of the Original
    2.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    2.34 + *
    2.35 + * If you wish your version of this file to be governed by only the CDDL
    2.36 + * or only the GPL Version 2, indicate your decision by adding
    2.37 + * "[Contributor] elects to include this software in this distribution
    2.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    2.39 + * single choice of license, a recipient has the option to distribute
    2.40 + * your version of this file under either the CDDL, the GPL Version 2 or
    2.41 + * to extend the choice of license to its licensees as provided above.
    2.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    2.43 + * Version 2 license, then the option applies only if the new code is
    2.44 + * made subject to such option by the copyright holder.
    2.45 + */
    2.46 +package org.netbeans.html.json.impl;
    2.47 +
    2.48 +import net.java.html.json.Model;
    2.49 +import net.java.html.json.Property;
    2.50 +import static org.testng.Assert.assertEquals;
    2.51 +import static org.testng.Assert.assertNotNull;
    2.52 +import static org.testng.Assert.assertNull;
    2.53 +import static org.testng.Assert.assertSame;
    2.54 +import org.testng.annotations.Test;
    2.55 +
    2.56 +@Model(className = "Infinity", properties = {
    2.57 +    @Property(name = "next", type = Infinity.class)
    2.58 +})
    2.59 +public class InfinityTest {
    2.60 +    @Test
    2.61 +    public void atLeastThousandStepsDeep() {
    2.62 +        Infinity infinity = new Infinity();
    2.63 +        int cnt = 0;
    2.64 +        while (++cnt < 1000) {
    2.65 +            infinity = infinity.getNext();
    2.66 +        }
    2.67 +        assertNotNull(infinity);
    2.68 +        assertEquals(cnt, 1000);
    2.69 +    }
    2.70 +
    2.71 +    @Test
    2.72 +    public void afterInitializationRemainsTheSame() {
    2.73 +        Infinity infinity = new Infinity();
    2.74 +        Infinity first = infinity.getNext();
    2.75 +        Infinity second = infinity.getNext();
    2.76 +        assertSame(first, second);
    2.77 +    }
    2.78 +
    2.79 +    @Test
    2.80 +    public void nullRemains() {
    2.81 +        Infinity infinity = new Infinity();
    2.82 +        infinity.setNext(null);
    2.83 +        assertNull(infinity.getNext(), "Remains null");
    2.84 +        assertNull(infinity.getNext(), "Again");
    2.85 +    }
    2.86 +
    2.87 +    @Test
    2.88 +    public void ownValueRemains() {
    2.89 +        Infinity infinity = new Infinity();
    2.90 +        Infinity n = new Infinity();
    2.91 +        infinity.setNext(n);
    2.92 +        assertEquals(infinity.getNext(), n, "Remains n");
    2.93 +        assertEquals(infinity.getNext(), n, "Again n");
    2.94 +    }
    2.95 +}
     3.1 --- a/src/main/javadoc/overview.html	Sat Dec 26 06:31:49 2015 +0100
     3.2 +++ b/src/main/javadoc/overview.html	Thu Dec 31 08:20:05 2015 +0100
     3.3 @@ -121,7 +121,8 @@
     3.4              enterprise OSGi ready</a>.
     3.5          Switched to <a target="_blank" href="https://netbeans.org/bugzilla/show_bug.cgi?id=257130">minified version 3.4.0</a>
     3.6          of <a target="_blank" href="http://knockoutjs.com">knockout.js</a>.
     3.7 -
     3.8 +        Better support for <a target="_blank" href="https://netbeans.org/bugzilla/show_bug.cgi?id=257348">
     3.9 +            recursive @Model definitions</a>.
    3.10          <h3>What's Been Improved in Version 1.2.3?</h3>
    3.11  
    3.12          One can control {@link net.java.html.json.OnReceive#headers() HTTP request headers}