@ComputedProperty method has to be static
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 08 Aug 2013 10:51:40 +0200
changeset 227ab5cfdd5623e
parent 226 1e99d5bac244
child 228 0f34c04bec7d
child 229 8eb446ccb00c
@ComputedProperty method has to be static
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
json/src/test/java/net/java/html/json/ModelProcessorTest.java
     1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Wed Aug 07 15:34:10 2013 +0200
     1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Thu Aug 08 10:51:40 2013 +0200
     1.3 @@ -455,6 +455,11 @@
     1.4              if (e.getAnnotation(ComputedProperty.class) == null) {
     1.5                  continue;
     1.6              }
     1.7 +            if (!e.getModifiers().contains(Modifier.STATIC)) {
     1.8 +                error("Method " + e.getSimpleName() + " has to be static when annotated by @ComputedProperty", e);
     1.9 +                ok = false;
    1.10 +                continue;
    1.11 +            }
    1.12              ExecutableElement ee = (ExecutableElement)e;
    1.13              final TypeMirror rt = ee.getReturnType();
    1.14              final Types tu = processingEnv.getTypeUtils();
     2.1 --- a/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Wed Aug 07 15:34:10 2013 +0200
     2.2 +++ b/json/src/test/java/net/java/html/json/ModelProcessorTest.java	Thu Aug 08 10:51:40 2013 +0200
     2.3 @@ -60,6 +60,38 @@
     2.4          }
     2.5      }
     2.6      
     2.7 +    @Test public void warnOnNonStatic() throws IOException {
     2.8 +        String html = "<html><body>"
     2.9 +            + "</body></html>";
    2.10 +        String code = "package x.y.z;\n"
    2.11 +            + "import net.java.html.json.Model;\n"
    2.12 +            + "import net.java.html.json.Property;\n"
    2.13 +            + "import net.java.html.json.ComputedProperty;\n"
    2.14 +            + "@Model(className=\"XModel\", properties={\n"
    2.15 +            + "  @Property(name=\"prop\", type=int.class)\n"
    2.16 +            + "})\n"
    2.17 +            + "class X {\n"
    2.18 +            + "    @ComputedProperty int y(int prop) {\n"
    2.19 +            + "        return prop;\n"
    2.20 +            + "    }\n"
    2.21 +            + "}\n";
    2.22 +        
    2.23 +        Compile c = Compile.create(html, code);
    2.24 +        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
    2.25 +        boolean ok = false;
    2.26 +        StringBuilder msgs = new StringBuilder();
    2.27 +        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
    2.28 +            String msg = e.getMessage(Locale.ENGLISH);
    2.29 +            if (msg.contains("y has to be static")) {
    2.30 +                ok = true;
    2.31 +            }
    2.32 +            msgs.append("\n").append(msg);
    2.33 +        }
    2.34 +        if (!ok) {
    2.35 +            fail("Should contain warning about non-static method:" + msgs);
    2.36 +        }
    2.37 +    }
    2.38 +    
    2.39      @Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
    2.40          String html = "<html><body>"
    2.41              + "</body></html>";