jdk/src/share/classes/java/beans/Introspector.java
author malenkov
Tue, 09 Jul 2013 18:01:58 +0400
changeset 18757 5ab9e48648e5
parent 16837 1af9ab28cf6d
child 19213 c360667a0da2
permissions -rw-r--r--
6707231: Wrong read Method returned for boolen properties Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8760
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4851
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4851
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4851
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4851
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4851
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
    28
import com.sun.beans.TypeResolver;
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
    29
import com.sun.beans.WeakCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.beans.finder.ClassFinder;
16837
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
    31
import com.sun.beans.finder.MethodFinder;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
7249
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
    33
import java.awt.Component;
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
    34
4851
4f7de5eeeac7 6921057: REGRESSION: persistence delegate issue on Windows and Linux against 5.u23b03/6u17b11
malenkov
parents: 4393
diff changeset
    35
import java.lang.ref.Reference;
4f7de5eeeac7 6921057: REGRESSION: persistence delegate issue on Windows and Linux against 5.u23b03/6u17b11
malenkov
parents: 4393
diff changeset
    36
import java.lang.ref.SoftReference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.reflect.Modifier;
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
    39
import java.lang.reflect.Type;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.EventListener;
7249
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
    46
import java.util.EventObject;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.TreeMap;
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
    49
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.reflect.misc.ReflectUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The Introspector class provides a standard way for tools to learn about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the properties, events, and methods supported by a target Java Bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * For each of those three kinds of information, the Introspector will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * separately analyze the bean's class and superclasses looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * either explicit or implicit information and use that information to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * build a BeanInfo object that comprehensively describes the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * For each class "Foo", explicit information may be available if there exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * a corresponding "FooBeanInfo" class that provides a non-null value when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * queried for the information.   We first look for the BeanInfo class by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * taking the full package-qualified name of the target bean class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * appending "BeanInfo" to form a new class name.  If this fails, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * we take the final classname component of this name, and look for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * class in each of the packages specified in the BeanInfo package search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * Thus for a class such as "sun.xyz.OurButton" we would first look for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * BeanInfo class called "sun.xyz.OurButtonBeanInfo" and if that failed we'd
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * look in each package in the BeanInfo search path for an OurButtonBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * class.  With the default search path, this would mean looking for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * "sun.beans.infos.OurButtonBeanInfo".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * If a class provides explicit BeanInfo about itself then we add that to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * the BeanInfo information we obtained from analyzing any derived classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * but we regard the explicit information as being definitive for the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * class and its base classes, and do not proceed any further up the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * If we don't find explicit BeanInfo on a class, we use low-level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * reflection to study the methods of the class and apply standard design
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * patterns to identify property accessors, event sources, or public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * methods.  We then proceed to analyze the class's superclass and add
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * in the information from it (and possibly on up the superclass chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * For more information about introspection and design patterns, please
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * consult the
8760
83843c0b84f7 7021517: java.beans code comments have issues with HTML4 compliance
malenkov
parents: 7249
diff changeset
    90
 *  <a href="http://java.sun.com/products/javabeans/docs/index.html">JavaBeans&trade; specification</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
public class Introspector {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // Flags that can be used to control getBeanInfo:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public final static int USE_ALL_BEANINFO           = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public final static int IGNORE_IMMEDIATE_BEANINFO  = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public final static int IGNORE_ALL_BEANINFO        = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Static Caches to speed up introspection.
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   101
    private static final WeakCache<Class<?>, Method[]> declaredMethodCache = new WeakCache<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   103
    private Class<?> beanClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private BeanInfo explicitBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private BeanInfo superBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private BeanInfo additionalBeanInfo[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private boolean propertyChangeSource = false;
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   109
    private static Class<EventListener> eventListenerType = EventListener.class;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // These should be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private String defaultEventName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private String defaultPropertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private int defaultEventIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private int defaultPropertyIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   117
    // Methods maps from Method names to MethodDescriptors
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   118
    private Map<String, MethodDescriptor> methods;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    // properties maps from String names to PropertyDescriptors
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   121
    private Map<String, PropertyDescriptor> properties;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // events maps from String names to EventSetDescriptors
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   124
    private Map<String, EventSetDescriptor> events;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private final static EventSetDescriptor[] EMPTY_EVENTSETDESCRIPTORS = new EventSetDescriptor[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static final String ADD_PREFIX = "add";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    static final String REMOVE_PREFIX = "remove";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    static final String GET_PREFIX = "get";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    static final String SET_PREFIX = "set";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    static final String IS_PREFIX = "is";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    //                          Public methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Introspect on a Java Bean and learn about all its properties, exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * methods, and events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * If the BeanInfo class for a Java Bean has been previously Introspected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * then the BeanInfo class is retrieved from the BeanInfo cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param beanClass  The bean class to be analyzed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return  A BeanInfo object describing the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see #flushCaches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @see #flushFromCaches
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static BeanInfo getBeanInfo(Class<?> beanClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        throws IntrospectionException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (!ReflectUtil.isPackageAccessible(beanClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   158
        ThreadGroupContext context = ThreadGroupContext.getContext();
5959
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5947
diff changeset
   159
        BeanInfo beanInfo;
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   160
        synchronized (declaredMethodCache) {
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   161
            beanInfo = context.getBeanInfo(beanClass);
5959
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5947
diff changeset
   162
        }
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5947
diff changeset
   163
        if (beanInfo == null) {
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5947
diff changeset
   164
            beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   165
            synchronized (declaredMethodCache) {
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   166
                context.putBeanInfo(beanClass, beanInfo);
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
   167
            }
3438
8bd2d2eeac83 6660539: Introspector shares cache of mutable BeanInfo between AppContexts.
malenkov
parents: 2
diff changeset
   168
        }
5959
3ff758c11233 6963811: Deadlock-prone locking changes in Introspector
malenkov
parents: 5947
diff changeset
   169
        return beanInfo;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Introspect on a Java bean and learn about all its properties, exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * methods, and events, subject to some control flags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * If the BeanInfo class for a Java Bean has been previously Introspected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * based on the same arguments then the BeanInfo class is retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * from the BeanInfo cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param beanClass  The bean class to be analyzed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param flags  Flags to control the introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *     If flags == USE_ALL_BEANINFO then we use all of the BeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *          classes we can discover.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *     If flags == IGNORE_IMMEDIATE_BEANINFO then we ignore any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *           BeanInfo associated with the specified beanClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *     If flags == IGNORE_ALL_BEANINFO then we ignore all BeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *           associated with the specified beanClass or any of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *           parent classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  A BeanInfo object describing the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public static BeanInfo getBeanInfo(Class<?> beanClass, int flags)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                                throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return getBeanInfo(beanClass, null, flags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Introspect on a Java bean and learn all about its properties, exposed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * methods, below a given "stop" point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * If the BeanInfo class for a Java Bean has been previously Introspected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * based on the same arguments, then the BeanInfo class is retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * from the BeanInfo cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param beanClass The bean class to be analyzed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param stopClass The baseclass at which to stop the analysis.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *    methods/properties/events in the stopClass or in its baseclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *    will be ignored in the analysis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception IntrospectionException if an exception occurs during
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *              introspection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                                throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return getBeanInfo(beanClass, stopClass, USE_ALL_BEANINFO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * Introspect on a Java Bean and learn about all its properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * exposed methods and events, below a given {@code stopClass} point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * subject to some control {@code flags}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *  <dt>USE_ALL_BEANINFO</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *  <dd>Any BeanInfo that can be discovered will be used.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *  <dt>IGNORE_IMMEDIATE_BEANINFO</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *  <dd>Any BeanInfo associated with the specified {@code beanClass} will be ignored.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *  <dt>IGNORE_ALL_BEANINFO</dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *  <dd>Any BeanInfo associated with the specified {@code beanClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *      or any of its parent classes will be ignored.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Any methods/properties/events in the {@code stopClass}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * or in its parent classes will be ignored in the analysis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * If the BeanInfo class for a Java Bean has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * previously introspected based on the same arguments then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the BeanInfo class is retrieved from the BeanInfo cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param beanClass  the bean class to be analyzed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param stopClass  the parent class at which to stop the analysis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param flags      flags to control the introspection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return a BeanInfo object describing the target bean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception IntrospectionException if an exception occurs during introspection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @since 1.7
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                        int flags) throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        BeanInfo bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (stopClass == null && flags == USE_ALL_BEANINFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Same parameters to take advantage of caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            bi = getBeanInfo(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            bi = (new Introspector(beanClass, stopClass, flags)).getBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return bi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        // Old behaviour: Make an independent copy of the BeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        //return new GenericBeanInfo(bi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Utility method to take a string and convert it to normal Java variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * name capitalization.  This normally means converting the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * character from upper case to lower case, but in the (unusual) special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * case when there is more than one character and both the first and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * second characters are upper case, we leave it alone.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * as "URL".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param  name The string to be decapitalized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @return  The decapitalized version of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public static String decapitalize(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (name == null || name.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        Character.isUpperCase(name.charAt(0))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        char chars[] = name.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        chars[0] = Character.toLowerCase(chars[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return new String(chars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Gets the list of package names that will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *          finding BeanInfo classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @return  The array of package names that will be searched in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *          order to find BeanInfo classes. The default value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *          for this array is implementation-dependent; e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *          Sun implementation initially sets to {"sun.beans.infos"}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
   298
    public static String[] getBeanInfoSearchPath() {
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   299
        return ThreadGroupContext.getContext().getBeanInfoFinder().getPackages();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Change the list of package names that will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *          finding BeanInfo classes.  The behaviour of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *          this method is undefined if parameter path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <p>First, if there is a security manager, its <code>checkPropertiesAccess</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * method is called. This could result in a SecurityException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param path  Array of package names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @exception  SecurityException  if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *             <code>checkPropertiesAccess</code> method doesn't allow setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *              of system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @see SecurityManager#checkPropertiesAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
   318
    public static void setBeanInfoSearchPath(String[] path) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            sm.checkPropertiesAccess();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   323
        ThreadGroupContext.getContext().getBeanInfoFinder().setPackages(path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Flush all of the Introspector's internal caches.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * not normally required.  It is normally only needed by advanced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * tools that update existing "Class" objects in-place and need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * to make the Introspector re-analyze existing Class objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public static void flushCaches() {
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   335
        synchronized (declaredMethodCache) {
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   336
            ThreadGroupContext.getContext().clearBeanInfoCache();
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
   337
            declaredMethodCache.clear();
3438
8bd2d2eeac83 6660539: Introspector shares cache of mutable BeanInfo between AppContexts.
malenkov
parents: 2
diff changeset
   338
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Flush the Introspector's internal cached information for a given class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * This method is not normally required.  It is normally only needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * by advanced tools that update existing "Class" objects in-place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * and need to make the Introspector re-analyze an existing Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Note that only the direct state associated with the target Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * object is flushed.  We do not flush state for other Class objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * with the same name, nor do we flush state for any related Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * objects (such as subclasses), even though their state may include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * information indirectly obtained from the target Class object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param clz  Class object to be flushed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @throws NullPointerException If the Class object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public static void flushFromCaches(Class<?> clz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (clz == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   360
        synchronized (declaredMethodCache) {
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   361
            ThreadGroupContext.getContext().removeBeanInfo(clz);
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
   362
            declaredMethodCache.put(clz, null);
3438
8bd2d2eeac83 6660539: Introspector shares cache of mutable BeanInfo between AppContexts.
malenkov
parents: 2
diff changeset
   363
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    //                  Private implementation methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   370
    private Introspector(Class<?> beanClass, Class<?> stopClass, int flags)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                            throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        this.beanClass = beanClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // Check stopClass is a superClass of startClass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        if (stopClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            boolean isSuper = false;
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   377
            for (Class<?> c = beanClass.getSuperclass(); c != null; c = c.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (c == stopClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    isSuper = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (!isSuper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw new IntrospectionException(stopClass.getName() + " not superclass of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                        beanClass.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (flags == USE_ALL_BEANINFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            explicitBeanInfo = findExplicitBeanInfo(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   392
        Class<?> superClass = beanClass.getSuperclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        if (superClass != stopClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int newFlags = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (newFlags == IGNORE_IMMEDIATE_BEANINFO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                newFlags = USE_ALL_BEANINFO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            superBeanInfo = getBeanInfo(superClass, stopClass, newFlags);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (explicitBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            additionalBeanInfo = explicitBeanInfo.getAdditionalBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (additionalBeanInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            additionalBeanInfo = new BeanInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Constructs a GenericBeanInfo class from the state of the Introspector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private BeanInfo getBeanInfo() throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // the evaluation order here is import, as we evaluate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        // event sets and locate PropertyChangeListeners before we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        // look for properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        BeanDescriptor bd = getTargetBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        MethodDescriptor mds[] = getTargetMethodInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        EventSetDescriptor esds[] = getTargetEventInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        PropertyDescriptor pds[] = getTargetPropertyInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        int defaultEvent = getTargetDefaultEventIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        int defaultProperty = getTargetDefaultPropertyIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        return new GenericBeanInfo(bd, esds, defaultEvent, pds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                        defaultProperty, mds, explicitBeanInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Looks for an explicit BeanInfo class that corresponds to the Class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * First it looks in the existing package that the Class is defined in,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * then it checks to see if the class is its own BeanInfo. Finally,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * the BeanInfo search path is prepended to the class and searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
3239
f675984c2349 6380849: RFE: Automatic discovery of PersistanceDelegates
malenkov
parents: 2
diff changeset
   435
     * @param beanClass  the class type of the bean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @return Instance of an explicit BeanInfo class or null if one isn't found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   438
    private static BeanInfo findExplicitBeanInfo(Class<?> beanClass) {
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
   439
        return ThreadGroupContext.getContext().getBeanInfoFinder().find(beanClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return An array of PropertyDescriptors describing the editable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * properties supported by the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private PropertyDescriptor[] getTargetPropertyInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        // Check if the bean has its own BeanInfo that will provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // explicit information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        PropertyDescriptor[] explicitProperties = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (explicitBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            explicitProperties = getPropertyDescriptors(this.explicitBeanInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if (explicitProperties == null && superBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // We have no explicit BeanInfo properties.  Check with our parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            addPropertyDescriptors(getPropertyDescriptors(this.superBeanInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        for (int i = 0; i < additionalBeanInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            addPropertyDescriptors(additionalBeanInfo[i].getPropertyDescriptors());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (explicitProperties != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            // Add the explicit BeanInfo data to our results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            addPropertyDescriptors(explicitProperties);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // Apply some reflection to the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // First get an array of all the public methods at this level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            Method methodList[] = getPublicDeclaredMethods(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // Now analyze each method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            for (int i = 0; i < methodList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                Method method = methodList[i];
11668
138b661e3841 7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents: 11140
diff changeset
   479
                if (method == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                // skip static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                int mods = method.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                if (Modifier.isStatic(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                String name = method.getName();
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   488
                Class<?>[] argTypes = method.getParameterTypes();
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   489
                Class<?> resultType = method.getReturnType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                int argCount = argTypes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                PropertyDescriptor pd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                if (name.length() <= 3 && !name.startsWith(IS_PREFIX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    // Optimization. Don't bother with invalid propertyNames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    if (argCount == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        if (name.startsWith(GET_PREFIX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            // Simple getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                            pd = new PropertyDescriptor(this.beanClass, name.substring(3), method, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                        } else if (resultType == boolean.class && name.startsWith(IS_PREFIX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                            // Boolean getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                            pd = new PropertyDescriptor(this.beanClass, name.substring(2), method, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    } else if (argCount == 1) {
3241
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 3239
diff changeset
   509
                        if (int.class.equals(argTypes[0]) && name.startsWith(GET_PREFIX)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                            pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, method, null);
3241
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 3239
diff changeset
   511
                        } else if (void.class.equals(resultType) && name.startsWith(SET_PREFIX)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                            // Simple setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                            pd = new PropertyDescriptor(this.beanClass, name.substring(3), null, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                            if (throwsException(method, PropertyVetoException.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                                pd.setConstrained(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                    } else if (argCount == 2) {
3241
6fd229e009e7 6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents: 3239
diff changeset
   519
                            if (void.class.equals(resultType) && int.class.equals(argTypes[0]) && name.startsWith(SET_PREFIX)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                            pd = new IndexedPropertyDescriptor(this.beanClass, name.substring(3), null, null, null, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                            if (throwsException(method, PropertyVetoException.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                pd.setConstrained(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    // This happens if a PropertyDescriptor or IndexedPropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    // constructor fins that the method violates details of the deisgn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    // pattern, e.g. by having an empty name, or a getter returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    // void , or whatever.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    pd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                if (pd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    // If this class or one of its base classes is a PropertyChange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    // source, then we assume that any properties we discover are "bound".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    if (propertyChangeSource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                        pd.setBound(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    addPropertyDescriptor(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        processPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // Allocate and populate the result array.
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   547
        PropertyDescriptor result[] =
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   548
                properties.values().toArray(new PropertyDescriptor[properties.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        // Set the default index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (defaultPropertyName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            for (int i = 0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                if (defaultPropertyName.equals(result[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    defaultPropertyIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   562
    private HashMap<String, List<PropertyDescriptor>> pdStore = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Adds the property descriptor to the list store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    private void addPropertyDescriptor(PropertyDescriptor pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        String propName = pd.getName();
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   569
        List<PropertyDescriptor> list = pdStore.get(propName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        if (list == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   571
            list = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            pdStore.put(propName, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        if (this.beanClass != pd.getClass0()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            // replace existing property descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            // only if we have types to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            // in the context of this.beanClass
13773
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   578
            Method read = pd.getReadMethod();
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   579
            Method write = pd.getWriteMethod();
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   580
            boolean cls = true;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   581
            if (read != null) cls = cls && read.getGenericReturnType() instanceof Class;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   582
            if (write != null) cls = cls && write.getGenericParameterTypes()[0] instanceof Class;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   583
            if (pd instanceof IndexedPropertyDescriptor) {
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   584
                IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   585
                Method readI = ipd.getIndexedReadMethod();
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   586
                Method writeI = ipd.getIndexedWriteMethod();
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   587
                if (readI != null) cls = cls && readI.getGenericReturnType() instanceof Class;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   588
                if (writeI != null) cls = cls && writeI.getGenericParameterTypes()[1] instanceof Class;
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   589
                if (!cls) {
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   590
                    pd = new IndexedPropertyDescriptor(ipd);
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   591
                    pd.updateGenericsFor(this.beanClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                }
13773
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   593
            }
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   594
            else if (!cls) {
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   595
                pd = new PropertyDescriptor(pd);
69665c88db93 7193977: REGRESSION:Java 7's JavaBeans persistence ignoring the "transient" flag on properties
malenkov
parents: 13654
diff changeset
   596
                pd.updateGenericsFor(this.beanClass);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        list.add(pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private void addPropertyDescriptors(PropertyDescriptor[] descriptors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (descriptors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            for (PropertyDescriptor descriptor : descriptors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                addPropertyDescriptor(descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private PropertyDescriptor[] getPropertyDescriptors(BeanInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int index = info.getDefaultPropertyIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if ((0 <= index) && (index < descriptors.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            this.defaultPropertyName = descriptors[index].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return descriptors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Populates the property descriptor table by merging the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * lists of Property descriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    private void processPropertyDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (properties == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   625
            properties = new TreeMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   628
        List<PropertyDescriptor> list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        PropertyDescriptor pd, gpd, spd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        IndexedPropertyDescriptor ipd, igpd, ispd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   633
        Iterator<List<PropertyDescriptor>> it = pdStore.values().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            pd = null; gpd = null; spd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            ipd = null; igpd = null; ispd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   638
            list = it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            // First pass. Find the latest getter method. Merge properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            // of previous getter methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            for (int i = 0; i < list.size(); i++) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   643
                pd = list.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    ipd = (IndexedPropertyDescriptor)pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    if (ipd.getIndexedReadMethod() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                        if (igpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                            igpd = new IndexedPropertyDescriptor(igpd, ipd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                            igpd = ipd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    if (pd.getReadMethod() != null) {
18757
5ab9e48648e5 6707231: Wrong read Method returned for boolen properties
malenkov
parents: 16837
diff changeset
   655
                        String pdName = pd.getReadMethod().getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                        if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                            // Don't replace the existing read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                            // method if it starts with "is"
18757
5ab9e48648e5 6707231: Wrong read Method returned for boolen properties
malenkov
parents: 16837
diff changeset
   659
                            String gpdName = gpd.getReadMethod().getName();
5ab9e48648e5 6707231: Wrong read Method returned for boolen properties
malenkov
parents: 16837
diff changeset
   660
                            if (gpdName.equals(pdName) || !gpdName.startsWith(IS_PREFIX)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                                gpd = new PropertyDescriptor(gpd, pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                            gpd = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            // Second pass. Find the latest setter method which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            // has the same type as the getter method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            for (int i = 0; i < list.size(); i++) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   673
                pd = list.get(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    ipd = (IndexedPropertyDescriptor)pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                    if (ipd.getIndexedWriteMethod() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        if (igpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                            if (igpd.getIndexedPropertyType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                                == ipd.getIndexedPropertyType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                                if (ispd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                                    ispd = new IndexedPropertyDescriptor(ispd, ipd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                    ispd = ipd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                            if (ispd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                                ispd = new IndexedPropertyDescriptor(ispd, ipd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                                ispd = ipd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                    if (pd.getWriteMethod() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                        if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                            if (gpd.getPropertyType() == pd.getPropertyType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                                if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                                    spd = new PropertyDescriptor(spd, pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                    spd = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                            if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                                spd = new PropertyDescriptor(spd, pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                                spd = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            // At this stage we should have either PDs or IPDs for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            // representative getters and setters. The order at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            // property descriptors are determined represent the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            // precedence of the property ordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            pd = null; ipd = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            if (igpd != null && ispd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                // Complete indexed properties set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                // Merge any classic property descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    PropertyDescriptor tpd = mergePropertyDescriptor(igpd, gpd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    if (tpd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        igpd = (IndexedPropertyDescriptor)tpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    PropertyDescriptor tpd = mergePropertyDescriptor(ispd, spd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    if (tpd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        ispd = (IndexedPropertyDescriptor)tpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                if (igpd == ispd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    pd = igpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                    pd = mergePropertyDescriptor(igpd, ispd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            } else if (gpd != null && spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                // Complete simple properties set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                if (gpd == spd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                    pd = gpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                    pd = mergePropertyDescriptor(gpd, spd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            } else if (ispd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                // indexed setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                pd = ispd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                // Merge any classic property descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    pd = mergePropertyDescriptor(ispd, spd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    pd = mergePropertyDescriptor(ispd, gpd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            } else if (igpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                // indexed getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                pd = igpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                // Merge any classic property descriptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    pd = mergePropertyDescriptor(igpd, gpd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    pd = mergePropertyDescriptor(igpd, spd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            } else if (spd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                // simple setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                pd = spd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            } else if (gpd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                // simple getter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                pd = gpd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            // Very special case to ensure that an IndexedPropertyDescriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            // doesn't contain less information than the enclosed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            // PropertyDescriptor. If it does, then recreate as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            // PropertyDescriptor. See 4168833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            if (pd instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                ipd = (IndexedPropertyDescriptor)pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                if (ipd.getIndexedReadMethod() == null && ipd.getIndexedWriteMethod() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    pd = new PropertyDescriptor(ipd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            // Find the first property descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // which does not have getter and setter methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            // See regression bug 4984912.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            if ( (pd == null) && (list.size() > 0) ) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   791
                pd = list.get(0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            if (pd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                properties.put(pd.getName(), pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Adds the property descriptor to the indexedproperty descriptor only if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * types are the same.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * The most specific property descriptor will take precedence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                                       PropertyDescriptor pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        PropertyDescriptor result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   810
        Class<?> propType = pd.getPropertyType();
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   811
        Class<?> ipropType = ipd.getIndexedPropertyType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (propType.isArray() && propType.getComponentType() == ipropType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                result = new IndexedPropertyDescriptor(pd, ipd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                result = new IndexedPropertyDescriptor(ipd, pd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            // Cannot merge the pd because of type mismatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            // Return the most specific pd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                result = ipd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                result = pd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                // Try to add methods which may have been lost in the type change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                // See 4168833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                Method write = result.getWriteMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                Method read = result.getReadMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                if (read == null && write != null) {
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
   832
                    read = findMethod(result.getClass0(),
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
   833
                                      GET_PREFIX + NameGenerator.capitalize(result.getName()), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                    if (read != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                            result.setReadMethod(read);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                            // no consequences for failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                if (write == null && read != null) {
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
   843
                    write = findMethod(result.getClass0(),
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
   844
                                       SET_PREFIX + NameGenerator.capitalize(result.getName()), 1,
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   845
                                       new Class<?>[] { FeatureDescriptor.getReturnType(result.getClass0(), read) });
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    if (write != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                            result.setWriteMethod(write);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        } catch (IntrospectionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                            // no consequences for failure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    // Handle regular pd merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    private PropertyDescriptor mergePropertyDescriptor(PropertyDescriptor pd1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                                       PropertyDescriptor pd2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        if (pd1.getClass0().isAssignableFrom(pd2.getClass0())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return new PropertyDescriptor(pd1, pd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            return new PropertyDescriptor(pd2, pd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    // Handle regular ipd merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                                                       IndexedPropertyDescriptor ipd2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        if (ipd1.getClass0().isAssignableFrom(ipd2.getClass0())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            return new IndexedPropertyDescriptor(ipd1, ipd2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return new IndexedPropertyDescriptor(ipd2, ipd1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @return An array of EventSetDescriptors describing the kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * events fired by the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    private EventSetDescriptor[] getTargetEventInfo() throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        if (events == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   885
            events = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        // Check if the bean has its own BeanInfo that will provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        // explicit information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        EventSetDescriptor[] explicitEvents = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        if (explicitBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            explicitEvents = explicitBeanInfo.getEventSetDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            int ix = explicitBeanInfo.getDefaultEventIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (ix >= 0 && ix < explicitEvents.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                defaultEventName = explicitEvents[ix].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (explicitEvents == null && superBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            // We have no explicit BeanInfo events.  Check with our parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            EventSetDescriptor supers[] = superBeanInfo.getEventSetDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            for (int i = 0 ; i < supers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                addEvent(supers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            int ix = superBeanInfo.getDefaultEventIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (ix >= 0 && ix < supers.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                defaultEventName = supers[ix].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        for (int i = 0; i < additionalBeanInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            EventSetDescriptor additional[] = additionalBeanInfo[i].getEventSetDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            if (additional != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                for (int j = 0 ; j < additional.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    addEvent(additional[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        if (explicitEvents != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            // Add the explicit explicitBeanInfo data to our results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            for (int i = 0 ; i < explicitEvents.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                addEvent(explicitEvents[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            // Apply some reflection to the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            // Get an array of all the public beans methods at this level
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            Method methodList[] = getPublicDeclaredMethods(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            // Find all suitable "add", "remove" and "get" Listener methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            // The name of the listener type is the key for these hashtables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            // i.e, ActionListener
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   936
            Map<String, Method> adds = null;
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   937
            Map<String, Method> removes = null;
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
   938
            Map<String, Method> gets = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            for (int i = 0; i < methodList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                Method method = methodList[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                // skip static methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                int mods = method.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                if (Modifier.isStatic(mods)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                String name = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                // Optimization avoid getParameterTypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                if (!name.startsWith(ADD_PREFIX) && !name.startsWith(REMOVE_PREFIX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                    && !name.startsWith(GET_PREFIX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   957
                if (name.startsWith(ADD_PREFIX)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   958
                    Class<?> returnType = method.getReturnType();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   959
                    if (returnType == void.class) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   960
                        Type[] parameterTypes = method.getGenericParameterTypes();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   961
                        if (parameterTypes.length == 1) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   962
                            Class<?> type = TypeResolver.erase(TypeResolver.resolveInClass(beanClass, parameterTypes[0]));
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   963
                            if (Introspector.isSubclass(type, eventListenerType)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   964
                                String listenerName = name.substring(3);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   965
                                if (listenerName.length() > 0 &&
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   966
                                    type.getName().endsWith(listenerName)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   967
                                    if (adds == null) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   968
                                        adds = new HashMap<>();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   969
                                    }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   970
                                    adds.put(listenerName, method);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   971
                                }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   972
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                }
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   976
                else if (name.startsWith(REMOVE_PREFIX)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   977
                    Class<?> returnType = method.getReturnType();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   978
                    if (returnType == void.class) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   979
                        Type[] parameterTypes = method.getGenericParameterTypes();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   980
                        if (parameterTypes.length == 1) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   981
                            Class<?> type = TypeResolver.erase(TypeResolver.resolveInClass(beanClass, parameterTypes[0]));
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   982
                            if (Introspector.isSubclass(type, eventListenerType)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   983
                                String listenerName = name.substring(6);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   984
                                if (listenerName.length() > 0 &&
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   985
                                    type.getName().endsWith(listenerName)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   986
                                    if (removes == null) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   987
                                        removes = new HashMap<>();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   988
                                    }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   989
                                    removes.put(listenerName, method);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   990
                                }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   991
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                }
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   995
                else if (name.startsWith(GET_PREFIX)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   996
                    Class<?>[] parameterTypes = method.getParameterTypes();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   997
                    if (parameterTypes.length == 0) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   998
                        Class<?> returnType = FeatureDescriptor.getReturnType(beanClass, method);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
   999
                        if (returnType.isArray()) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1000
                            Class<?> type = returnType.getComponentType();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1001
                            if (Introspector.isSubclass(type, eventListenerType)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1002
                                String listenerName  = name.substring(3, name.length() - 1);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1003
                                if (listenerName.length() > 0 &&
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1004
                                    type.getName().endsWith(listenerName)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1005
                                    if (gets == null) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1006
                                        gets = new HashMap<>();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1007
                                    }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1008
                                    gets.put(listenerName, method);
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1009
                                }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1010
                            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            if (adds != null && removes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                // Now look for matching addFooListener+removeFooListener pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                // Bonus if there is a matching getFooListeners method as well.
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1019
                Iterator<String> keys = adds.keySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                while (keys.hasNext()) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1021
                    String listenerName = keys.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                    // Skip any "add" which doesn't have a matching "remove" or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    // a listener name that doesn't end with Listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    if (removes.get(listenerName) == null || !listenerName.endsWith("Listener")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                    String eventName = decapitalize(listenerName.substring(0, listenerName.length()-8));
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1028
                    Method addMethod = adds.get(listenerName);
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1029
                    Method removeMethod = removes.get(listenerName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                    Method getMethod = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    if (gets != null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1032
                        getMethod = gets.get(listenerName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                    }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1034
                    Class<?> argType = FeatureDescriptor.getParameterTypes(beanClass, addMethod)[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    // generate a list of Method objects for each of the target methods:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    Method allMethods[] = getPublicDeclaredMethods(argType);
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1038
                    List<Method> validMethods = new ArrayList<>(allMethods.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                    for (int i = 0; i < allMethods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                        if (allMethods[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        if (isEventHandler(allMethods[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                            validMethods.add(allMethods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1048
                    Method[] methods = validMethods.toArray(new Method[validMethods.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                    EventSetDescriptor esd = new EventSetDescriptor(eventName, argType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                                                                    methods, addMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                                                    removeMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                                                                    getMethod);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    // If the adder method throws the TooManyListenersException then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    // is a Unicast event source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    if (throwsException(addMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                                        java.util.TooManyListenersException.class)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        esd.setUnicast(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    addEvent(esd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            } // if (adds != null ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        EventSetDescriptor[] result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        if (events.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            result = EMPTY_EVENTSETDESCRIPTORS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            // Allocate and populate the result array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            result = new EventSetDescriptor[events.size()];
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1071
            result = events.values().toArray(result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            // Set the default index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            if (defaultEventName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                for (int i = 0; i < result.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    if (defaultEventName.equals(result[i].getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        defaultEventIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    private void addEvent(EventSetDescriptor esd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        String key = esd.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (esd.getName().equals("propertyChange")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            propertyChangeSource = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1090
        EventSetDescriptor old = events.get(key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (old == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            events.put(key, esd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        EventSetDescriptor composite = new EventSetDescriptor(old, esd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        events.put(key, composite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * @return An array of MethodDescriptors describing the private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * methods supported by the target bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    private MethodDescriptor[] getTargetMethodInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        if (methods == null) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1105
            methods = new HashMap<>(100);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        // Check if the bean has its own BeanInfo that will provide
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        // explicit information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        MethodDescriptor[] explicitMethods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if (explicitBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            explicitMethods = explicitBeanInfo.getMethodDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (explicitMethods == null && superBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            // We have no explicit BeanInfo methods.  Check with our parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            MethodDescriptor supers[] = superBeanInfo.getMethodDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            for (int i = 0 ; i < supers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                addMethod(supers[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        for (int i = 0; i < additionalBeanInfo.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            MethodDescriptor additional[] = additionalBeanInfo[i].getMethodDescriptors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            if (additional != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                for (int j = 0 ; j < additional.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    addMethod(additional[j]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        if (explicitMethods != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            // Add the explicit explicitBeanInfo data to our results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            for (int i = 0 ; i < explicitMethods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                addMethod(explicitMethods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            // Apply some reflection to the current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            // First get an array of all the beans methods at this level
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            Method methodList[] = getPublicDeclaredMethods(beanClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            // Now analyze each method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            for (int i = 0; i < methodList.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                Method method = methodList[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                MethodDescriptor md = new MethodDescriptor(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                addMethod(md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        // Allocate and populate the result array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        MethodDescriptor result[] = new MethodDescriptor[methods.size()];
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1158
        result = methods.values().toArray(result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    private void addMethod(MethodDescriptor md) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        // We have to be careful here to distinguish method by both name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        // and argument lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        // This method gets called a *lot, so we try to be efficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        String name = md.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1169
        MethodDescriptor old = methods.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        if (old == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            // This is the common case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            methods.put(name, md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        // We have a collision on method names.  This is rare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        // Check if old and md have the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        String[] p1 = md.getParamNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        String[] p2 = old.getParamNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        boolean match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (p1.length == p2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            match = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            for (int i = 0; i < p1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                if (p1[i] != p2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    match = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        if (match) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            MethodDescriptor composite = new MethodDescriptor(old, md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            methods.put(name, composite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        // We have a collision on method names with different type signatures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        // This is very rare.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        String longKey = makeQualifiedMethodName(name, p1);
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1202
        old = methods.get(longKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        if (old == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            methods.put(longKey, md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        MethodDescriptor composite = new MethodDescriptor(old, md);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        methods.put(longKey, composite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * Creates a key for a method in a method cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    private static String makeQualifiedMethodName(String name, String[] params) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        StringBuffer sb = new StringBuffer(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        sb.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        for (int i = 0; i < params.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            sb.append(params[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    private int getTargetDefaultEventIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        return defaultEventIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
    private int getTargetDefaultPropertyIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        return defaultPropertyIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    private BeanDescriptor getTargetBeanDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        // Use explicit info, if available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        if (explicitBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            BeanDescriptor bd = explicitBeanInfo.getBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
            if (bd != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                return (bd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        // OK, fabricate a default BeanDescriptor.
7249
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1241
        return new BeanDescriptor(this.beanClass, findCustomizerClass(this.beanClass));
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1242
    }
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1243
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1244
    private static Class<?> findCustomizerClass(Class<?> type) {
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1245
        String name = type.getName() + "Customizer";
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1246
        try {
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1247
            type = ClassFinder.findClass(name, type.getClassLoader());
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1248
            // Each customizer should inherit java.awt.Component and implement java.beans.Customizer
8760
83843c0b84f7 7021517: java.beans code comments have issues with HTML4 compliance
malenkov
parents: 7249
diff changeset
  1249
            // according to the section 9.3 of JavaBeans&trade; specification
7249
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1250
            if (Component.class.isAssignableFrom(type) && Customizer.class.isAssignableFrom(type)) {
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1251
                return type;
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1252
            }
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1253
        }
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1254
        catch (Exception exception) {
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1255
            // ignore any exceptions
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1256
        }
34380acce3b6 6447751: Bean Customizer should be detectable by reflection name alone
malenkov
parents: 6657
diff changeset
  1257
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
    private boolean isEventHandler(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        // We assume that a method is an event handler if it has a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        // argument, whose type inherit from java.util.Event.
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1263
        Type argTypes[] = m.getGenericParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (argTypes.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        }
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1267
        return isSubclass(TypeResolver.erase(TypeResolver.resolveInClass(beanClass, argTypes[0])), EventObject.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * Internal method to return *public* methods within a class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1273
    private static Method[] getPublicDeclaredMethods(Class<?> clz) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        // Looking up Class.getDeclaredMethods is relatively expensive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        // so we cache the results.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        if (!ReflectUtil.isPackageAccessible(clz)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            return new Method[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        }
11086
09b1d6383d0b 7064279: Introspector.getBeanInfo() should release some resources in timely manner
malenkov
parents: 9035
diff changeset
  1279
        synchronized (declaredMethodCache) {
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1280
            Method[] result = declaredMethodCache.get(clz);
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1281
            if (result == null) {
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1282
                result = clz.getMethods();
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1283
                for (int i = 0; i < result.length; i++) {
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1284
                    Method method = result[i];
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1285
                    if (!method.getDeclaringClass().equals(clz)) {
16837
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1286
                        result[i] = null; // ignore methods declared elsewhere
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1287
                    }
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1288
                    else {
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1289
                        try {
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1290
                            method = MethodFinder.findAccessibleMethod(method);
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1291
                            Class<?> type = method.getDeclaringClass();
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1292
                            result[i] = type.equals(clz) || type.isInterface()
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1293
                                    ? method
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1294
                                    : null; // ignore methods from superclasses
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1295
                        }
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1296
                        catch (NoSuchMethodException exception) {
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1297
                            // commented out because of 6976577
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1298
                            // result[i] = null; // ignore inaccessible methods
1af9ab28cf6d 4683761: Incomplete Introspection on nonpublic classes lead to IllegalAccessExceptions
malenkov
parents: 13773
diff changeset
  1299
                        }
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1300
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                }
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1302
                declaredMethodCache.put(clz, result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            }
4382
c0759b1ccb56 5102804: Memory leak in Introspector.getBeanInfo(Class) for custom BeanInfo: Class param
malenkov
parents: 3476
diff changeset
  1304
            return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    // Package private support methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    //======================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1312
    /**
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1313
     * Internal support for finding a target methodName with a given
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1314
     * parameter list on a given class.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1315
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1316
    private static Method internalFindMethod(Class<?> start, String methodName,
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1317
                                                 int argCount, Class args[]) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1318
        // For overriden methods we need to find the most derived version.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1319
        // So we start with the given class and walk up the superclass chain.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1320
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1321
        Method method = null;
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1322
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1323
        for (Class<?> cl = start; cl != null; cl = cl.getSuperclass()) {
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1324
            Method methods[] = getPublicDeclaredMethods(cl);
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1325
            for (int i = 0; i < methods.length; i++) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1326
                method = methods[i];
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1327
                if (method == null) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1328
                    continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                }
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1330
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1331
                // make sure method signature matches.
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1332
                if (method.getName().equals(methodName)) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1333
                    Type[] params = method.getGenericParameterTypes();
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1334
                    if (params.length == argCount) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1335
                        if (args != null) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1336
                            boolean different = false;
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1337
                            if (argCount > 0) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1338
                                for (int j = 0; j < argCount; j++) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1339
                                    if (TypeResolver.erase(TypeResolver.resolveInClass(start, params[j])) != args[j]) {
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1340
                                        different = true;
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1341
                                        continue;
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1342
                                    }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1343
                                }
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1344
                                if (different) {
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1345
                                    continue;
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1346
                                }
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1347
                            }
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1348
                        }
13356
89a34c00fd8c 7187618: PropertyDescriptor Performance Slow
malenkov
parents: 11668
diff changeset
  1349
                        return method;
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1350
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        }
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1354
        method = null;
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1355
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1356
        // Now check any inherited interfaces.  This is necessary both when
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1357
        // the argument class is itself an interface, and when the argument
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1358
        // class is an abstract class.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1359
        Class ifcs[] = start.getInterfaces();
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1360
        for (int i = 0 ; i < ifcs.length; i++) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1361
            // Note: The original implementation had both methods calling
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1362
            // the 3 arg method. This is preserved but perhaps it should
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1363
            // pass the args array instead of null.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1364
            method = internalFindMethod(ifcs[i], methodName, argCount, null);
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1365
            if (method != null) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1366
                break;
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1367
            }
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1368
        }
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1369
        return method;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1372
    /**
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1373
     * Find a target methodName on a given class.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1374
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1375
    static Method findMethod(Class<?> cls, String methodName, int argCount) {
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1376
        return findMethod(cls, methodName, argCount, null);
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1377
    }
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1378
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1379
    /**
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1380
     * Find a target methodName with specific parameter list on a given class.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1381
     * <p>
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1382
     * Used in the contructors of the EventSetDescriptor,
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1383
     * PropertyDescriptor and the IndexedPropertyDescriptor.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1384
     * <p>
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1385
     * @param cls The Class object on which to retrieve the method.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1386
     * @param methodName Name of the method.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1387
     * @param argCount Number of arguments for the desired method.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1388
     * @param args Array of argument types for the method.
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1389
     * @return the method or null if not found
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1390
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1391
    static Method findMethod(Class<?> cls, String methodName, int argCount,
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1392
                             Class args[]) {
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1393
        if (methodName == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
6657
15dbb366c6a3 6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents: 5959
diff changeset
  1396
        return internalFindMethod(cls, methodName, argCount, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * Return true if class a is either equivalent to class b, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * if class a is a subclass of class b, i.e. if a either "extends"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * or "implements" b.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     * Note tht either or both "Class" objects may represent interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1405
    static  boolean isSubclass(Class<?> a, Class<?> b) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        // We rely on the fact that for any given java class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        // primtitive type there is a unqiue Class object, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        // we can use object equivalence in the comparisons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        if (a == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        if (a == null || b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        }
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1415
        for (Class<?> x = a; x != null; x = x.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
            if (x == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
            if (b.isInterface()) {
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1420
                Class<?>[] interfaces = x.getInterfaces();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
                for (int i = 0; i < interfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
                    if (isSubclass(interfaces[i], b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * Return true iff the given method throws the given exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1434
    private boolean throwsException(Method method, Class<?> exception) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        Class exs[] = method.getExceptionTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        for (int i = 0; i < exs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (exs[i] == exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * Try to create an instance of a named class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * First try the classloader of "sibling", then try the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * classloader then the class loader of the current Thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     */
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1449
    static Object instantiate(Class<?> sibling, String className)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                 throws InstantiationException, IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                                                ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        // First check with sibling's classloader (if any).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        ClassLoader cl = sibling.getClassLoader();
11120
f8576c769572 7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents: 9035
diff changeset
  1454
        Class<?> cls = ClassFinder.findClass(className, cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        return cls.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
} // end class Introspector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
//===========================================================================
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
 * Package private implementation support class for Introspector's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
 * internal use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
 * Mostly this is used as a placeholder for the descriptors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
class GenericBeanInfo extends SimpleBeanInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    private BeanDescriptor beanDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    private EventSetDescriptor[] events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    private int defaultEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
    private PropertyDescriptor[] properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    private int defaultProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
    private MethodDescriptor[] methods;
13654
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1477
    private Reference<BeanInfo> targetBeanInfoRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    public GenericBeanInfo(BeanDescriptor beanDescriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                EventSetDescriptor[] events, int defaultEvent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                PropertyDescriptor[] properties, int defaultProperty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                MethodDescriptor[] methods, BeanInfo targetBeanInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
        this.beanDescriptor = beanDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        this.events = events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
        this.defaultEvent = defaultEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
        this.properties = properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
        this.defaultProperty = defaultProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        this.methods = methods;
13654
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1489
        this.targetBeanInfoRef = (targetBeanInfo != null)
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1490
                ? new SoftReference<>(targetBeanInfo)
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1491
                : null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * Package-private dup constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * This must isolate the new object from any changes to the old object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    GenericBeanInfo(GenericBeanInfo old) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        beanDescriptor = new BeanDescriptor(old.beanDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
        if (old.events != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
            int len = old.events.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            events = new EventSetDescriptor[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                events[i] = new EventSetDescriptor(old.events[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        defaultEvent = old.defaultEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        if (old.properties != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            int len = old.properties.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            properties = new PropertyDescriptor[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                PropertyDescriptor oldp = old.properties[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                if (oldp instanceof IndexedPropertyDescriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                    properties[i] = new IndexedPropertyDescriptor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                                        (IndexedPropertyDescriptor) oldp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                    properties[i] = new PropertyDescriptor(oldp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        defaultProperty = old.defaultProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        if (old.methods != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            int len = old.methods.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            methods = new MethodDescriptor[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                methods[i] = new MethodDescriptor(old.methods[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        }
4851
4f7de5eeeac7 6921057: REGRESSION: persistence delegate issue on Windows and Linux against 5.u23b03/6u17b11
malenkov
parents: 4393
diff changeset
  1530
        this.targetBeanInfoRef = old.targetBeanInfoRef;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
    public PropertyDescriptor[] getPropertyDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        return properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    public int getDefaultPropertyIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        return defaultProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    public EventSetDescriptor[] getEventSetDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
        return events;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
    public int getDefaultEventIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
        return defaultEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
    public MethodDescriptor[] getMethodDescriptors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
        return methods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    public BeanDescriptor getBeanDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        return beanDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    public java.awt.Image getIcon(int iconKind) {
13654
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1558
        BeanInfo targetBeanInfo = getTargetBeanInfo();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        if (targetBeanInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
            return targetBeanInfo.getIcon(iconKind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        return super.getIcon(iconKind);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    }
13654
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1564
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1565
    private BeanInfo getTargetBeanInfo() {
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1566
        if (this.targetBeanInfoRef == null) {
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1567
            return null;
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1568
        }
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1569
        BeanInfo targetBeanInfo = this.targetBeanInfoRef.get();
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1570
        if (targetBeanInfo == null) {
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1571
            targetBeanInfo = ThreadGroupContext.getContext().getBeanInfoFinder()
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1572
                    .find(this.beanDescriptor.getBeanClass());
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1573
            if (targetBeanInfo != null) {
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1574
                this.targetBeanInfoRef = new SoftReference<>(targetBeanInfo);
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1575
            }
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1576
        }
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1577
        return targetBeanInfo;
9a35ae08fd2b 7195106: REGRESSION : There is no way to get Icon inf, once Softreference is released
malenkov
parents: 13356
diff changeset
  1578
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
}