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