Differentiate between the name of constant and the associated model class name union
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 24 Jan 2014 18:01:37 +0100
branchunion
changeset 496036bd062251a
parent 495 739510d3b4c2
child 497 ee07b3c73e6b
Differentiate between the name of constant and the associated model class name
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json-tck/src/main/java/net/java/html/json/tests/Uni.java
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Fri Jan 24 17:23:27 2014 +0100
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Fri Jan 24 18:01:37 2014 +0100
     1.3 @@ -88,6 +88,20 @@
     1.4          assert p2.getFirstName().equals(p.getFirstName()) : 
     1.5              "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
     1.6      }
     1.7 +    @KOTest public void unionJSONInABrowser() throws Throwable {
     1.8 +        Union u = Union.createOne(new Union.One(), "a");
     1.9 +        Object json;
    1.10 +        try {
    1.11 +            json = parseJSON(u.toString());
    1.12 +        } catch (Throwable ex) {
    1.13 +            throw new IllegalStateException("Can't parse " + u).initCause(ex);
    1.14 +        }
    1.15 +        
    1.16 +        Union u2 = Models.fromRaw(newContext(), Union.class, json);
    1.17 +        
    1.18 +        assert u.getUni().equals(u2.getUni()) : 
    1.19 +            "Should be the same: " + u.getUni() + " != " + u2.getUni();
    1.20 +    }
    1.21      
    1.22      @KOTest public void toJSONWithEscapeCharactersInABrowser() throws Throwable {
    1.23          Person p = Models.bind(new Person(), newContext());
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/Uni.java	Fri Jan 24 18:01:37 2014 +0100
     2.3 @@ -0,0 +1,61 @@
     2.4 +/**
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2013-2013 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-2013 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 net.java.html.json.tests;
    2.47 +
    2.48 +import net.java.html.json.Model;
    2.49 +import net.java.html.json.Property;
    2.50 +
    2.51 +@Model(className = "Union", properties = {
    2.52 +    @Property(name="names", type = String.class, array = true)
    2.53 +})
    2.54 +enum Uni {
    2.55 +    @Model(className = "One", properties = {
    2.56 +        @Property(name = "ones", type = String.class, array = true)
    2.57 +    })
    2.58 +    one,
    2.59 +    @Model(className = "Two", properties = {
    2.60 +        @Property(name = "twos", type = String.class, array = true)
    2.61 +        
    2.62 +    })
    2.63 +    two;
    2.64 +}
     3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Fri Jan 24 17:23:27 2014 +0100
     3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Fri Jan 24 18:01:37 2014 +0100
     3.3 @@ -617,9 +617,13 @@
     3.4                          if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
     3.5                              continue;
     3.6                          }
     3.7 +                        Model em = ec.getAnnotation(Model.class);
     3.8 +                        if (em == null) {
     3.9 +                            continue;
    3.10 +                        }
    3.11                          defaultGenerated = true;
    3.12                          w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class),");
    3.13 -                        w.append(" new ").append(ec.getSimpleName()).append("());\n");
    3.14 +                        w.append(" new ").append(em.className()).append("());\n");
    3.15                          break;
    3.16                      }
    3.17                      if (!defaultGenerated) {
    3.18 @@ -862,7 +866,11 @@
    3.19                      if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    3.20                          continue;
    3.21                      }
    3.22 -                    w.write("    if (union instanceof " + ec.getSimpleName() + ") return " + inPckName(e) + "." + ec.getSimpleName() + ";\n");
    3.23 +                    Model em = ec.getAnnotation(Model.class);
    3.24 +                    if (em == null) {
    3.25 +                        continue;
    3.26 +                    }
    3.27 +                    w.write("    if (union instanceof " + em.className()+ ") return " + inPckName(e) + "." + ec.getSimpleName() + ";\n");
    3.28                  }
    3.29                  w.write("    return null;\n");
    3.30                  w.write("  }\n");
    3.31 @@ -870,10 +878,14 @@
    3.32                      if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    3.33                          continue;
    3.34                      }
    3.35 -                    w.write("    public " + ec.getSimpleName() + " get" + ec.getSimpleName() + "() {\n");
    3.36 -                    w.write("      return union instanceof " + ec.getSimpleName() + " ? (" + ec.getSimpleName() + ")union : null;\n");
    3.37 +                    Model em = ec.getAnnotation(Model.class);
    3.38 +                    if (em == null) {
    3.39 +                        continue;
    3.40 +                    }
    3.41 +                    w.write("    public " + em.className() + " get" + em.className()+ "() {\n");
    3.42 +                    w.write("      return union instanceof " + em.className()+ " ? (" + em.className() + ")union : null;\n");
    3.43                      w.write("    }\n");
    3.44 -                    w.write("    public static " + className + " create" + ec.getSimpleName() + " (" + ec.getSimpleName() + " union");
    3.45 +                    w.write("    public static " + className + " create" + em.className()+ " (" + em.className() + " union");
    3.46                      w.write(factoryHeader.toString());
    3.47                      w.write(") { return new " + className + "(union");
    3.48                      for (Prprt p : props) {
    3.49 @@ -1873,7 +1885,11 @@
    3.50              if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    3.51                  continue;
    3.52              }
    3.53 -            w.write("    if (union instanceof " + ec.getSimpleName() + ") newUnion = ((" + ec.getSimpleName() + ")union).clone(ctx);\n");
    3.54 +            Model em = ec.getAnnotation(Model.class);
    3.55 +            if (em == null) {
    3.56 +                continue;
    3.57 +            }
    3.58 +            w.write("    if (union instanceof " + em.className() + ") newUnion = ((" + em.className() + ")union).clone(ctx);\n");
    3.59          }
    3.60          w.write("    " + className + " ret = new " + className + "(ctx, newUnion);\n");
    3.61          for (Prprt p : props) {