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                 if (igpd != null) {
 751                     gpd = mergePropertyWithIndexedProperty(gpd, igpd);
 752                 }
 753                 if (ispd != null) {
 754                     spd = mergePropertyWithIndexedProperty(spd, ispd);
 755                 }
 756                 // Complete simple properties set
 757                 if (gpd == spd) {
 758                     pd = gpd;
 759                 } else if (spd instanceof IndexedPropertyDescriptor) {
 760                     pd = mergePropertyWithIndexedProperty(gpd, (IndexedPropertyDescriptor) spd);
 761                 } else if (gpd instanceof IndexedPropertyDescriptor) {
 762                     pd = mergePropertyWithIndexedProperty(spd, (IndexedPropertyDescriptor) gpd);
 763                 } else {
 764                     pd = mergePropertyDescriptor(gpd, spd);
 765                 }
 766             } else if (ispd != null) {
 767                 // indexed setter
 768                 pd = ispd;
 769                 // Merge any classic property descriptors
 770                 if (spd != null) {
 771                     pd = mergePropertyDescriptor(ispd, spd);
 772                 }
 773                 if (gpd != null) {
 774                     pd = mergePropertyDescriptor(ispd, gpd);
 775                 }
 776             } else if (igpd != null) {
 777                 // indexed getter
 778                 pd = igpd;
 779                 // Merge any classic property descriptors
 780                 if (gpd != null) {
 781                     pd = mergePropertyDescriptor(igpd, gpd);
 782                 }
 783                 if (spd != null) {
 784                     pd = mergePropertyDescriptor(igpd, spd);
 785                 }
 786             } else if (spd != null) {
 787                 // simple setter
 788                 pd = spd;
 789             } else if (gpd != null) {
 790                 // simple getter
 791                 pd = gpd;
 792             }
 793 
 794             // Very special case to ensure that an IndexedPropertyDescriptor
 795             // doesn't contain less information than the enclosed
 796             // PropertyDescriptor. If it does, then recreate as a
 797             // PropertyDescriptor. See 4168833
 798             if (pd instanceof IndexedPropertyDescriptor) {
 799                 ipd = (IndexedPropertyDescriptor)pd;
 800                 if (ipd.getIndexedReadMethod() == null && ipd.getIndexedWriteMethod() == null) {
 801                     pd = new PropertyDescriptor(ipd);
 802                 }
 803             }
 804 
 805             // Find the first property descriptor
 806             // which does not have getter and setter methods.
 807             // See regression bug 4984912.
 808             if ( (pd == null) && (list.size() > 0) ) {
 809                 pd = list.get(0);
 810             }
 811 
 812             if (pd != null) {
 813                 properties.put(pd.getName(), pd);
 814             }
 815         }
 816     }
 817 
 818     private static boolean isAssignable(Class<?> current, Class<?> candidate) {
 819         return current == null ? candidate == null : current.isAssignableFrom(candidate);
 820     }
 821 
 822     private PropertyDescriptor mergePropertyWithIndexedProperty(PropertyDescriptor pd, IndexedPropertyDescriptor ipd) {
 823         Class<?> type = pd.getPropertyType();
 824         if (type.isArray() && (type.getComponentType() == ipd.getIndexedPropertyType())) {
 825             return pd.getClass0().isAssignableFrom(ipd.getClass0())
 826                     ? new IndexedPropertyDescriptor(pd, ipd)
 827                     : new IndexedPropertyDescriptor(ipd, pd);
 828         }
 829         return pd;
 830     }
 831 
 832     /**
 833      * Adds the property descriptor to the indexedproperty descriptor only if the
 834      * types are the same.
 835      *
 836      * The most specific property descriptor will take precedence.
 837      */
 838     private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd,
 839                                                        PropertyDescriptor pd) {
 840         PropertyDescriptor result = null;
 841 
 842         Class<?> propType = pd.getPropertyType();
 843         Class<?> ipropType = ipd.getIndexedPropertyType();
 844 
 845         if (propType.isArray() && propType.getComponentType() == ipropType) {
 846             if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
 847                 result = new IndexedPropertyDescriptor(pd, ipd);
 848             } else {
 849                 result = new IndexedPropertyDescriptor(ipd, pd);
 850             }
 851         } else {
 852             // Cannot merge the pd because of type mismatch
 853             // Return the most specific pd
 854             if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
 855                 result = ipd;
 856             } else {
 857                 result = pd;
 858                 // Try to add methods which may have been lost in the type change
 859                 // See 4168833
 860                 Method write = result.getWriteMethod();
 861                 Method read = result.getReadMethod();
 862 
 863                 if (read == null && write != null) {
 864                     read = findMethod(result.getClass0(),
 865                                       GET_PREFIX + NameGenerator.capitalize(result.getName()), 0);
 866                     if (read != null) {
 867                         try {
 868                             result.setReadMethod(read);
 869                         } catch (IntrospectionException ex) {
 870                             // no consequences for failure.
 871                         }
 872                     }
 873                 }
 874                 if (write == null && read != null) {
 875                     write = findMethod(result.getClass0(),
 876                                        SET_PREFIX + NameGenerator.capitalize(result.getName()), 1,
 877                                        new Class<?>[] { FeatureDescriptor.getReturnType(result.getClass0(), read) });
 878                     if (write != null) {
 879                         try {
 880                             result.setWriteMethod(write);
 881                         } catch (IntrospectionException ex) {
 882                             // no consequences for failure.
 883                         }
 884                     }
 885                 }
 886             }
 887         }
 888         return result;
 889     }
 890 
 891     // Handle regular pd merge
 892     private PropertyDescriptor mergePropertyDescriptor(PropertyDescriptor pd1,
 893                                                        PropertyDescriptor pd2) {
 894         if (pd1.getClass0().isAssignableFrom(pd2.getClass0())) {
 895             return new PropertyDescriptor(pd1, pd2);
 896         } else {
 897             return new PropertyDescriptor(pd2, pd1);
 898         }
 899     }
 900 
 901     // Handle regular ipd merge
 902     private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd1,
 903                                                        IndexedPropertyDescriptor ipd2) {
 904         if (ipd1.getClass0().isAssignableFrom(ipd2.getClass0())) {
 905             return new IndexedPropertyDescriptor(ipd1, ipd2);
 906         } else {
 907             return new IndexedPropertyDescriptor(ipd2, ipd1);
 908         }
 909     }
 910 
 911     /**
 912      * @return An array of EventSetDescriptors describing the kinds of
 913      * events fired by the target bean.
 914      */
 915     private EventSetDescriptor[] getTargetEventInfo() throws IntrospectionException {
 916         if (events == null) {
 917             events = new HashMap<>();
 918         }
 919 
 920         // Check if the bean has its own BeanInfo that will provide
 921         // explicit information.
 922         EventSetDescriptor[] explicitEvents = null;
 923         if (explicitBeanInfo != null) {
 924             explicitEvents = explicitBeanInfo.getEventSetDescriptors();
 925             int ix = explicitBeanInfo.getDefaultEventIndex();
 926             if (ix >= 0 && ix < explicitEvents.length) {
 927                 defaultEventName = explicitEvents[ix].getName();
 928             }
 929         }
 930 
 931         if (explicitEvents == null && superBeanInfo != null) {
 932             // We have no explicit BeanInfo events.  Check with our parent.
 933             EventSetDescriptor supers[] = superBeanInfo.getEventSetDescriptors();
 934             for (int i = 0 ; i < supers.length; i++) {
 935                 addEvent(supers[i]);
 936             }
 937             int ix = superBeanInfo.getDefaultEventIndex();
 938             if (ix >= 0 && ix < supers.length) {
 939                 defaultEventName = supers[ix].getName();
 940             }
 941         }
 942 
 943         for (int i = 0; i < additionalBeanInfo.length; i++) {
 944             EventSetDescriptor additional[] = additionalBeanInfo[i].getEventSetDescriptors();
 945             if (additional != null) {
 946                 for (int j = 0 ; j < additional.length; j++) {
 947                     addEvent(additional[j]);
 948                 }
 949             }
 950         }
 951 
 952         if (explicitEvents != null) {
 953             // Add the explicit explicitBeanInfo data to our results.
 954             for (int i = 0 ; i < explicitEvents.length; i++) {
 955                 addEvent(explicitEvents[i]);
 956             }
 957 
 958         } else {
 959 
 960             // Apply some reflection to the current class.
 961 
 962             // Get an array of all the public beans methods at this level
 963             Method methodList[] = getPublicDeclaredMethods(beanClass);
 964 
 965             // Find all suitable "add", "remove" and "get" Listener methods
 966             // The name of the listener type is the key for these hashtables
 967             // i.e, ActionListener
 968             Map<String, Method> adds = null;
 969             Map<String, Method> removes = null;
 970             Map<String, Method> gets = null;
 971 
 972             for (int i = 0; i < methodList.length; i++) {
 973                 Method method = methodList[i];
 974                 if (method == null) {
 975                     continue;
 976                 }
 977                 // skip static methods.
 978                 int mods = method.getModifiers();
 979                 if (Modifier.isStatic(mods)) {
 980                     continue;
 981                 }
 982                 String name = method.getName();
 983                 // Optimization avoid getParameterTypes
 984                 if (!name.startsWith(ADD_PREFIX) && !name.startsWith(REMOVE_PREFIX)
 985                     && !name.startsWith(GET_PREFIX)) {
 986                     continue;
 987                 }
 988 
 989                 if (name.startsWith(ADD_PREFIX)) {
 990                     Class<?> returnType = method.getReturnType();
 991                     if (returnType == void.class) {
 992                         Type[] parameterTypes = method.getGenericParameterTypes();
 993                         if (parameterTypes.length == 1) {
 994                             Class<?> type = TypeResolver.erase(TypeResolver.resolveInClass(beanClass, parameterTypes[0]));
 995                             if (Introspector.isSubclass(type, eventListenerType)) {
 996                                 String listenerName = name.substring(3);
 997                                 if (listenerName.length() > 0 &&
 998                                     type.getName().endsWith(listenerName)) {
 999                                     if (adds == null) {
1000                                         adds = new HashMap<>();
1001                                     }
1002                                     adds.put(listenerName, method);
1003                                 }
1004                             }
1005                         }
1006                     }
1007                 }
1008                 else if (name.startsWith(REMOVE_PREFIX)) {
1009                     Class<?> returnType = method.getReturnType();
1010                     if (returnType == void.class) {
1011                         Type[] parameterTypes = method.getGenericParameterTypes();
1012                         if (parameterTypes.length == 1) {
1013                             Class<?> type = TypeResolver.erase(TypeResolver.resolveInClass(beanClass, parameterTypes[0]));
1014                             if (Introspector.isSubclass(type, eventListenerType)) {
1015                                 String listenerName = name.substring(6);
1016                                 if (listenerName.length() > 0 &&
1017                                     type.getName().endsWith(listenerName)) {
1018                                     if (removes == null) {
1019                                         removes = new HashMap<>();
1020                                     }
1021                                     removes.put(listenerName, method);
1022                                 }
1023                             }
1024                         }
1025                     }
1026                 }
1027                 else if (name.startsWith(GET_PREFIX)) {
1028                     Class<?>[] parameterTypes = method.getParameterTypes();
1029                     if (parameterTypes.length == 0) {
1030                         Class<?> returnType = FeatureDescriptor.getReturnType(beanClass, method);
1031                         if (returnType.isArray()) {
1032                             Class<?> type = returnType.getComponentType();
1033                             if (Introspector.isSubclass(type, eventListenerType)) {
1034                                 String listenerName  = name.substring(3, name.length() - 1);
1035                                 if (listenerName.length() > 0 &&
1036                                     type.getName().endsWith(listenerName)) {
1037                                     if (gets == null) {
1038                                         gets = new HashMap<>();
1039                                     }
1040                                     gets.put(listenerName, method);
1041                                 }
1042                             }
1043                         }
1044                     }
1045                 }
1046             }
1047 
1048             if (adds != null && removes != null) {
1049                 // Now look for matching addFooListener+removeFooListener pairs.
1050                 // Bonus if there is a matching getFooListeners method as well.
1051                 Iterator<String> keys = adds.keySet().iterator();
1052                 while (keys.hasNext()) {
1053                     String listenerName = keys.next();
1054                     // Skip any "add" which doesn't have a matching "remove" or
1055                     // a listener name that doesn't end with Listener
1056                     if (removes.get(listenerName) == null || !listenerName.endsWith("Listener")) {
1057                         continue;
1058                     }
1059                     String eventName = decapitalize(listenerName.substring(0, listenerName.length()-8));
1060                     Method addMethod = adds.get(listenerName);
1061                     Method removeMethod = removes.get(listenerName);
1062                     Method getMethod = null;
1063                     if (gets != null) {
1064                         getMethod = gets.get(listenerName);
1065                     }
1066                     Class<?> argType = FeatureDescriptor.getParameterTypes(beanClass, addMethod)[0];
1067 
1068                     // generate a list of Method objects for each of the target methods:
1069                     Method allMethods[] = getPublicDeclaredMethods(argType);
1070                     List<Method> validMethods = new ArrayList<>(allMethods.length);
1071                     for (int i = 0; i < allMethods.length; i++) {
1072                         if (allMethods[i] == null) {
1073                             continue;
1074                         }
1075 
1076                         if (isEventHandler(allMethods[i])) {
1077                             validMethods.add(allMethods[i]);
1078                         }
1079                     }
1080                     Method[] methods = validMethods.toArray(new Method[validMethods.size()]);
1081 
1082                     EventSetDescriptor esd = new EventSetDescriptor(eventName, argType,
1083                                                                     methods, addMethod,
1084                                                                     removeMethod,
1085                                                                     getMethod);
1086 
1087                     // If the adder method throws the TooManyListenersException then it
1088                     // is a Unicast event source.
1089                     if (throwsException(addMethod,
1090                                         java.util.TooManyListenersException.class)) {
1091                         esd.setUnicast(true);
1092                     }
1093                     addEvent(esd);
1094                 }
1095             } // if (adds != null ...
1096         }
1097         EventSetDescriptor[] result;
1098         if (events.size() == 0) {
1099             result = EMPTY_EVENTSETDESCRIPTORS;
1100         } else {
1101             // Allocate and populate the result array.
1102             result = new EventSetDescriptor[events.size()];
1103             result = events.values().toArray(result);
1104 
1105             // Set the default index.
1106             if (defaultEventName != null) {
1107                 for (int i = 0; i < result.length; i++) {
1108                     if (defaultEventName.equals(result[i].getName())) {
1109                         defaultEventIndex = i;
1110                     }
1111                 }
1112             }
1113         }
1114         return result;
1115     }
1116 
1117     private void addEvent(EventSetDescriptor esd) {
1118         String key = esd.getName();
1119         if (esd.getName().equals("propertyChange")) {
1120             propertyChangeSource = true;
1121         }
1122         EventSetDescriptor old = events.get(key);
1123         if (old == null) {
1124             events.put(key, esd);
1125             return;
1126         }
1127         EventSetDescriptor composite = new EventSetDescriptor(old, esd);
1128         events.put(key, composite);
1129     }
1130 
1131     /**
1132      * @return An array of MethodDescriptors describing the private
1133      * methods supported by the target bean.
1134      */
1135     private MethodDescriptor[] getTargetMethodInfo() {
1136         if (methods == null) {
1137             methods = new HashMap<>(100);
1138         }
1139 
1140         // Check if the bean has its own BeanInfo that will provide
1141         // explicit information.
1142         MethodDescriptor[] explicitMethods = null;
1143         if (explicitBeanInfo != null) {
1144             explicitMethods = explicitBeanInfo.getMethodDescriptors();
1145         }
1146 
1147         if (explicitMethods == null && superBeanInfo != null) {
1148             // We have no explicit BeanInfo methods.  Check with our parent.
1149             MethodDescriptor supers[] = superBeanInfo.getMethodDescriptors();
1150             for (int i = 0 ; i < supers.length; i++) {
1151                 addMethod(supers[i]);
1152             }
1153         }
1154 
1155         for (int i = 0; i < additionalBeanInfo.length; i++) {
1156             MethodDescriptor additional[] = additionalBeanInfo[i].getMethodDescriptors();
1157             if (additional != null) {
1158                 for (int j = 0 ; j < additional.length; j++) {
1159                     addMethod(additional[j]);
1160                 }
1161             }
1162         }
1163 
1164         if (explicitMethods != null) {
1165             // Add the explicit explicitBeanInfo data to our results.
1166             for (int i = 0 ; i < explicitMethods.length; i++) {
1167                 addMethod(explicitMethods[i]);
1168             }
1169 
1170         } else {
1171 
1172             // Apply some reflection to the current class.
1173 
1174             // First get an array of all the beans methods at this level
1175             Method methodList[] = getPublicDeclaredMethods(beanClass);
1176 
1177             // Now analyze each method.
1178             for (int i = 0; i < methodList.length; i++) {
1179                 Method method = methodList[i];
1180                 if (method == null) {
1181                     continue;
1182                 }
1183                 MethodDescriptor md = new MethodDescriptor(method);
1184                 addMethod(md);
1185             }
1186         }
1187 
1188         // Allocate and populate the result array.
1189         MethodDescriptor result[] = new MethodDescriptor[methods.size()];
1190         result = methods.values().toArray(result);
1191 
1192         return result;
1193     }
1194 
1195     private void addMethod(MethodDescriptor md) {
1196         // We have to be careful here to distinguish method by both name
1197         // and argument lists.
1198         // This method gets called a *lot, so we try to be efficient.
1199         String name = md.getName();
1200 
1201         MethodDescriptor old = methods.get(name);
1202         if (old == null) {
1203             // This is the common case.
1204             methods.put(name, md);
1205             return;
1206         }
1207 
1208         // We have a collision on method names.  This is rare.
1209 
1210         // Check if old and md have the same type.
1211         String[] p1 = md.getParamNames();
1212         String[] p2 = old.getParamNames();
1213 
1214         boolean match = false;
1215         if (p1.length == p2.length) {
1216             match = true;
1217             for (int i = 0; i < p1.length; i++) {
1218                 if (p1[i] != p2[i]) {
1219                     match = false;
1220                     break;
1221                 }
1222             }
1223         }
1224         if (match) {
1225             MethodDescriptor composite = new MethodDescriptor(old, md);
1226             methods.put(name, composite);
1227             return;
1228         }
1229 
1230         // We have a collision on method names with different type signatures.
1231         // This is very rare.
1232 
1233         String longKey = makeQualifiedMethodName(name, p1);
1234         old = methods.get(longKey);
1235         if (old == null) {
1236             methods.put(longKey, md);
1237             return;
1238         }
1239         MethodDescriptor composite = new MethodDescriptor(old, md);
1240         methods.put(longKey, composite);
1241     }
1242 
1243     /**
1244      * Creates a key for a method in a method cache.
1245      */
1246     private static String makeQualifiedMethodName(String name, String[] params) {
1247         StringBuffer sb = new StringBuffer(name);
1248         sb.append('=');
1249         for (int i = 0; i < params.length; i++) {
1250             sb.append(':');
1251             sb.append(params[i]);
1252         }
1253         return sb.toString();
1254     }
1255 
1256     private int getTargetDefaultEventIndex() {
1257         return defaultEventIndex;
1258     }
1259 
1260     private int getTargetDefaultPropertyIndex() {
1261         return defaultPropertyIndex;
1262     }
1263 
1264     private BeanDescriptor getTargetBeanDescriptor() {
1265         // Use explicit info, if available,
1266         if (explicitBeanInfo != null) {
1267             BeanDescriptor bd = explicitBeanInfo.getBeanDescriptor();
1268             if (bd != null) {
1269                 return (bd);
1270             }
1271         }
1272         // OK, fabricate a default BeanDescriptor.
1273         return new BeanDescriptor(this.beanClass, findCustomizerClass(this.beanClass));
1274     }
1275 
1276     private static Class<?> findCustomizerClass(Class<?> type) {
1277         String name = type.getName() + "Customizer";
1278         try {
1279             type = ClassFinder.findClass(name, type.getClassLoader());
1280             // Each customizer should inherit java.awt.Component and implement java.beans.Customizer
1281             // according to the section 9.3 of JavaBeans&trade; specification
1282             if (Component.class.isAssignableFrom(type) && Customizer.class.isAssignableFrom(type)) {
1283                 return type;
1284             }
1285         }
1286         catch (Exception exception) {
1287             // ignore any exceptions
1288         }
1289         return null;
1290     }
1291 
1292     private boolean isEventHandler(Method m) {
1293         // We assume that a method is an event handler if it has a single
1294         // argument, whose type inherit from java.util.Event.
1295         Type argTypes[] = m.getGenericParameterTypes();
1296         if (argTypes.length != 1) {
1297             return false;
1298         }
1299         return isSubclass(TypeResolver.erase(TypeResolver.resolveInClass(beanClass, argTypes[0])), EventObject.class);
1300     }
1301 
1302     /*
1303      * Internal method to return *public* methods within a class.
1304      */
1305     private static Method[] getPublicDeclaredMethods(Class<?> clz) {
1306         // Looking up Class.getDeclaredMethods is relatively expensive,
1307         // so we cache the results.
1308         if (!ReflectUtil.isPackageAccessible(clz)) {
1309             return new Method[0];
1310         }
1311         synchronized (declaredMethodCache) {
1312             Method[] result = declaredMethodCache.get(clz);
1313             if (result == null) {
1314                 result = clz.getMethods();
1315                 for (int i = 0; i < result.length; i++) {
1316                     Method method = result[i];
1317                     if (!method.getDeclaringClass().equals(clz)) {
1318                         result[i] = null; // ignore methods declared elsewhere
1319                     }
1320                     else {
1321                         try {
1322                             method = MethodFinder.findAccessibleMethod(method);
1323                             Class<?> type = method.getDeclaringClass();
1324                             result[i] = type.equals(clz) || type.isInterface()
1325                                     ? method
1326                                     : null; // ignore methods from superclasses
1327                         }
1328                         catch (NoSuchMethodException exception) {
1329                             // commented out because of 6976577
1330                             // result[i] = null; // ignore inaccessible methods
1331                         }
1332                     }
1333                 }
1334                 declaredMethodCache.put(clz, result);
1335             }
1336             return result;
1337         }
1338     }
1339 
1340     //======================================================================
1341     // Package private support methods.
1342     //======================================================================
1343 
1344     /**
1345      * Internal support for finding a target methodName with a given
1346      * parameter list on a given class.
1347      */
1348     private static Method internalFindMethod(Class<?> start, String methodName,
1349                                                  int argCount, Class args[]) {
1350         // For overriden methods we need to find the most derived version.
1351         // So we start with the given class and walk up the superclass chain.
1352 
1353         Method method = null;
1354 
1355         for (Class<?> cl = start; cl != null; cl = cl.getSuperclass()) {
1356             Method methods[] = getPublicDeclaredMethods(cl);
1357             for (int i = 0; i < methods.length; i++) {
1358                 method = methods[i];
1359                 if (method == null) {
1360                     continue;
1361                 }
1362 
1363                 // make sure method signature matches.
1364                 if (method.getName().equals(methodName)) {
1365                     Type[] params = method.getGenericParameterTypes();
1366                     if (params.length == argCount) {
1367                         if (args != null) {
1368                             boolean different = false;
1369                             if (argCount > 0) {
1370                                 for (int j = 0; j < argCount; j++) {
1371                                     if (TypeResolver.erase(TypeResolver.resolveInClass(start, params[j])) != args[j]) {
1372                                         different = true;
1373                                         continue;
1374                                     }
1375                                 }
1376                                 if (different) {
1377                                     continue;
1378                                 }
1379                             }
1380                         }
1381                         return method;
1382                     }
1383                 }
1384             }
1385         }
1386         method = null;
1387 
1388         // Now check any inherited interfaces.  This is necessary both when
1389         // the argument class is itself an interface, and when the argument
1390         // class is an abstract class.
1391         Class ifcs[] = start.getInterfaces();
1392         for (int i = 0 ; i < ifcs.length; i++) {
1393             // Note: The original implementation had both methods calling
1394             // the 3 arg method. This is preserved but perhaps it should
1395             // pass the args array instead of null.
1396             method = internalFindMethod(ifcs[i], methodName, argCount, null);
1397             if (method != null) {
1398                 break;
1399             }
1400         }
1401         return method;
1402     }
1403 
1404     /**
1405      * Find a target methodName on a given class.
1406      */
1407     static Method findMethod(Class<?> cls, String methodName, int argCount) {
1408         return findMethod(cls, methodName, argCount, null);
1409     }
1410 
1411     /**
1412      * Find a target methodName with specific parameter list on a given class.
1413      * <p>
1414      * Used in the contructors of the EventSetDescriptor,
1415      * PropertyDescriptor and the IndexedPropertyDescriptor.
1416      * <p>
1417      * @param cls The Class object on which to retrieve the method.
1418      * @param methodName Name of the method.
1419      * @param argCount Number of arguments for the desired method.
1420      * @param args Array of argument types for the method.
1421      * @return the method or null if not found
1422      */
1423     static Method findMethod(Class<?> cls, String methodName, int argCount,
1424                              Class args[]) {
1425         if (methodName == null) {
1426             return null;
1427         }
1428         return internalFindMethod(cls, methodName, argCount, args);
1429     }
1430 
1431     /**
1432      * Return true if class a is either equivalent to class b, or
1433      * if class a is a subclass of class b, i.e. if a either "extends"
1434      * or "implements" b.
1435      * Note tht either or both "Class" objects may represent interfaces.
1436      */
1437     static  boolean isSubclass(Class<?> a, Class<?> b) {
1438         // We rely on the fact that for any given java class or
1439         // primtitive type there is a unqiue Class object, so
1440         // we can use object equivalence in the comparisons.
1441         if (a == b) {
1442             return true;
1443         }
1444         if (a == null || b == null) {
1445             return false;
1446         }
1447         for (Class<?> x = a; x != null; x = x.getSuperclass()) {
1448             if (x == b) {
1449                 return true;
1450             }
1451             if (b.isInterface()) {
1452                 Class<?>[] interfaces = x.getInterfaces();
1453                 for (int i = 0; i < interfaces.length; i++) {
1454                     if (isSubclass(interfaces[i], b)) {
1455                         return true;
1456                     }
1457                 }
1458             }
1459         }
1460         return false;
1461     }
1462 
1463     /**
1464      * Return true iff the given method throws the given exception.
1465      */
1466     private boolean throwsException(Method method, Class<?> exception) {
1467         Class exs[] = method.getExceptionTypes();
1468         for (int i = 0; i < exs.length; i++) {
1469             if (exs[i] == exception) {
1470                 return true;
1471             }
1472         }
1473         return false;
1474     }
1475 
1476     /**
1477      * Try to create an instance of a named class.
1478      * First try the classloader of "sibling", then try the system
1479      * classloader then the class loader of the current Thread.
1480      */
1481     static Object instantiate(Class<?> sibling, String className)
1482                  throws InstantiationException, IllegalAccessException,
1483                                                 ClassNotFoundException {
1484         // First check with sibling's classloader (if any).
1485         ClassLoader cl = sibling.getClassLoader();
1486         Class<?> cls = ClassFinder.findClass(className, cl);
1487         return cls.newInstance();
1488     }
1489 
1490 } // end class Introspector
1491 
1492 //===========================================================================
1493 
1494 /**
1495  * Package private implementation support class for Introspector's
1496  * internal use.
1497  * <p>
1498  * Mostly this is used as a placeholder for the descriptors.
1499  */
1500 
1501 class GenericBeanInfo extends SimpleBeanInfo {
1502 
1503     private BeanDescriptor beanDescriptor;
1504     private EventSetDescriptor[] events;
1505     private int defaultEvent;
1506     private PropertyDescriptor[] properties;
1507     private int defaultProperty;
1508     private MethodDescriptor[] methods;
1509     private Reference<BeanInfo> targetBeanInfoRef;
1510 
1511     public GenericBeanInfo(BeanDescriptor beanDescriptor,
1512                 EventSetDescriptor[] events, int defaultEvent,
1513                 PropertyDescriptor[] properties, int defaultProperty,
1514                 MethodDescriptor[] methods, BeanInfo targetBeanInfo) {
1515         this.beanDescriptor = beanDescriptor;
1516         this.events = events;
1517         this.defaultEvent = defaultEvent;
1518         this.properties = properties;
1519         this.defaultProperty = defaultProperty;
1520         this.methods = methods;
1521         this.targetBeanInfoRef = (targetBeanInfo != null)
1522                 ? new SoftReference<>(targetBeanInfo)
1523                 : null;
1524     }
1525 
1526     /**
1527      * Package-private dup constructor
1528      * This must isolate the new object from any changes to the old object.
1529      */
1530     GenericBeanInfo(GenericBeanInfo old) {
1531 
1532         beanDescriptor = new BeanDescriptor(old.beanDescriptor);
1533         if (old.events != null) {
1534             int len = old.events.length;
1535             events = new EventSetDescriptor[len];
1536             for (int i = 0; i < len; i++) {
1537                 events[i] = new EventSetDescriptor(old.events[i]);
1538             }
1539         }
1540         defaultEvent = old.defaultEvent;
1541         if (old.properties != null) {
1542             int len = old.properties.length;
1543             properties = new PropertyDescriptor[len];
1544             for (int i = 0; i < len; i++) {
1545                 PropertyDescriptor oldp = old.properties[i];
1546                 if (oldp instanceof IndexedPropertyDescriptor) {
1547                     properties[i] = new IndexedPropertyDescriptor(
1548                                         (IndexedPropertyDescriptor) oldp);
1549                 } else {
1550                     properties[i] = new PropertyDescriptor(oldp);
1551                 }
1552             }
1553         }
1554         defaultProperty = old.defaultProperty;
1555         if (old.methods != null) {
1556             int len = old.methods.length;
1557             methods = new MethodDescriptor[len];
1558             for (int i = 0; i < len; i++) {
1559                 methods[i] = new MethodDescriptor(old.methods[i]);
1560             }
1561         }
1562         this.targetBeanInfoRef = old.targetBeanInfoRef;
1563     }
1564 
1565     public PropertyDescriptor[] getPropertyDescriptors() {
1566         return properties;
1567     }
1568 
1569     public int getDefaultPropertyIndex() {
1570         return defaultProperty;
1571     }
1572 
1573     public EventSetDescriptor[] getEventSetDescriptors() {
1574         return events;
1575     }
1576 
1577     public int getDefaultEventIndex() {
1578         return defaultEvent;
1579     }
1580 
1581     public MethodDescriptor[] getMethodDescriptors() {
1582         return methods;
1583     }
1584 
1585     public BeanDescriptor getBeanDescriptor() {
1586         return beanDescriptor;
1587     }
1588 
1589     public java.awt.Image getIcon(int iconKind) {
1590         BeanInfo targetBeanInfo = getTargetBeanInfo();
1591         if (targetBeanInfo != null) {
1592             return targetBeanInfo.getIcon(iconKind);
1593         }
1594         return super.getIcon(iconKind);
1595     }
1596 
1597     private BeanInfo getTargetBeanInfo() {
1598         if (this.targetBeanInfoRef == null) {
1599             return null;
1600         }
1601         BeanInfo targetBeanInfo = this.targetBeanInfoRef.get();
1602         if (targetBeanInfo == null) {
1603             targetBeanInfo = ThreadGroupContext.getContext().getBeanInfoFinder()
1604                     .find(this.beanDescriptor.getBeanClass());
1605             if (targetBeanInfo != null) {
1606                 this.targetBeanInfoRef = new SoftReference<>(targetBeanInfo);
1607             }
1608         }
1609         return targetBeanInfo;
1610     }
1611 }