Repository: incubator-juneau Updated Branches: refs/heads/master 7ca611354 -> 9b04bb9c6
Implement proxy interface toString() method. Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/9b04bb9c Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/9b04bb9c Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/9b04bb9c Branch: refs/heads/master Commit: 9b04bb9c69109e4401fa702531fd8eb975fb8402 Parents: 7ca6113 Author: JamesBognar <[email protected]> Authored: Mon May 15 20:24:38 2017 -0400 Committer: JamesBognar <[email protected]> Committed: Mon May 15 20:24:38 2017 -0400 ---------------------------------------------------------------------- .../juneau/BeanProxyInvocationHandler.java | 4 +- juneau-core/src/main/javadoc/overview.html | 90 +++++++++++++++++--- 2 files changed, 81 insertions(+), 13 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9b04bb9c/juneau-core/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java b/juneau-core/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java index 4965e1e..57bd0be 100644 --- a/juneau-core/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java +++ b/juneau-core/src/main/java/org/apache/juneau/BeanProxyInvocationHandler.java @@ -15,6 +15,8 @@ package org.apache.juneau; import java.lang.reflect.*; import java.util.*; +import org.apache.juneau.json.*; + /** * Provides an {@link InvocationHandler} for creating beans from bean interfaces. * <p> @@ -64,7 +66,7 @@ public class BeanProxyInvocationHandler<T> implements InvocationHandler { return Integer.valueOf(this.beanProps.hashCode()); if (method.getName().equals("toString") && (paramTypes.length == 0)) - return this.beanProps.toString(); + return JsonSerializer.DEFAULT_LAX.toString(this.beanProps); String prop = this.meta.getterProps.get(method); if (prop != null) http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9b04bb9c/juneau-core/src/main/javadoc/overview.html ---------------------------------------------------------------------- diff --git a/juneau-core/src/main/javadoc/overview.html b/juneau-core/src/main/javadoc/overview.html index 07d0a01..f9f7088 100644 --- a/juneau-core/src/main/javadoc/overview.html +++ b/juneau-core/src/main/javadoc/overview.html @@ -84,6 +84,7 @@ <ol> <li><p><a class='doclink' href='#Core.BeanSubTypes'>Bean Subtypes</a></p> </ol> + <li><p><a class='doclink' href='#Core.VirtualBeans'>Virtual Beans</a></p> <li><p><a class='doclink' href='#Core.PojoCategories'>POJO Categories</a></p> <li><p><a class='doclink' href='#Core.SimpleVarLanguage'>Simple Variable Language</a></p> <li><p><a class='doclink' href='#Core.ConfigFile'>Configuration Files</a></p> @@ -1109,8 +1110,61 @@ </div> <!-- ======================================================================================================== --> + <a id="Core.VirtualBeans"></a> + <h3 class='topic' onclick='toggle(this)'>2.8 - Virtual Beans</h3> + <div class='topic'> + <p> + The {@link org.apache.juneau.BeanContext#BEAN_useInterfaceProxies} setting (enabled by default) allows + the Juneau parsers to parse content into virtual beans (bean interfaces without implementation classes). + </p> + <p> + For example, the following code creates an instance of the specified unimplemented interface: + </p> + <p class='bcode'> + <jc>// Our unimplemented interface</jc> + <jk>public interface</jk> Address { + + String getStreet(); + <jk>void</jk> setStreet(String x); + + String getCity(); + <jk>void</jk> setCity(String x); + + StateEnum getState(); + <jk>void</jk> setState(StateEnum x); + + <jk>int</jk> getZip(); + <jk>void</jk> setZip(<jk>int</jk> zip); + } + + <jc>// Our code</jc> + Address address = JsonParser.<jsf>DEFAULT</jsf>.parse( + <js>"{street:'123 Main St', city:'Anywhere', state:'PR', zip:12345}"</js>, + Address.<jk>class</jk> + ); + + <jk>int</jk> zip = address.getZip(); + address.setState(StateEnum.<jsf>NY</jsf>); + </p> + <p> + Getter and setter values can be any parsable values, even other virtual beans. + </p> + <p> + Under-the-covers, a virtual bean is simply a proxy interface on top of an existing <code>BeanMap</code> + instance. From a programmatic point-of-view, they're indistinguishable from real beans, and can be + manipulated and serialized like any other bean. + </p> + <p> + Virtual beans can also be created programmatically using the <code>BeanContext</code> class: + </p> + <p class='bcode'> + Address address = BeanContext.<jsf>DEFAULT</jsf>.createSession().newBean(Address.<jk>class</jk>); + </p> + </div> + + <!-- ======================================================================================================== --> <a id="Core.PojoCategories"></a> - <h3 class='topic' onclick='toggle(this)'>2.8 - POJO Categories</h3> + <h3 class='topic' onclick='toggle(this)'>2.9 - POJO Categories</h3> <div class='topic'> <p> The following chart shows POJOs categorized into groups and whether they can be serialized or parsed: @@ -1143,7 +1197,7 @@ <td> <b>With standard keys/values</b><br> Map keys are group [1, 4a, 5a] objects.<br> - Map, Collection, and array values are group [1, 2, 3a, 4a, 5a] objects. + Map, Collection, and array values are group [1, 2, 3ac, 4a, 5a] objects. </td> <td> <ul class='normal'> @@ -1184,7 +1238,7 @@ <td> <b>With standard properties</b><br> These are beans that have no-arg constructors and one or more properties defined by public getter and setter methods or public fields.<br> - Property values are group [1, 2, 3a, 4a, 5a] objects. + Property values are group [1, 2, 3ac, 4a, 5a] objects. </td> <td> </td> <td style='background-color:lightgreen;text-align:center'><b>yes</b></td> @@ -1203,6 +1257,17 @@ <td style='background-color:lightgreen;text-align:center'><b>yes</b></td> <td style='background-color:salmon;text-align:center'><b>no</b></td> </tr> + <tr class='light bb'> + <td style='text-align:center'>3c</td> + <td> + <b>Virtual beans</b><br> + These are unimplemented bean interfaces with properties of type [1, 2, 3ac, 4a, 5a] objects.<br> + Parsers will automatically create interface proxies on top of BeanMap instances. + </td> + <td> </td> + <td style='background-color:lightgreen;text-align:center'><b>yes</b></td> + <td style='background-color:lightgreen;text-align:center'><b>yes</b></td> + </tr> <tr class='dark bb' style='background-color:lightyellow'> <td style='text-align:center'>4</td> <td> @@ -1219,7 +1284,7 @@ <tr class='light bb'> <td style='text-align:center'>4a</td> <td> - <b>2-way swapped to group [1, 2a, 3a] objects</b><br> + <b>2-way swapped to group [1, 2a, 3ac] objects</b><br> For example, a swap that converts a {@code Date} to a {@code String}. </td> <td> @@ -1260,7 +1325,7 @@ <td> Classes with a method that converts it to a serializable form: <ul> - <li><code><jk>public</jk> X swap(BeanSession);</code> where <code>X</code> is in groups [1, 2a, 3a]. + <li><code><jk>public</jk> X swap(BeanSession);</code> where <code>X</code> is in groups [1, 2a, 3ac]. <li><code><jk>public</jk> String toString();</code> where the string is any meaningful data. </ul> And a method that converts it back into the original object: @@ -1271,8 +1336,8 @@ <li><code><jk>public static</jk> T parseString(String);</code> <li><code><jk>public static</jk> T forName(String);</code> <li><code><jk>public static</jk> T forString(String);</code> - <li><code><jk>public</jk> T(X);</code> where <code>X</code> is in groups [1, 2a, 3a]. - <li><code><jk>public static</jk> T unswap(BeanSession,X);</code> where <code>X</code> is in groups [1, 2a, 3a]. + <li><code><jk>public</jk> T(X);</code> where <code>X</code> is in groups [1, 2a, 3ac]. + <li><code><jk>public static</jk> T unswap(BeanSession,X);</code> where <code>X</code> is in groups [1, 2a, 3ac]. </ul> </td> <td> @@ -1325,7 +1390,7 @@ <!-- ======================================================================================================== --> <a id="Core.SVL"></a> - <h4 class='topic' onclick='toggle(this)'>2.9 - Simple Variable Language</h4> + <h4 class='topic' onclick='toggle(this)'>2.10 - Simple Variable Language</h4> <div class='topic'> <p> The <a class='doclink' href='org/apache/juneau/svl/package-summary.html#TOC'>org.apache.juneau.svl</a> package defines an API for a language called "Simple Variable Language". @@ -1361,7 +1426,7 @@ <!-- ======================================================================================================== --> <a id="Core.ConfigFile"></a> - <h3 class='topic' onclick='toggle(this)'>2.10 - Configuration Files</h3> + <h3 class='topic' onclick='toggle(this)'>2.11 - Configuration Files</h3> <div class='topic'> <p> The <a class='doclink' href='org/apache/juneau/ini/package-summary.html#TOC'>org.apache.juneau.ini</a> package contains a powerful API for creating and using INI-style config files. @@ -1597,7 +1662,7 @@ <!-- ======================================================================================================== --> <a id="Core.SupportedLanguages"></a> - <h3 class='topic' onclick='toggle(this)'>2.11 - Supported Languages</h3> + <h3 class='topic' onclick='toggle(this)'>2.12 - Supported Languages</h3> <div class='topic'> <p> Extensive javadocs exist for individual language support. @@ -1621,7 +1686,7 @@ <!-- ======================================================================================================== --> <a id="Core.JacksonComparison"></a> - <h3 class='topic' onclick='toggle(this)'>2.12 - Comparison with Jackson</h3> + <h3 class='topic' onclick='toggle(this)'>2.13 - Comparison with Jackson</h3> <div class='topic'> <p> Juneau was developed independently from Jackson, but shares many of the same features and capabilities. @@ -6039,7 +6104,8 @@ <ul class='spaced-list'> <li>New package: {@link org.apache.juneau.http}. <li>Support for dynamic beans. See {@link org.apache.juneau.annotation.BeanProperty#name() @BeanProperty.name()}. - <li>New doc: <a class='doclink' href='#Core.JacksonComparison'>2.12 - Comparison with Jackson</a> + <li>New doc: <a class='doclink' href='#Core.VirtualBeans'>2.8 - Virtual Beans</a> + <li>New doc: <a class='doclink' href='#Core.JacksonComparison'>2.13 - Comparison with Jackson</a> <li>All parsers now allow for numeric types with <js>'K'</js>/<js>'M'</js>/<js>'G'</js> suffixes to represent kilobytes, megabytes, and gigabytes. <p class='bcode'>
