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  * @since 1.1
  38  */
  39 
  40 public class BeanDescriptor extends FeatureDescriptor {
  41 
  42     private Reference<? extends Class<?>> beanClassRef;
  43     private Reference<? extends Class<?>> customizerClassRef;
  44 
  45     /**
  46      * Create a BeanDescriptor for a bean that doesn't have a customizer.
  47      *
  48      * @param beanClass  The Class object of the Java class that implements
  49      *          the bean.  For example sun.beans.OurButton.class.
  50      */
  51     public BeanDescriptor(Class<?> beanClass) {
  52         this(beanClass, null);
  53     }
  54 
  55     /**
  56      * Create a BeanDescriptor for a bean that has a customizer.
  57      *
  58      * @param beanClass  The Class object of the Java class that implements
  59      *          the bean.  For example sun.beans.OurButton.class.
  60      * @param customizerClass  The Class object of the Java class that implements
  61      *          the bean's Customizer.  For example sun.beans.OurButtonCustomizer.class.
  62      */
  63     public BeanDescriptor(Class<?> beanClass, Class<?> customizerClass) {
  64         this.beanClassRef = getWeakReference(beanClass);
  65         this.customizerClassRef = getWeakReference(customizerClass);
  66 
  67         String name = beanClass.getName();
  68         while (name.indexOf('.') >= 0) {
  69             name = name.substring(name.indexOf('.')+1);
  70         }
  71         setName(name);
  72     }
  73 
  74     /**
  75      * Gets the bean's Class object.
  76      *
  77      * @return The Class object for the bean.
  78      */
  79     public Class<?> getBeanClass() {
  80         return (this.beanClassRef != null)
  81                 ? this.beanClassRef.get()
  82                 : null;
  83     }
  84 
  85     /**
  86      * Gets the Class object for the bean's customizer.
  87      *
  88      * @return The Class object for the bean's customizer.  This may
  89      * be null if the bean doesn't have a customizer.
  90      */
  91     public Class<?> getCustomizerClass() {
  92         return (this.customizerClassRef != null)
  93                 ? this.customizerClassRef.get()
  94                 : null;
  95     }
  96 
  97     /*
  98      * Package-private dup constructor
  99      * This must isolate the new object from any changes to the old object.
 100      */
 101     BeanDescriptor(BeanDescriptor old) {
 102         super(old);
 103         beanClassRef = old.beanClassRef;
 104         customizerClassRef = old.customizerClassRef;
 105     }
 106 
 107     void appendTo(StringBuilder sb) {
 108         appendTo(sb, "beanClass", this.beanClassRef);
 109         appendTo(sb, "customizerClass", this.customizerClassRef);
 110     }
 111 }