diff -r 3299d5335b8f json/src/main/java/org/netbeans/html/json/impl/Bindings.java --- a/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/main/java/org/netbeans/html/json/impl/Bindings.java Wed Nov 26 22:20:33 2014 +0100 @@ -60,8 +60,11 @@ this.bp = bp; } - public PropertyBinding registerProperty(String propName, int index, M model, Proto.Type access, boolean readOnly) { - return PropertyBindingAccessor.create(access, this, propName, index, model, readOnly); + public PropertyBinding registerProperty( + String propName, int index, M model, + Proto.Type access, boolean readOnly, Class type + ) { + return PropertyBindingAccessor.create(access, this, propName, index, model, readOnly, type); } public static Bindings apply(BrwsrCtx c, Object model) { diff -r 3299d5335b8f json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Wed Nov 26 22:20:33 2014 +0100 @@ -320,6 +320,7 @@ { for (int i = 0; i < propsGetSet.size(); i++) { w.append(" registerProperty(\"").append(propsGetSet.get(i).name).append("\", "); + w.append(propsGetSet.get(i).type).append(".class, "); w.append((i) + ", " + propsGetSet.get(i).readOnly + ");\n"); } } diff -r 3299d5335b8f json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java --- a/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/main/java/org/netbeans/html/json/impl/PropertyBindingAccessor.java Wed Nov 26 22:20:33 2014 +0100 @@ -65,8 +65,10 @@ } protected abstract PropertyBinding newBinding( - Proto.Type access, Bindings bindings, String name, int index, M model, boolean readOnly + Proto.Type access, Bindings bindings, String name, + int index, M model, boolean readOnly, Class type ); + protected abstract JSONCall newCall( BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, String method, Object data @@ -87,9 +89,10 @@ } static PropertyBinding create( - Proto.Type access, Bindings bindings, String name, int index, M model , boolean readOnly + Proto.Type access, Bindings bindings, String name, + int index, M model, boolean readOnly, Class type ) { - return DEFAULT.newBinding(access, bindings, name, index, model, readOnly); + return DEFAULT.newBinding(access, bindings, name, index, model, readOnly, type); } static JSONCall createCall( BrwsrCtx ctx, RcvrJSON callback, String urlBefore, String urlAfter, diff -r 3299d5335b8f json/src/main/java/org/netbeans/html/json/spi/PropertyBinding.java --- a/json/src/main/java/org/netbeans/html/json/spi/PropertyBinding.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/main/java/org/netbeans/html/json/spi/PropertyBinding.java Wed Nov 26 22:20:33 2014 +0100 @@ -91,10 +91,10 @@ @Override protected PropertyBinding newBinding( - Proto.Type access, Bindings bindings, String name, - int index, M model, boolean readOnly + Proto.Type access, Bindings bindings, + String name, int index, M model, boolean readOnly, Class type ) { - return new Impl(bindings, name, index, model, access, readOnly); + return new Impl(bindings, name, index, model, access, readOnly, type); } }; } @@ -125,6 +125,14 @@ */ public abstract boolean isReadOnly(); + /** The type of the property. This is the type one can expect + * when {@link #getValue() getValue} method is called. In case the type + * is not known, return {@link Object}. + * @return a class of the type of the property + * @since 1.1 + */ + public abstract Class type(); + private static final class Impl extends PropertyBinding { public final String name; public final boolean readOnly; @@ -132,14 +140,19 @@ private final Proto.Type access; private final Bindings bindings; private final int index; + private final Class type; - public Impl(Bindings bindings, String name, int index, M model, Proto.Type access, boolean readOnly) { + public Impl( + Bindings bindings, String name, int index, + M model, Proto.Type access, boolean readOnly, Class type + ) { this.bindings = bindings; this.name = name; this.index = index; this.model = model; this.access = access; this.readOnly = readOnly; + this.type = type; } @Override @@ -163,6 +176,11 @@ public String getPropertyName() { return name; } + + @Override + public Class type() { + return type; + } } // end of PBData } diff -r 3299d5335b8f json/src/main/java/org/netbeans/html/json/spi/Proto.java --- a/json/src/main/java/org/netbeans/html/json/spi/Proto.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/main/java/org/netbeans/html/json/spi/Proto.java Wed Nov 26 22:20:33 2014 +0100 @@ -461,7 +461,9 @@ PropertyBinding[] pb = new PropertyBinding[type.propertyNames.length]; for (int i = 0; i < pb.length; i++) { pb[i] = b.registerProperty( - type.propertyNames[i], i, obj, type, type.propertyReadOnly[i] + type.propertyNames[i], i, obj, type, + type.propertyReadOnly[i], + type.propertyTypes[i] ); } FunctionBinding[] fb = new FunctionBinding[type.functions.length]; @@ -500,6 +502,7 @@ public static abstract class Type { private final Class clazz; private final String[] propertyNames; + private final Class[] propertyTypes; private final boolean[] propertyReadOnly; private final String[] functions; @@ -522,6 +525,7 @@ } this.clazz = clazz; this.propertyNames = new String[properties]; + this.propertyTypes = new Class[properties]; this.propertyReadOnly = new boolean[properties]; this.functions = new String[functions]; JSON.register(clazz, this); @@ -535,8 +539,24 @@ * @param readOnly is the property read only? */ protected final void registerProperty(String name, int index, boolean readOnly) { + registerProperty(name, Object.class, index, readOnly); + } + + /** Registers property for the type. It is expected each index + * is initialized only once. + * + * @param name name of the property + * @param type class type of the property + * @param index index of the property + * @param readOnly is the property read only? + * @since 1.1 + */ + protected final void registerProperty( + String name, Class type, int index, boolean readOnly + ) { assert propertyNames[index] == null; propertyNames[index] = name; + propertyTypes[index] = type; propertyReadOnly[index] = readOnly; } diff -r 3299d5335b8f json/src/test/java/net/java/html/json/MapModelTest.java --- a/json/src/test/java/net/java/html/json/MapModelTest.java Thu Nov 20 14:59:04 2014 +0100 +++ b/json/src/test/java/net/java/html/json/MapModelTest.java Wed Nov 26 22:20:33 2014 +0100 @@ -85,6 +85,7 @@ One o = (One)v; assertEquals(o.changes, 1, "One change so far"); assertFalse(o.pb.isReadOnly(), "Mutable property"); + assertEquals(o.pb.type(), String.class, "Type is string"); assertEquals(o.get(), "Jarda", "Value should be in the map"); @@ -132,6 +133,7 @@ One o = (One)v; o.set("FEMALE"); + assertEquals(o.pb.type(), Sex.class, "enum type"); assertEquals(p.getSex(), Sex.FEMALE, "Changed to female"); }