src/share/classes/java/beans/Beans.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  45 import java.net.URL;
  46 
  47 import java.security.AccessController;
  48 import java.security.PrivilegedAction;
  49 
  50 import java.util.Enumeration;
  51 import java.util.Hashtable;
  52 import java.util.Iterator;
  53 import java.util.Vector;
  54 
  55 /**
  56  * This class provides some general purpose beans control methods.
  57  */
  58 
  59 public class Beans {
  60 
  61     /**
  62      * <p>
  63      * Instantiate a JavaBean.
  64      * </p>
  65      *
  66      * @param     cls         the class-loader from which we should create
  67      *                        the bean.  If this is null, then the system
  68      *                        class-loader is used.
  69      * @param     beanName    the name of the bean within the class-loader.
  70      *                        For example "sun.beanbox.foobah"
  71      *
  72      * @exception ClassNotFoundException if the class of a serialized
  73      *              object could not be found.
  74      * @exception IOException if an I/O error occurs.
  75      */
  76 
  77     public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException {
  78         return Beans.instantiate(cls, beanName, null, null);
  79     }
  80 
  81     /**
  82      * <p>
  83      * Instantiate a JavaBean.
  84      * </p>

  85      *
  86      * @param     cls         the class-loader from which we should create
  87      *                        the bean.  If this is null, then the system
  88      *                        class-loader is used.
  89      * @param     beanName    the name of the bean within the class-loader.
  90      *                        For example "sun.beanbox.foobah"
  91      * @param     beanContext The BeanContext in which to nest the new bean
  92      *
  93      * @exception ClassNotFoundException if the class of a serialized
  94      *              object could not be found.
  95      * @exception IOException if an I/O error occurs.
  96      */
  97 
  98     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException {
  99         return Beans.instantiate(cls, beanName, beanContext, null);
 100     }
 101 
 102     /**
 103      * Instantiate a bean.
 104      * <p>


 120      * instance of that class.
 121      * <p>
 122      * If the bean is a subtype of java.applet.Applet, then it is given
 123      * some special initialization.  First, it is supplied with a default
 124      * AppletStub and AppletContext.  Second, if it was instantiated from
 125      * a classname the applet's "init" method is called.  (If the bean was
 126      * deserialized this step is skipped.)
 127      * <p>
 128      * Note that for beans which are applets, it is the caller's responsiblity
 129      * to call "start" on the applet.  For correct behaviour, this should be done
 130      * after the applet has been added into a visible AWT container.
 131      * <p>
 132      * Note that applets created via beans.instantiate run in a slightly
 133      * different environment than applets running inside browsers.  In
 134      * particular, bean applets have no access to "parameters", so they may
 135      * wish to provide property get/set methods to set parameter values.  We
 136      * advise bean-applet developers to test their bean-applets against both
 137      * the JDK appletviewer (for a reference browser environment) and the
 138      * BDK BeanBox (for a reference bean container).
 139      *

 140      * @param     cls         the class-loader from which we should create
 141      *                        the bean.  If this is null, then the system
 142      *                        class-loader is used.
 143      * @param     beanName    the name of the bean within the class-loader.
 144      *                        For example "sun.beanbox.foobah"
 145      * @param     beanContext The BeanContext in which to nest the new bean
 146      * @param     initializer The AppletInitializer for the new bean
 147      *
 148      * @exception ClassNotFoundException if the class of a serialized
 149      *              object could not be found.
 150      * @exception IOException if an I/O error occurs.
 151      */
 152 
 153     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
 154                         throws IOException, ClassNotFoundException {
 155 
 156         InputStream ins;
 157         ObjectInputStream oins = null;
 158         Object result = null;
 159         boolean serialized = false;


 344 
 345         return result;
 346     }
 347 
 348     @SuppressWarnings("unchecked")
 349     private static void unsafeBeanContextAdd(BeanContext beanContext, Object res) {
 350         beanContext.add(res);
 351     }
 352 
 353     /**
 354      * From a given bean, obtain an object representing a specified
 355      * type view of that source object.
 356      * <p>
 357      * The result may be the same object or a different object.  If
 358      * the requested target view isn't available then the given
 359      * bean is returned.
 360      * <p>
 361      * This method is provided in Beans 1.0 as a hook to allow the
 362      * addition of more flexible bean behaviour in the future.
 363      *


 364      * @param bean        Object from which we want to obtain a view.
 365      * @param targetType  The type of view we'd like to get.
 366      *
 367      */
 368     public static Object getInstanceOf(Object bean, Class<?> targetType) {
 369         return bean;
 370     }
 371 
 372     /**
 373      * Check if a bean can be viewed as a given target type.
 374      * The result will be true if the Beans.getInstanceof method
 375      * can be used on the given bean to obtain an object that
 376      * represents the specified targetType type view.
 377      *
 378      * @param bean  Bean from which we want to obtain a view.
 379      * @param targetType  The type of view we'd like to get.
 380      * @return "true" if the given bean supports the given targetType.
 381      *
 382      */
 383     public static boolean isInstanceOf(Object bean, Class<?> targetType) {
 384         return Introspector.isSubclass(bean.getClass(), targetType);
 385     }
 386 
 387 
 388     /**
 389      * Test if we are in design-mode.
 390      *
 391      * @return  True if we are running in an application construction
 392      *          environment.
 393      *
 394      * @see DesignMode
 395      */
 396     public static boolean isDesignTime() {
 397         return ThreadGroupContext.getContext().isDesignTime();
 398     }
 399 
 400     /**
 401      * Determines whether beans can assume a GUI is available.
 402      *
 403      * @return  True if we are running in an environment where beans
 404      *     can assume that an interactive GUI is available, so they
 405      *     can pop up dialog boxes, etc.  This will normally return
 406      *     true in a windowing environment, and will normally return


   1 /*
   2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  45 import java.net.URL;
  46 
  47 import java.security.AccessController;
  48 import java.security.PrivilegedAction;
  49 
  50 import java.util.Enumeration;
  51 import java.util.Hashtable;
  52 import java.util.Iterator;
  53 import java.util.Vector;
  54 
  55 /**
  56  * This class provides some general purpose beans control methods.
  57  */
  58 
  59 public class Beans {
  60 
  61     /**
  62      * <p>
  63      * Instantiate a JavaBean.
  64      * </p>
  65      * @return a JavaBean
  66      * @param     cls         the class-loader from which we should create
  67      *                        the bean.  If this is null, then the system
  68      *                        class-loader is used.
  69      * @param     beanName    the name of the bean within the class-loader.
  70      *                        For example "sun.beanbox.foobah"
  71      *
  72      * @exception ClassNotFoundException if the class of a serialized
  73      *              object could not be found.
  74      * @exception IOException if an I/O error occurs.
  75      */
  76 
  77     public static Object instantiate(ClassLoader cls, String beanName) throws IOException, ClassNotFoundException {
  78         return Beans.instantiate(cls, beanName, null, null);
  79     }
  80 
  81     /**
  82      * <p>
  83      * Instantiate a JavaBean.
  84      * </p>
  85      * @return a JavaBean
  86      *
  87      * @param     cls         the class-loader from which we should create
  88      *                        the bean.  If this is null, then the system
  89      *                        class-loader is used.
  90      * @param     beanName    the name of the bean within the class-loader.
  91      *                        For example "sun.beanbox.foobah"
  92      * @param     beanContext The BeanContext in which to nest the new bean
  93      *
  94      * @exception ClassNotFoundException if the class of a serialized
  95      *              object could not be found.
  96      * @exception IOException if an I/O error occurs.
  97      */
  98 
  99     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext) throws IOException, ClassNotFoundException {
 100         return Beans.instantiate(cls, beanName, beanContext, null);
 101     }
 102 
 103     /**
 104      * Instantiate a bean.
 105      * <p>


 121      * instance of that class.
 122      * <p>
 123      * If the bean is a subtype of java.applet.Applet, then it is given
 124      * some special initialization.  First, it is supplied with a default
 125      * AppletStub and AppletContext.  Second, if it was instantiated from
 126      * a classname the applet's "init" method is called.  (If the bean was
 127      * deserialized this step is skipped.)
 128      * <p>
 129      * Note that for beans which are applets, it is the caller's responsiblity
 130      * to call "start" on the applet.  For correct behaviour, this should be done
 131      * after the applet has been added into a visible AWT container.
 132      * <p>
 133      * Note that applets created via beans.instantiate run in a slightly
 134      * different environment than applets running inside browsers.  In
 135      * particular, bean applets have no access to "parameters", so they may
 136      * wish to provide property get/set methods to set parameter values.  We
 137      * advise bean-applet developers to test their bean-applets against both
 138      * the JDK appletviewer (for a reference browser environment) and the
 139      * BDK BeanBox (for a reference bean container).
 140      *
 141      * @return a JavaBean
 142      * @param     cls         the class-loader from which we should create
 143      *                        the bean.  If this is null, then the system
 144      *                        class-loader is used.
 145      * @param     beanName    the name of the bean within the class-loader.
 146      *                        For example "sun.beanbox.foobah"
 147      * @param     beanContext The BeanContext in which to nest the new bean
 148      * @param     initializer The AppletInitializer for the new bean
 149      *
 150      * @exception ClassNotFoundException if the class of a serialized
 151      *              object could not be found.
 152      * @exception IOException if an I/O error occurs.
 153      */
 154 
 155     public static Object instantiate(ClassLoader cls, String beanName, BeanContext beanContext, AppletInitializer initializer)
 156                         throws IOException, ClassNotFoundException {
 157 
 158         InputStream ins;
 159         ObjectInputStream oins = null;
 160         Object result = null;
 161         boolean serialized = false;


 346 
 347         return result;
 348     }
 349 
 350     @SuppressWarnings("unchecked")
 351     private static void unsafeBeanContextAdd(BeanContext beanContext, Object res) {
 352         beanContext.add(res);
 353     }
 354 
 355     /**
 356      * From a given bean, obtain an object representing a specified
 357      * type view of that source object.
 358      * <p>
 359      * The result may be the same object or a different object.  If
 360      * the requested target view isn't available then the given
 361      * bean is returned.
 362      * <p>
 363      * This method is provided in Beans 1.0 as a hook to allow the
 364      * addition of more flexible bean behaviour in the future.
 365      *
 366      * @return an object representing a specified type view of the
 367      * source object
 368      * @param bean        Object from which we want to obtain a view.
 369      * @param targetType  The type of view we'd like to get.
 370      *
 371      */
 372     public static Object getInstanceOf(Object bean, Class<?> targetType) {
 373         return bean;
 374     }
 375 
 376     /**
 377      * Check if a bean can be viewed as a given target type.
 378      * The result will be true if the Beans.getInstanceof method
 379      * can be used on the given bean to obtain an object that
 380      * represents the specified targetType type view.
 381      *
 382      * @param bean  Bean from which we want to obtain a view.
 383      * @param targetType  The type of view we'd like to get.
 384      * @return "true" if the given bean supports the given targetType.
 385      *
 386      */
 387     public static boolean isInstanceOf(Object bean, Class<?> targetType) {
 388         return Introspector.isSubclass(bean.getClass(), targetType);
 389     }

 390 
 391     /**
 392      * Test if we are in design-mode.
 393      *
 394      * @return  True if we are running in an application construction
 395      *          environment.
 396      *
 397      * @see DesignMode
 398      */
 399     public static boolean isDesignTime() {
 400         return ThreadGroupContext.getContext().isDesignTime();
 401     }
 402 
 403     /**
 404      * Determines whether beans can assume a GUI is available.
 405      *
 406      * @return  True if we are running in an environment where beans
 407      *     can assume that an interactive GUI is available, so they
 408      *     can pop up dialog boxes, etc.  This will normally return
 409      *     true in a windowing environment, and will normally return