1 /*
2  * Copyright (c) 1996, 2015, 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 package java.beans;
26
27 import java.lang.ref.Reference;
28 import javax.swing.SwingContainer;
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         JavaBean annotation = beanClass.getAnnotation(JavaBean.class);
74         if (annotation != null) {
75             setPreferred(true);
76             String description = annotation.description();
77             if (!description.isEmpty()) {
78                 setShortDescription(description);
79             }
80         }
81         SwingContainer container = beanClass.getAnnotation(SwingContainer.class);
82         if (container != null) {
83             setValue("isContainer", container.value());
84             setValue("containerDelegate", container.delegate());
85         }
86     }
87
88     /**
89      * Gets the bean's Class object.
90      *
91      * @return The Class object for the bean.
92      */

93     public Class<?> getBeanClass() {
94         return (this.beanClassRef != null)
95                 ? this.beanClassRef.get()
96                 : null;
97     }
98
99     /**
100      * Gets the Class object for the bean's customizer.
101      *
102      * @return The Class object for the bean's customizer.  This may
103      * be null if the bean doesn't have a customizer.
104      */

105     public Class<?> getCustomizerClass() {
106         return (this.customizerClassRef != null)
107                 ? this.customizerClassRef.get()
108                 : null;
109     }
110
111     /*
112      * Package-private dup constructor
113      * This must isolate the new object from any changes to the old object.
114      */

115     BeanDescriptor(BeanDescriptor old) {
116         super(old);
117         beanClassRef = old.beanClassRef;
118         customizerClassRef = old.customizerClassRef;
119     }
120
121     void appendTo(StringBuilder sb) {
122         appendTo(sb, "beanClass"this.beanClassRef);
123         appendTo(sb, "customizerClass"this.customizerClassRef);
124     }
125 }
126