1 /*
   2  * Copyright (c) 1996, 2011, 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
  23  * questions.
  24  */
  25 
  26 package java.beans;
  27 
  28 import java.lang.ref.Reference;
  29 
  30 /**
  31  * A BeanDescriptor provides global information about a "bean",
  32  * including its Java class, its displayName, etc.
  33  * <p>
  34  * This is one of the kinds of descriptor returned by a BeanInfo object,
  35  * which also returns descriptors for properties, method, and events.
  36  */
  37 
  38 public class BeanDescriptor extends FeatureDescriptor {
  39 
  40     private Reference<? extends Class<?>> beanClassRef;
  41     private Reference<? extends Class<?>> customizerClassRef;
  42 
  43     /**
  44      * Create a BeanDescriptor for a bean that doesn't have a customizer.
  45      *
  46      * @param beanClass  The Class object of the Java class that implements
  47      *          the bean.  For example sun.beans.OurButton.class.
  48      */
  49     public BeanDescriptor(Class<?> beanClass) {
  50         this(beanClass, null);
  51     }
  52 
  53     /**
  54      * Create a BeanDescriptor for a bean that has a customizer.
  55      *
  56      * @param beanClass  The Class object of the Java class that implements
  57      *          the bean.  For example sun.beans.OurButton.class.
  58      * @param customizerClass  The Class object of the Java class that implements
  59      *          the bean's Customizer.  For example sun.beans.OurButtonCustomizer.class.
  60      */
  61     public BeanDescriptor(Class<?> beanClass, Class<?> customizerClass) {
  62         this.beanClassRef = getWeakReference(beanClass);
  63         this.customizerClassRef = getWeakReference(customizerClass);
  64 
  65         String name = beanClass.getName();
  66         while (name.indexOf('.') >= 0) {
  67             name = name.substring(name.indexOf('.')+1);
  68         }
  69         setName(name);
  70     }
  71 
  72     /**
  73      * Gets the bean's Class object.
  74      *
  75      * @return The Class object for the bean.
  76      */
  77     public Class<?> getBeanClass() {
  78         return (this.beanClassRef != null)
  79                 ? this.beanClassRef.get()
  80                 : null;
  81     }
  82 
  83     /**
  84      * Gets the Class object for the bean's customizer.
  85      *
  86      * @return The Class object for the bean's customizer.  This may
  87      * be null if the bean doesn't have a customizer.
  88      */
  89     public Class<?> getCustomizerClass() {
  90         return (this.customizerClassRef != null)
  91                 ? this.customizerClassRef.get()
  92                 : null;
  93     }
  94 
  95     /*
  96      * Package-private dup constructor
  97      * This must isolate the new object from any changes to the old object.
  98      */
  99     BeanDescriptor(BeanDescriptor old) {
 100         super(old);
 101         beanClassRef = old.beanClassRef;
 102         customizerClassRef = old.customizerClassRef;
 103     }
 104 
 105     void appendTo(StringBuilder sb) {
 106         appendTo(sb, "beanClass", this.beanClassRef);
 107         appendTo(sb, "customizerClass", this.customizerClassRef);
 108     }
 109 }