jdk/src/share/classes/java/beans/Encoder.java
changeset 4846 48f8e6d40dde
parent 3240 2f79c1748c93
child 5506 202f599c92aa
equal deleted inserted replaced
4845:64433d7632f2 4846:48f8e6d40dde
     1 /*
     1 /*
     2  * Copyright 2000-2009 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2000-2010 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
   110         }
   110         }
   111     }
   111     }
   112 
   112 
   113     /**
   113     /**
   114      * Returns the persistence delegate for the given type.
   114      * Returns the persistence delegate for the given type.
   115      * The persistence delegate is calculated
   115      * The persistence delegate is calculated by applying
   116      * by applying the following of rules in order:
   116      * the following rules in order:
   117      * <ul>
   117      * <ol>
   118      * <li>
   118      * <li>
   119      * If the type is an array, an internal persistence
   119      * If a persistence delegate is associated with the given type
   120      * delegate is returned which will instantiate an
   120      * by using the {@link #setPersistenceDelegate} method
   121      * array of the appropriate type and length, initializing
   121      * it is returned.
   122      * each of its elements as if they are properties.
   122      * <li>
   123      * <li>
   123      * A persistence delegate is then looked up by the name
   124      * If the type is a proxy, an internal persistence
   124      * composed of the the fully qualified name of the given type
   125      * delegate is returned which will instantiate a
   125      * and the "PersistenceDelegate" postfix.
   126      * new proxy instance using the static
   126      * For example, a persistence delegate for the {@code Bean} class
   127      * "newProxyInstance" method defined in the
   127      * should be named {@code BeanPersistenceDelegate}
   128      * Proxy class.
   128      * and located in the same package.
   129      * <li>
   129      * <pre>
   130      * If the BeanInfo for this type has a <code>BeanDescriptor</code>
   130      * public class Bean { ... }
   131      * which defined a "persistenceDelegate" property, this
   131      * public class BeanPersistenceDelegate { ... }</pre>
   132      * value is returned.
   132      * The instance of the {@code BeanPersistenceDelegate} class
   133      * <li>
   133      * is returned for the {@code Bean} class.
   134      * In all other cases the default persistence delegate
   134      * <li>
   135      * is returned. The default persistence delegate assumes
   135      * If the type is {@code null},
   136      * the type is a <em>JavaBean</em>, implying that it has a default constructor
   136      * a shared internal persistence delegate is returned
   137      * and that its state may be characterized by the matching pairs
   137      * that encodes {@code null} value.
   138      * of "setter" and "getter" methods returned by the Introspector.
   138      * <li>
       
   139      * If the type is a {@code enum} declaration,
       
   140      * a shared internal persistence delegate is returned
       
   141      * that encodes constants of this enumeration
       
   142      * by their names.
       
   143      * <li>
       
   144      * If the type is a primitive type or the corresponding wrapper,
       
   145      * a shared internal persistence delegate is returned
       
   146      * that encodes values of the given type.
       
   147      * <li>
       
   148      * If the type is an array,
       
   149      * a shared internal persistence delegate is returned
       
   150      * that encodes an array of the appropriate type and length,
       
   151      * and each of its elements as if they are properties.
       
   152      * <li>
       
   153      * If the type is a proxy,
       
   154      * a shared internal persistence delegate is returned
       
   155      * that encodes a proxy instance by using
       
   156      * the {@link java.lang.reflect.Proxy#newProxyInstance} method.
       
   157      * <li>
       
   158      * If the {@link BeanInfo} for this type has a {@link BeanDescriptor}
       
   159      * which defined a "persistenceDelegate" attribute,
       
   160      * the value of this named attribute is returned.
       
   161      * <li>
       
   162      * In all other cases the default persistence delegate is returned.
       
   163      * The default persistence delegate assumes the type is a <em>JavaBean</em>,
       
   164      * implying that it has a default constructor and that its state
       
   165      * may be characterized by the matching pairs of "setter" and "getter"
       
   166      * methods returned by the {@link Introspector} class.
   139      * The default constructor is the constructor with the greatest number
   167      * The default constructor is the constructor with the greatest number
   140      * of parameters that has the {@link ConstructorProperties} annotation.
   168      * of parameters that has the {@link ConstructorProperties} annotation.
   141      * If none of the constructors have the {@code ConstructorProperties} annotation,
   169      * If none of the constructors has the {@code ConstructorProperties} annotation,
   142      * then the nullary constructor (constructor with no parameters) will be used.
   170      * then the nullary constructor (constructor with no parameters) will be used.
   143      * For example, in the following the nullary constructor
   171      * For example, in the following code fragment, the nullary constructor
   144      * for {@code Foo} will be used, while the two parameter constructor
   172      * for the {@code Foo} class will be used,
   145      * for {@code Bar} will be used.
   173      * while the two-parameter constructor
   146      * <code>
   174      * for the {@code Bar} class will be used.
   147      *   public class Foo {
   175      * <pre>
       
   176      * public class Foo {
   148      *     public Foo() { ... }
   177      *     public Foo() { ... }
   149      *     public Foo(int x) { ... }
   178      *     public Foo(int x) { ... }
   150      *   }
   179      * }
   151      *   public class Bar {
   180      * public class Bar {
   152      *     public Bar() { ... }
   181      *     public Bar() { ... }
   153      *     &#64;ConstructorProperties({"x"})
   182      *     &#64;ConstructorProperties({"x"})
   154      *     public Bar(int x) { ... }
   183      *     public Bar(int x) { ... }
   155      *     &#64;ConstructorProperties({"x", "y"})
   184      *     &#64;ConstructorProperties({"x", "y"})
   156      *     public Bar(int x, int y) { ... }
   185      *     public Bar(int x, int y) { ... }
   157      *   }
   186      * }</pre>
   158      * </code>
   187      * </ol>
   159      * </ul>
   188      *
   160      *
   189      * @param type  the class of the objects
   161      * @param  type The type of the object.
   190      * @return the persistence delegate for the given type
   162      * @return The persistence delegate for this type of object.
       
   163      *
   191      *
   164      * @see #setPersistenceDelegate
   192      * @see #setPersistenceDelegate
   165      * @see java.beans.Introspector#getBeanInfo
   193      * @see java.beans.Introspector#getBeanInfo
   166      * @see java.beans.BeanInfo#getBeanDescriptor
   194      * @see java.beans.BeanInfo#getBeanDescriptor
   167      */
   195      */
   174         }
   202         }
   175         return MetaData.getPersistenceDelegate(type);
   203         return MetaData.getPersistenceDelegate(type);
   176     }
   204     }
   177 
   205 
   178     /**
   206     /**
   179      * Sets the persistence delegate associated with this <code>type</code> to
   207      * Associates the specified persistence delegate with the given type.
   180      * <code>persistenceDelegate</code>.
   208      *
   181      *
   209      * @param type  the class of objects that the specified persistence delegate applies to
   182      * @param  type The class of objects that <code>persistenceDelegate</code> applies to.
   210      * @param delegate  the persistence delegate for instances of the given type
   183      * @param  persistenceDelegate The persistence delegate for instances of <code>type</code>.
       
   184      *
   211      *
   185      * @see #getPersistenceDelegate
   212      * @see #getPersistenceDelegate
   186      * @see java.beans.Introspector#getBeanInfo
   213      * @see java.beans.Introspector#getBeanInfo
   187      * @see java.beans.BeanInfo#getBeanDescriptor
   214      * @see java.beans.BeanInfo#getBeanDescriptor
   188      */
   215      */
   189     public void setPersistenceDelegate(Class<?> type,
   216     public void setPersistenceDelegate(Class<?> type, PersistenceDelegate delegate) {
   190                                        PersistenceDelegate persistenceDelegate)
       
   191     {
       
   192         synchronized (this.finder) {
   217         synchronized (this.finder) {
   193             this.finder.register(type, persistenceDelegate);
   218             this.finder.register(type, delegate);
   194         }
   219         }
   195     }
   220     }
   196 
   221 
   197     /**
   222     /**
   198      * Removes the entry for this instance, returning the old entry.
   223      * Removes the entry for this instance, returning the old entry.