A bit of detection of duplicated properties union
authorJaroslav Tulach <jaroslav.tulach@netbeans.org>
Tue, 28 Jan 2014 15:14:14 +0100
branchunion
changeset 501688d1b6299d7
parent 500 643d88ddac2a
child 502 d09a640f5a56
A bit of detection of duplicated properties
json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/Compile.java
json/src/test/java/net/java/html/json/ModelProcessorTest.java
     1.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Tue Jan 28 14:36:47 2014 +0100
     1.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java	Tue Jan 28 15:14:14 2014 +0100
     1.3 @@ -448,6 +448,9 @@
     1.4              if (p == null) {
     1.5                  continue;
     1.6              }
     1.7 +            if (prop == props.length) {
     1.8 +                continue;
     1.9 +            }
    1.10              boolean[] isModel = { false };
    1.11              boolean[] isEnum = { false };
    1.12              boolean isPrimitive[] = { false };
    1.13 @@ -1039,14 +1042,19 @@
    1.14                  }
    1.15                  w.write("  }\n");
    1.16              }
    1.17 -
    1.18 -            props.add(new PropInfo(
    1.19 +            final PropInfo pi = new PropInfo(
    1.20                  p.name(),
    1.21                  gs[2],
    1.22                  gs[3],
    1.23                  gs[0],
    1.24                  castTo
    1.25 -            ));
    1.26 +            );
    1.27 +            if (props.contains(pi)) {
    1.28 +                ok = false;
    1.29 +                error("Duplicated property " + p.name(), where);
    1.30 +            }
    1.31 +
    1.32 +            props.add(pi);
    1.33          }
    1.34          return ok;
    1.35      }
    1.36 @@ -1140,14 +1148,19 @@
    1.37              w.write("      proto.releaseLock();\n");
    1.38              w.write("    }\n");
    1.39              w.write("  }\n");
    1.40 -
    1.41 -            props.add(new PropInfo(
    1.42 +            final PropInfo pi = new PropInfo(
    1.43                  e.getSimpleName().toString(),
    1.44                  gs[2],
    1.45                  null,
    1.46                  gs[0],
    1.47                  tn
    1.48 -            ));
    1.49 +            );
    1.50 +            if (props.contains(pi)) {
    1.51 +                ok = false;
    1.52 +                error("Duplicated property " + e.getSimpleName(), e);
    1.53 +            }
    1.54 +
    1.55 +            props.add(pi);
    1.56          }
    1.57          
    1.58          return ok;
    1.59 @@ -2341,5 +2354,27 @@
    1.60              this.p3 = p4;
    1.61              this.p4 = p5;
    1.62          }
    1.63 -    }
    1.64 +
    1.65 +        @Override
    1.66 +        public int hashCode() {
    1.67 +            int hash = 7;
    1.68 +            hash = 19 * hash + (this.p0 != null ? this.p0.hashCode() : 0);
    1.69 +            return hash;
    1.70 +        }
    1.71 +
    1.72 +        @Override
    1.73 +        public boolean equals(Object obj) {
    1.74 +            if (obj == null) {
    1.75 +                return false;
    1.76 +            }
    1.77 +            if (getClass() != obj.getClass()) {
    1.78 +                return false;
    1.79 +            }
    1.80 +            final PropInfo other = (PropInfo) obj;
    1.81 +            if ((this.p0 == null) ? (other.p0 != null) : !this.p0.equals(other.p0)) {
    1.82 +                return false;
    1.83 +            }
    1.84 +            return true;
    1.85 +        }
    1.86 +    } // end of PropInfo
    1.87  }
     2.1 --- a/json/src/test/java/net/java/html/json/Compile.java	Tue Jan 28 14:36:47 2014 +0100
     2.2 +++ b/json/src/test/java/net/java/html/json/Compile.java	Tue Jan 28 15:14:14 2014 +0100
     2.3 @@ -253,7 +253,7 @@
     2.4          return pkg;
     2.5      }
     2.6      private static String findCls(String java) throws IOException {
     2.7 -        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
     2.8 +        Pattern p = Pattern.compile("(class|enum)\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
     2.9          Matcher m = p.matcher(java);
    2.10          if (!m.find()) {
    2.11              throw new IOException("Can't find package declaration in the java file");
     3.1 --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Tue Jan 28 14:36:47 2014 +0100
     3.2 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Tue Jan 28 15:14:14 2014 +0100
     3.3 @@ -82,6 +82,132 @@
     3.4          }
     3.5      }
     3.6      
     3.7 +    @Test public void duplicatedProperty() throws IOException {
     3.8 +        String html = "<html><body>"
     3.9 +            + "</body></html>";
    3.10 +        String code = "package x.y.z;\n"
    3.11 +            + "import net.java.html.json.Model;\n"
    3.12 +            + "import net.java.html.json.Property;\n"
    3.13 +            + "@Model(className=\"XModel\", properties={\n"
    3.14 +            + "  @Property(name=\"prop\", type=String.class),\n"
    3.15 +            + "  @Property(name=\"prop\", type=int.class)\n"
    3.16 +            + "})\n"
    3.17 +            + "class X {\n"
    3.18 +            + "}\n";
    3.19 +        
    3.20 +        Compile c = Compile.create(html, code);
    3.21 +        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
    3.22 +        boolean ok = false;
    3.23 +        StringBuilder msgs = new StringBuilder();
    3.24 +        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
    3.25 +            String msg = e.getMessage(Locale.ENGLISH);
    3.26 +            if (msg.contains("uplicated prop")) {
    3.27 +                ok = true;
    3.28 +            }
    3.29 +            msgs.append("\n").append(msg);
    3.30 +        }
    3.31 +        if (!ok) {
    3.32 +            fail("Should contain warning about already defined property:" + msgs);
    3.33 +        }
    3.34 +    }
    3.35 +    
    3.36 +    @Test public void duplicatedComputedProperty() throws IOException {
    3.37 +        String html = "<html><body>"
    3.38 +            + "</body></html>";
    3.39 +        String code = "package x.y.z;\n"
    3.40 +            + "import net.java.html.json.Model;\n"
    3.41 +            + "import net.java.html.json.Property;\n"
    3.42 +            + "import net.java.html.json.ComputedProperty;\n"
    3.43 +            + "@Model(className=\"XModel\", properties={\n"
    3.44 +            + "  @Property(name=\"a\", type=String.class),\n"
    3.45 +            + "  @Property(name=\"prop\", type=String.class),\n"
    3.46 +            + "})\n"
    3.47 +            + "class X {\n"
    3.48 +            + "  @ComputedProperty static int prop(String a) {\n"
    3.49 +            + "    return 1;\n"
    3.50 +            + "  }\n"
    3.51 +            + "}\n";
    3.52 +        
    3.53 +        Compile c = Compile.create(html, code);
    3.54 +        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
    3.55 +        boolean ok = false;
    3.56 +        StringBuilder msgs = new StringBuilder();
    3.57 +        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
    3.58 +            String msg = e.getMessage(Locale.ENGLISH);
    3.59 +            if (msg.contains("uplicated prop")) {
    3.60 +                ok = true;
    3.61 +            }
    3.62 +            msgs.append("\n").append(msg);
    3.63 +        }
    3.64 +        if (!ok) {
    3.65 +            fail("Should contain warning about already defined property:" + msgs);
    3.66 +        }
    3.67 +    }
    3.68 +    
    3.69 +    @Test public void duplicatedInUnionProperty() throws IOException {
    3.70 +        String html = "<html><body>"
    3.71 +            + "</body></html>";
    3.72 +        String code = "package x.y.z;\n"
    3.73 +            + "import net.java.html.json.Model;\n"
    3.74 +            + "import net.java.html.json.Property;\n"
    3.75 +            + "@Model(className=\"XModel\", properties={\n"
    3.76 +            + "  @Property(name=\"prop\", type=String.class)\n"
    3.77 +            + "})\n"
    3.78 +            + "enum type {\n"
    3.79 +            + "  @Model(className=\"EModel\", properties={\n"
    3.80 +            + "    @Property(name=\"prop\", type=int.class)\n"
    3.81 +            + "  })\n"
    3.82 +            + "  E\n"
    3.83 +            + "}\n";
    3.84 +        
    3.85 +        Compile c = Compile.create(html, code);
    3.86 +        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
    3.87 +        boolean ok = false;
    3.88 +        StringBuilder msgs = new StringBuilder();
    3.89 +        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
    3.90 +            String msg = e.getMessage(Locale.ENGLISH);
    3.91 +            if (msg.contains("uplicated prop")) {
    3.92 +                ok = true;
    3.93 +            }
    3.94 +            msgs.append("\n").append(msg);
    3.95 +        }
    3.96 +        if (!ok) {
    3.97 +            fail("Should contain warning about already defined property:" + msgs);
    3.98 +        }
    3.99 +    }
   3.100 +    
   3.101 +    @Test public void duplicatedByEnumTypeProperty() throws IOException {
   3.102 +        String html = "<html><body>"
   3.103 +            + "</body></html>";
   3.104 +        String code = "package x.y.z;\n"
   3.105 +            + "import net.java.html.json.Model;\n"
   3.106 +            + "import net.java.html.json.Property;\n"
   3.107 +            + "@Model(className=\"XModel\", properties={\n"
   3.108 +            + "  @Property(name=\"type\", type=String.class)\n"
   3.109 +            + "})\n"
   3.110 +            + "enum type {\n"
   3.111 +            + "  @Model(className=\"EModel\", properties={\n"
   3.112 +            + "    @Property(name=\"prop\", type=int.class)\n"
   3.113 +            + "  })\n"
   3.114 +            + "  E\n"
   3.115 +            + "}\n";
   3.116 +        
   3.117 +        Compile c = Compile.create(html, code);
   3.118 +        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
   3.119 +        boolean ok = false;
   3.120 +        StringBuilder msgs = new StringBuilder();
   3.121 +        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
   3.122 +            String msg = e.getMessage(Locale.ENGLISH);
   3.123 +            if (msg.contains("uplicated prop")) {
   3.124 +                ok = true;
   3.125 +            }
   3.126 +            msgs.append("\n").append(msg);
   3.127 +        }
   3.128 +        if (!ok) {
   3.129 +            fail("Should contain warning about already defined 'type' property:" + msgs);
   3.130 +        }
   3.131 +    }
   3.132 +    
   3.133      @Test public void warnOnNonStatic() throws IOException {
   3.134          String html = "<html><body>"
   3.135              + "</body></html>";