It is probably more consistent to use constructors rather than factory methods union
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Tue, 28 Jan 2014 08:52:29 +0100
branchunion
changeset 4983e7700d27b99
parent 497 ee07b3c73e6b
child 499 07c20dc62794
It is probably more consistent to use constructors rather than factory methods
json-tck/src/main/java/net/java/html/json/tests/JSONTest.java
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Mon Jan 27 18:09:00 2014 +0100
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/JSONTest.java	Tue Jan 28 08:52:29 2014 +0100
     1.3 @@ -89,7 +89,7 @@
     1.4              "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
     1.5      }
     1.6      @KOTest public void unionJSONInABrowser() throws Throwable {
     1.7 -        Union u = Union.createOne(new Union.One(), "a");
     1.8 +        Union u = new Union(new Union.One(), "a");
     1.9          Object json;
    1.10          try {
    1.11              json = parseJSON(u.toString());
     2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Mon Jan 27 18:09:00 2014 +0100
     2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Tue Jan 28 08:52:29 2014 +0100
     2.3 @@ -611,29 +611,38 @@
     2.4                  // enum
     2.5                  StringBuilder factoryHeader = new StringBuilder();
     2.6                  Prprt firstArray = null;
     2.7 -                if (true) {
     2.8 -                    w.write("  private final Object union;\n");
     2.9 +                w.write("  private final Object union;\n");
    2.10 +
    2.11 +                w.append("  public ").append(className).append("() {\n");
    2.12 +                boolean defaultGenerated = false;
    2.13 +                for (Element ec : e.getEnclosedElements()) {
    2.14 +                    if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    2.15 +                        continue;
    2.16 +                    }
    2.17 +                    Model em = ec.getAnnotation(Model.class);
    2.18 +                    if (em == null) {
    2.19 +                        continue;
    2.20 +                    }
    2.21 +                    defaultGenerated = true;
    2.22 +                    w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class),");
    2.23 +                    w.append(" new ").append(em.className()).append("());\n");
    2.24 +                    break;
    2.25 +                }
    2.26 +                if (!defaultGenerated) {
    2.27 +                    w.append("    throw new IllegalStateException();\n");
    2.28 +                }
    2.29 +                w.append("  }\n");
    2.30 +                for (Element ec : e.getEnclosedElements()) {
    2.31 +                    if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    2.32 +                        continue;
    2.33 +                    }
    2.34 +                    Model em = ec.getAnnotation(Model.class);
    2.35 +                    if (em == null) {
    2.36 +                        continue;
    2.37 +                    }
    2.38 +                    factoryHeader.setLength(0);
    2.39                      
    2.40 -                    w.append("  public ").append(className).append("() {\n");
    2.41 -                    boolean defaultGenerated = false;
    2.42 -                    for (Element ec : e.getEnclosedElements()) {
    2.43 -                        if (ec.getKind() != ElementKind.ENUM_CONSTANT) {
    2.44 -                            continue;
    2.45 -                        }
    2.46 -                        Model em = ec.getAnnotation(Model.class);
    2.47 -                        if (em == null) {
    2.48 -                            continue;
    2.49 -                        }
    2.50 -                        defaultGenerated = true;
    2.51 -                        w.append("    this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class),");
    2.52 -                        w.append(" new ").append(em.className()).append("());\n");
    2.53 -                        break;
    2.54 -                    }
    2.55 -                    if (!defaultGenerated) {
    2.56 -                        w.append("    throw new IllegalStateException();\n");
    2.57 -                    }
    2.58 -                    w.append("  }\n");
    2.59 -                    w.append("  private ").append(className).append("(Object union");
    2.60 +                    w.append("  public ").append(className).append("(").append(em.className()).append(" union");
    2.61                      for (Prprt p : props) {
    2.62                          if (p.array()) {
    2.63                              if (firstArray == null) {
    2.64 @@ -901,22 +910,11 @@
    2.65                      w.write("    public " + em.className() + " get" + em.className()+ "() {\n");
    2.66                      w.write("      return union instanceof " + em.className()+ " ? (" + em.className() + ")union : null;\n");
    2.67                      w.write("    }\n");
    2.68 -                    w.write("    public static " + className + " create" + em.className()+ " (" + em.className() + " union");
    2.69 -                    w.write(factoryHeader.toString());
    2.70 -                    w.write(") { return new " + className + "(union");
    2.71 -                    for (Prprt p : props) {
    2.72 -                        if (p.array()) {
    2.73 -                            continue;
    2.74 -                        }
    2.75 -                        w.write(", ");
    2.76 -                        w.write(p.name());
    2.77 +                    if (props.length > 0) {
    2.78 +                        w.write("    public " + className + "(" + em.className() + " union) {\n");
    2.79 +                        w.append("      this(net.java.html.BrwsrCtx.findDefault(").append(className).append(".class), union, null);\n");
    2.80 +                        w.write("    }\n");
    2.81                      }
    2.82 -                    if (firstArray != null) {
    2.83 -                        w.write(", ");
    2.84 -                        w.write(firstArray.name());
    2.85 -                    }
    2.86 -                    
    2.87 -                    w.write("); }\n");
    2.88                      generateEnumConstantModel(w, ec);
    2.89                  }
    2.90                  // end of enum
     3.1 --- a/json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java	Mon Jan 27 18:09:00 2014 +0100
     3.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/PlainEnumTest.java	Tue Jan 28 08:52:29 2014 +0100
     3.3 @@ -54,7 +54,7 @@
     3.4   */
     3.5  public class PlainEnumTest {
     3.6      @Test public void unionA() {
     3.7 -        Union on = Union.createA(new Union.A(9), 11, 1.1);
     3.8 +        Union on = new Union(new Union.A(9), 11, 1.1);
     3.9          assertEquals(on.getX(), 11);
    3.10          assertEquals(on.getY(), 1.1);
    3.11          
    3.12 @@ -65,7 +65,7 @@
    3.13      }
    3.14  
    3.15      @Test public void unionB() {
    3.16 -        Union on = Union.createB(new Union.B("9.9"), 11, 1.1);
    3.17 +        Union on = new Union(new Union.B("9.9"), 11, 1.1);
    3.18          assertEquals(on.getX(), 11);
    3.19          assertEquals(on.getY(), 1.1);
    3.20          
    3.21 @@ -76,7 +76,7 @@
    3.22      }
    3.23  
    3.24      @Test public void cloneUnionB() {
    3.25 -        Union old = Union.createB(new Union.B("9.9"), 11, 1.1);
    3.26 +        Union old = new Union(new Union.B("9.9"), 11, 1.1);
    3.27          Union on = old.clone();
    3.28          assertNotSame(old.getB(), on.getB());
    3.29          
    3.30 @@ -118,7 +118,7 @@
    3.31      }
    3.32      
    3.33      @Test public void serializeAsASingleObject() {
    3.34 -        Union on = Union.createB(new Union.B("9.9"), 11, 1.1);
    3.35 +        Union on = new Union(new Union.B("9.9"), 11, 1.1);
    3.36          String json = on.toString();
    3.37          assertNotEquals(json.indexOf("\"9.9\""), -1, "The string 9.9 should be in the output");
    3.38          assertNotEquals(json.indexOf("Abc"), -1, "The property 'Abc' should be in");