#247470: @Property names can be Java keywords. Also providing special handling for 'class' which has getter accessClass to not conflict with Object.getClass
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 16 Oct 2014 09:12:59 +0200
changeset 8627cacce04dfd8
parent 861 52b0e31920c1
child 863 49eff98a9310
#247470: @Property names can be Java keywords. Also providing special handling for 'class' which has getter accessClass to not conflict with Object.getClass
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/KeywordsTest.java
     1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Fri Sep 26 15:44:39 2014 +0200
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Thu Oct 16 09:12:59 2014 +0200
     1.3 @@ -283,7 +283,8 @@
     1.4                          String tn = typeName(e, p);
     1.5                          w.write(sep);
     1.6                          w.write(tn);
     1.7 -                        w.write(" " + p.name());
     1.8 +                        String[] third = toGetSet(p.name(), tn, false);
     1.9 +                        w.write(" " + third[2]);
    1.10                          sep = ", ";
    1.11                      }
    1.12                      if (firstArray != null) {
    1.13 @@ -294,7 +295,8 @@
    1.14                          tn = checkType(firstArray, isModel, isEnum, isPrimitive);
    1.15                          w.write(sep);
    1.16                          w.write(tn);
    1.17 -                        w.write("... " + firstArray.name());
    1.18 +                        String[] third = toGetSet(firstArray.name(), tn, true);
    1.19 +                        w.write("... " + third[2]);
    1.20                      }
    1.21                      w.append(") {\n");
    1.22                      w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class));\n");
    1.23 @@ -302,10 +304,12 @@
    1.24                          if (p.array()) {
    1.25                              continue;
    1.26                          }
    1.27 -                        w.write("    this.prop_" + p.name() + " = " + p.name() + ";\n");
    1.28 +                        String[] third = toGetSet(p.name(), null, false);
    1.29 +                        w.write("    this.prop_" + p.name() + " = " + third[2] + ";\n");
    1.30                      }
    1.31                      if (firstArray != null) {
    1.32 -                        w.write("    proto.initTo(this.prop_" + firstArray.name() + ", " + firstArray.name() + ");\n");
    1.33 +                        String[] third = toGetSet(firstArray.name(), null, true);
    1.34 +                        w.write("    proto.initTo(this.prop_" + firstArray.name() + ", " + third[2] + ");\n");
    1.35                      }
    1.36                      w.append("  };\n");
    1.37                  }
    1.38 @@ -597,8 +601,8 @@
    1.39              
    1.40              props.add(new GetSet(
    1.41                  p.name(),
    1.42 -                gs[2],
    1.43 -                gs[3],
    1.44 +                gs[0],
    1.45 +                gs[1],
    1.46                  tn,
    1.47                  gs[3] == null && !p.array()
    1.48              ));
    1.49 @@ -726,7 +730,7 @@
    1.50  
    1.51              props.add(new GetSet(
    1.52                  e.getSimpleName().toString(),
    1.53 -                gs[2],
    1.54 +                gs[0],
    1.55                  null,
    1.56                  tn,
    1.57                  true
    1.58 @@ -738,32 +742,24 @@
    1.59  
    1.60      private static String[] toGetSet(String name, String type, boolean array) {
    1.61          String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
    1.62 -        String bck2brwsrType = "L" + type.replace('.', '_') + "_2";
    1.63 -        if ("int".equals(type)) {
    1.64 -            bck2brwsrType = "I";
    1.65 +        boolean clazz = "class".equals(name);
    1.66 +        String pref = clazz ? "access" : "get";
    1.67 +        if ("boolean".equals(type) && !array) {
    1.68 +            pref = "is";
    1.69          }
    1.70 -        if ("double".equals(type)) {
    1.71 -            bck2brwsrType = "D";
    1.72 -        }
    1.73 -        String pref = "get";
    1.74 -        if ("boolean".equals(type)) {
    1.75 -            pref = "is";
    1.76 -            bck2brwsrType = "Z";
    1.77 -        }
    1.78 -        final String nu = n.replace('.', '_');
    1.79          if (array) {
    1.80              return new String[] { 
    1.81 -                "get" + n,
    1.82 +                pref + n,
    1.83                  null,
    1.84 -                "get" + nu + "__Ljava_util_List_2",
    1.85 +                "a" + n,
    1.86                  null
    1.87              };
    1.88          }
    1.89          return new String[]{
    1.90              pref + n, 
    1.91              "set" + n, 
    1.92 -            pref + nu + "__" + bck2brwsrType,
    1.93 -            "set" + nu + "__V" + bck2brwsrType
    1.94 +            "a" + n,
    1.95 +            ""
    1.96          };
    1.97      }
    1.98  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/json/src/test/java/net/java/html/json/KeywordsTest.java	Thu Oct 16 09:12:59 2014 +0200
     2.3 @@ -0,0 +1,99 @@
     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 net.java.html.json;
    2.47 +
    2.48 +import static org.testng.Assert.*;
    2.49 +import org.testng.annotations.Test;
    2.50 +
    2.51 +@Model(className = "Keywords", properties = {
    2.52 +    @Property(name = "private", type = String.class),
    2.53 +    @Property(name = "public", type = double.class),
    2.54 +    @Property(name = "final", type = String.class),
    2.55 +    @Property(name = "int", type = int.class),
    2.56 +    @Property(name = "class", type = String.class),
    2.57 +//    @Property(name = "{", type = String.class),
    2.58 +    @Property(name = "array", type = KeywordsInArray.class)
    2.59 +})
    2.60 +public class KeywordsTest {
    2.61 +    @Model(className = "KeywordsInArray", properties = {
    2.62 +        @Property(name = "private", type = String.class, array = true),
    2.63 +        @Property(name = "public", type = double.class, array = true),
    2.64 +        @Property(name = "final", type = String.class, array = true),
    2.65 +        @Property(name = "int", type = int.class, array = true),
    2.66 +        @Property(name = "class", type = String.class, array = true),
    2.67 +//    @Property(name = "{", type = String.class),
    2.68 +        @Property(name = "array", type = Keywords.class, array = true)
    2.69 +    })
    2.70 +    static class KeywordsInArrayCntrl {
    2.71 +    }
    2.72 +    
    2.73 +    @Test public void verifyKeywordsClassCompiles() {
    2.74 +        Keywords k = new Keywords();
    2.75 +        k.setClass("c");
    2.76 +        k.setFinal("f");
    2.77 +        k.setInt(10);
    2.78 +        k.setPrivate("p");
    2.79 +        k.setPublic(42.0);
    2.80 +        
    2.81 +        assertEquals(k.accessClass(), "c");
    2.82 +        assertEquals(k.getFinal(), "f");
    2.83 +        assertEquals(k.getInt(), 10);
    2.84 +        assertEquals(k.getPrivate(), "p");
    2.85 +        assertEquals(k.getPublic(), 42.0);
    2.86 +    }
    2.87 +    
    2.88 +    @Test public void verifyKeywordsInArrayClassCompiles() {
    2.89 +        KeywordsInArray k = new KeywordsInArray();
    2.90 +        k.accessClass().add("c");
    2.91 +        k.getFinal().add("f");
    2.92 +        k.getInt().add(10);
    2.93 +        k.getPrivate().add("p");
    2.94 +        k.getPublic().add(42.0);
    2.95 +        
    2.96 +        assertEquals(k.accessClass().get(0), "c");
    2.97 +        assertEquals(k.getFinal().get(0), "f");
    2.98 +        assertEquals(k.getInt().get(0), Integer.valueOf(10));
    2.99 +        assertEquals(k.getPrivate().get(0), "p");
   2.100 +        assertEquals(k.getPublic().get(0), 42.0);
   2.101 +    }
   2.102 +}