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