Introducing KOProperty to allow us to call non-public methods and to do conversion of primitive types which is broken in WebView fx
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 13 Apr 2013 09:14:54 +0200
branchfx
changeset 975094cd25a16d9
parent 974 cab175cc3358
child 976 6629bfa8f973
Introducing KOProperty to allow us to call non-public methods and to do conversion of primitive types which is broken in WebView
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOProperty.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java
javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOProperty.java	Sat Apr 13 09:14:54 2013 +0200
     1.3 @@ -0,0 +1,51 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +
    1.22 +package org.apidesign.bck2brwsr.htmlpage;
    1.23 +
    1.24 +import java.lang.reflect.Method;
    1.25 +
    1.26 +/**
    1.27 + *
    1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.29 + */
    1.30 +public final class KOProperty {
    1.31 +    private final Object obj;
    1.32 +    private final Method getter;
    1.33 +    private final Method setter;
    1.34 +    
    1.35 +    KOProperty(Object obj, String getter, String setter) throws NoSuchMethodException {
    1.36 +        this.obj = obj;
    1.37 +        this.getter = obj.getClass().getDeclaredMethod(getter);
    1.38 +        this.getter.setAccessible(true);
    1.39 +        if (setter != null) {
    1.40 +            this.setter = obj.getClass().getDeclaredMethod(setter, this.getter.getReturnType());
    1.41 +            this.setter.setAccessible(true);
    1.42 +        } else {
    1.43 +            this.setter = null;
    1.44 +        }
    1.45 +    }
    1.46 +    
    1.47 +    public Object get() throws Exception {
    1.48 +        return getter.invoke(obj);
    1.49 +    }
    1.50 +    
    1.51 +    public void set(Object value) throws Exception {
    1.52 +        setter.invoke(obj, value);
    1.53 +    }
    1.54 +}
     2.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Fri Apr 12 16:13:50 2013 +0200
     2.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/Knockout.java	Sat Apr 13 09:14:54 2013 +0200
     2.3 @@ -146,7 +146,8 @@
     2.4          
     2.5          Object ko = e.executeScript("ko");
     2.6          try {
     2.7 -            bnd.call("bnd", ko, bindings, model, prop, strip(getter), strip(setter), primitive, array);
     2.8 +            KOProperty kop = new KOProperty(model, strip(getter), strip(setter));
     2.9 +            bnd.call("bnd", ko, bindings, kop, prop, "get", "set", primitive, array);
    2.10              LOG.log(Level.FINE, "binding defined for {0}: {1}", new Object[]{prop, ((JSObject)bindings).getMember(prop)});
    2.11          } catch (Throwable ex) {
    2.12              LOG.log(Level.FINE, "binding failed for {0} on {1}", new Object[]{prop, bindings});
     3.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Fri Apr 12 16:13:50 2013 +0200
     3.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Sat Apr 13 09:14:54 2013 +0200
     3.3 @@ -193,7 +193,7 @@
     3.4                  w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
     3.5                  w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
     3.6                  w.append("import org.apidesign.bck2brwsr.core.JavaScriptOnly;\n");
     3.7 -                w.append("public final class ").append(className).append(" implements Cloneable {\n");
     3.8 +                w.append("final class ").append(className).append(" implements Cloneable {\n");
     3.9                  w.append("  private boolean locked;\n");
    3.10                  w.append("  private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n");
    3.11                  w.append(body.toString());
    3.12 @@ -343,7 +343,7 @@
    3.13                  w.append("package " + pkg + ";\n");
    3.14                  w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
    3.15                  w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n");
    3.16 -                w.append("public final class ").append(className).append(" {\n");
    3.17 +                w.append("final class ").append(className).append(" {\n");
    3.18                  w.append("  private boolean locked;\n");
    3.19                  if (!initializeOnClick(className, (TypeElement) e, w, pp)) {
    3.20                      ok = false;