The model can contain derived properties model
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 20 Jan 2013 14:29:10 +0100
branchmodel
changeset 49114268cd404a4
parent 490 e089ef6785c0
child 492 854286e49061
The model can contain derived properties
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java
javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java
     1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Sun Jan 20 13:05:02 2013 +0100
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Sun Jan 20 14:29:10 2013 +0100
     1.3 @@ -22,6 +22,7 @@
     1.4  import java.io.OutputStreamWriter;
     1.5  import java.io.Writer;
     1.6  import java.util.ArrayList;
     1.7 +import java.util.Collection;
     1.8  import java.util.Collections;
     1.9  import java.util.List;
    1.10  import java.util.Locale;
    1.11 @@ -34,15 +35,18 @@
    1.12  import javax.annotation.processing.SupportedAnnotationTypes;
    1.13  import javax.lang.model.element.AnnotationMirror;
    1.14  import javax.lang.model.element.Element;
    1.15 +import javax.lang.model.element.ElementKind;
    1.16  import javax.lang.model.element.ExecutableElement;
    1.17  import javax.lang.model.element.Modifier;
    1.18  import javax.lang.model.element.PackageElement;
    1.19  import javax.lang.model.element.TypeElement;
    1.20 +import javax.lang.model.element.VariableElement;
    1.21  import javax.lang.model.type.MirroredTypeException;
    1.22  import javax.lang.model.type.TypeMirror;
    1.23  import javax.tools.Diagnostic;
    1.24  import javax.tools.FileObject;
    1.25  import javax.tools.StandardLocation;
    1.26 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
    1.27  import org.apidesign.bck2brwsr.htmlpage.api.On;
    1.28  import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.29  import org.apidesign.bck2brwsr.htmlpage.api.Property;
    1.30 @@ -101,6 +105,7 @@
    1.31                      }
    1.32                      w.append("  }\n");
    1.33                      generateProperties(w, p.properties());
    1.34 +                    generateComputedProperties(w, e.getEnclosedElements());
    1.35                      w.append("}\n");
    1.36                  } finally {
    1.37                      w.close();
    1.38 @@ -234,7 +239,7 @@
    1.39  
    1.40      private static void generateProperties(Writer w, Property[] properties) throws IOException {
    1.41          for (Property p : properties) {
    1.42 -            String[] gs = toGetSet(p);
    1.43 +            String[] gs = toGetSet(p.name(), null);
    1.44  
    1.45              final String tn = typeName(p);
    1.46              w.write("private static " + tn + " prop_" + p.name() + ";\n");
    1.47 @@ -247,8 +252,34 @@
    1.48          }
    1.49      }
    1.50  
    1.51 -    private static String[] toGetSet(Property p) {
    1.52 -        String n = Character.toUpperCase(p.name().charAt(0)) + p.name().substring(1);
    1.53 +    private void generateComputedProperties(Writer w, Collection<? extends Element> arr) throws IOException {
    1.54 +        for (Element e : arr) {
    1.55 +            if (e.getKind() != ElementKind.METHOD) {
    1.56 +                continue;
    1.57 +            }
    1.58 +            if (e.getAnnotation(ComputedProperty.class) == null) {
    1.59 +                continue;
    1.60 +            }
    1.61 +            ExecutableElement ee = (ExecutableElement)e;
    1.62 +            String[] gs = toGetSet(ee.getSimpleName().toString(), null);
    1.63 +
    1.64 +            final String tn = ee.getReturnType().toString();
    1.65 +            w.write("public static " + tn + " " + gs[0] + "() {\n");
    1.66 +            w.write("  return " + e.getEnclosingElement().getSimpleName() + '.' + e.getSimpleName() + "(");
    1.67 +            String sep = "";
    1.68 +            for (VariableElement pe : ee.getParameters()) {
    1.69 +                String[] call = toGetSet(pe.getSimpleName().toString(), null);
    1.70 +                w.write(sep);
    1.71 +                w.write(call[0] + "()");
    1.72 +                sep = ", ";
    1.73 +            }
    1.74 +            w.write(");\n");
    1.75 +            w.write("}\n");
    1.76 +        }
    1.77 +    }
    1.78 +
    1.79 +    private static String[] toGetSet(String name, Object type) {
    1.80 +        String n = Character.toUpperCase(name.charAt(0)) + name.substring(1);
    1.81  //        if (p.type() == boolean.class) {
    1.82  //            return new String[] { "is" + n, "set" + n };
    1.83  //        } else {
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java	Sun Jan 20 14:29:10 2013 +0100
     2.3 @@ -0,0 +1,38 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.bck2brwsr.htmlpage.api;
    2.22 +
    2.23 +import java.lang.annotation.ElementType;
    2.24 +import java.lang.annotation.Retention;
    2.25 +import java.lang.annotation.RetentionPolicy;
    2.26 +import java.lang.annotation.Target;
    2.27 +
    2.28 +/** Can be used in classes annotated with {@link Page} annotation to
    2.29 + * define a derived property. Value of derived property is based on values
    2.30 + * of {@link Property} as enumerated by {@link Page#properties()}.
    2.31 + * <p>
    2.32 + * The name of the derived property is the name of the method. The arguments
    2.33 + * of the method define the property names (from {@link Page#properties()} list)
    2.34 + * the value of property depends on. 
    2.35 + *
    2.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    2.37 + */
    2.38 +@Retention(RetentionPolicy.SOURCE)
    2.39 +@Target(ElementType.METHOD)
    2.40 +public @interface ComputedProperty {
    2.41 +}
     3.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java	Sun Jan 20 13:05:02 2013 +0100
     3.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ModelTest.java	Sun Jan 20 14:29:10 2013 +0100
     3.3 @@ -17,6 +17,7 @@
     3.4   */
     3.5  package org.apidesign.bck2brwsr.htmlpage;
     3.6  
     3.7 +import org.apidesign.bck2brwsr.htmlpage.api.ComputedProperty;
     3.8  import org.apidesign.bck2brwsr.htmlpage.api.Page;
     3.9  import org.apidesign.bck2brwsr.htmlpage.api.Property;
    3.10  import static org.testng.Assert.*;
    3.11 @@ -36,4 +37,14 @@
    3.12          Model.setValue(10);
    3.13          assertEquals(10, Model.getValue(), "Value changed");
    3.14      }
    3.15 +    
    3.16 +    @Test public void computedMethod() {
    3.17 +        Model.setValue(4);
    3.18 +        assertEquals(16, Model.getPowerValue());
    3.19 +    }
    3.20 +    
    3.21 +    @ComputedProperty
    3.22 +    static int powerValue(int value) {
    3.23 +        return value * value;
    3.24 +    }
    3.25  }