-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathIntrospector.java
1397 lines (1272 loc) · 54.9 KB
/
Introspector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.beans;
import java.awt.Component;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import com.sun.beans.TypeResolver;
import com.sun.beans.finder.ClassFinder;
import com.sun.beans.introspect.ClassInfo;
import com.sun.beans.introspect.EventSetInfo;
import com.sun.beans.introspect.PropertyInfo;
import jdk.internal.access.JavaBeansAccess;
import jdk.internal.access.SharedSecrets;
import sun.reflect.misc.ReflectUtil;
/**
* The Introspector class provides a standard way for tools to learn about
* the properties, events, and methods supported by a target Java Bean.
* <p>
* For each of those three kinds of information, the Introspector will
* separately analyze the bean's class and superclasses looking for
* either explicit or implicit information and use that information to
* build a BeanInfo object that comprehensively describes the target bean.
* <p>
* For each class "Foo", explicit information may be available if there exists
* a corresponding "FooBeanInfo" class that provides a non-null value when
* queried for the information. We first look for the BeanInfo class by
* taking the full package-qualified name of the target bean class and
* appending "BeanInfo" to form a new class name. If this fails, then
* we take the final classname component of this name, and look for that
* class in each of the packages specified in the BeanInfo package search
* path.
* <p>
* Thus for a class such as "sun.xyz.OurButton" we would first look for a
* BeanInfo class called "sun.xyz.OurButtonBeanInfo" and if that failed we'd
* look in each package in the BeanInfo search path for an OurButtonBeanInfo
* class. With the default search path, this would mean looking for
* "sun.beans.infos.OurButtonBeanInfo".
* <p>
* If a class provides explicit BeanInfo about itself then we add that to
* the BeanInfo information we obtained from analyzing any derived classes,
* but we regard the explicit information as being definitive for the current
* class and its base classes, and do not proceed any further up the superclass
* chain.
* <p>
* If we don't find explicit BeanInfo on a class, we use low-level
* reflection to study the methods of the class and apply standard design
* patterns to identify property accessors, event sources, or public
* methods. We then proceed to analyze the class's superclass and add
* in the information from it (and possibly on up the superclass chain).
* <p>
* For more information about introspection and design patterns, please
* consult the
* <a href="http://www.oracle.com/technetwork/java/javase/documentation/spec-136004.html">JavaBeans specification</a>.
*
* @since 1.1
*/
public class Introspector {
// Flags that can be used to control getBeanInfo:
/**
* Flag to indicate to use of all beaninfo.
* @since 1.2
*/
public static final int USE_ALL_BEANINFO = 1;
/**
* Flag to indicate to ignore immediate beaninfo.
* @since 1.2
*/
public static final int IGNORE_IMMEDIATE_BEANINFO = 2;
/**
* Flag to indicate to ignore all beaninfo.
* @since 1.2
*/
public static final int IGNORE_ALL_BEANINFO = 3;
private Class<?> beanClass;
private BeanInfo explicitBeanInfo;
private BeanInfo superBeanInfo;
private BeanInfo[] additionalBeanInfo;
private boolean propertyChangeSource = false;
// These should be removed.
private String defaultEventName;
private String defaultPropertyName;
private int defaultEventIndex = -1;
private int defaultPropertyIndex = -1;
// Methods maps from Method names to MethodDescriptors
private Map<String, MethodDescriptor> methods;
// properties maps from String names to PropertyDescriptors
private Map<String, PropertyDescriptor> properties;
// events maps from String names to EventSetDescriptors
private Map<String, EventSetDescriptor> events;
private static final EventSetDescriptor[] EMPTY_EVENTSETDESCRIPTORS = new EventSetDescriptor[0];
static final String ADD_PREFIX = "add";
static final String REMOVE_PREFIX = "remove";
static final String GET_PREFIX = "get";
static final String SET_PREFIX = "set";
static final String IS_PREFIX = "is";
// register with SharedSecrets for JMX usage
static {
SharedSecrets.setJavaBeansAccess(new JavaBeansAccess() {
@Override
public Method getReadMethod(Class<?> clazz, String property) throws Exception {
BeanInfo bi = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] pds = bi.getPropertyDescriptors();
for (PropertyDescriptor pd: pds) {
if (pd.getName().equals(property)) {
return pd.getReadMethod();
}
}
return null;
}
@Override
public String[] getConstructorPropertiesValue(Constructor<?> ctr) {
ConstructorProperties cp = ctr.getAnnotation(ConstructorProperties.class);
String [] ret = cp != null ? cp.value() : null;
return ret;
}
});
}
//======================================================================
// Public methods
//======================================================================
/**
* Introspect on a Java Bean and learn about all its properties, exposed
* methods, and events.
* <p>
* If the BeanInfo class for a Java Bean has been previously Introspected
* then the BeanInfo class is retrieved from the BeanInfo cache.
*
* @param beanClass The bean class to be analyzed.
* @return A BeanInfo object describing the target bean.
* @throws IntrospectionException if an exception occurs during
* introspection.
* @see #flushCaches
* @see #flushFromCaches
*/
public static BeanInfo getBeanInfo(Class<?> beanClass)
throws IntrospectionException
{
if (!ReflectUtil.isPackageAccessible(beanClass)) {
return (new Introspector(beanClass, null, USE_ALL_BEANINFO)).getBeanInfo();
}
ThreadGroupContext context = ThreadGroupContext.getContext();
BeanInfo beanInfo = context.getBeanInfo(beanClass);
if (beanInfo == null) {
beanInfo = new Introspector(beanClass, null, USE_ALL_BEANINFO).getBeanInfo();
context.putBeanInfo(beanClass, beanInfo);
}
return beanInfo;
}
/**
* Introspect on a Java bean and learn about all its properties, exposed
* methods, and events, subject to some control flags.
* <p>
* If the BeanInfo class for a Java Bean has been previously Introspected
* based on the same arguments then the BeanInfo class is retrieved
* from the BeanInfo cache.
*
* @param beanClass The bean class to be analyzed.
* @param flags Flags to control the introspection.
* If flags == USE_ALL_BEANINFO then we use all of the BeanInfo
* classes we can discover.
* If flags == IGNORE_IMMEDIATE_BEANINFO then we ignore any
* BeanInfo associated with the specified beanClass.
* If flags == IGNORE_ALL_BEANINFO then we ignore all BeanInfo
* associated with the specified beanClass or any of its
* parent classes.
* @return A BeanInfo object describing the target bean.
* @throws IntrospectionException if an exception occurs during
* introspection.
* @since 1.2
*/
public static BeanInfo getBeanInfo(Class<?> beanClass, int flags)
throws IntrospectionException {
return getBeanInfo(beanClass, null, flags);
}
/**
* Introspect on a Java bean and learn all about its properties, exposed
* methods, below a given "stop" point.
* <p>
* If the BeanInfo class for a Java Bean has been previously Introspected
* based on the same arguments, then the BeanInfo class is retrieved
* from the BeanInfo cache.
* @return the BeanInfo for the bean
* @param beanClass The bean class to be analyzed.
* @param stopClass The baseclass at which to stop the analysis. Any
* methods/properties/events in the stopClass or in its baseclasses
* will be ignored in the analysis.
* @throws IntrospectionException if an exception occurs during
* introspection.
*/
public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass)
throws IntrospectionException {
return getBeanInfo(beanClass, stopClass, USE_ALL_BEANINFO);
}
/**
* Introspect on a Java Bean and learn about all its properties,
* exposed methods and events, below a given {@code stopClass} point
* subject to some control {@code flags}.
* <dl>
* <dt>USE_ALL_BEANINFO</dt>
* <dd>Any BeanInfo that can be discovered will be used.</dd>
* <dt>IGNORE_IMMEDIATE_BEANINFO</dt>
* <dd>Any BeanInfo associated with the specified {@code beanClass} will be ignored.</dd>
* <dt>IGNORE_ALL_BEANINFO</dt>
* <dd>Any BeanInfo associated with the specified {@code beanClass}
* or any of its parent classes will be ignored.</dd>
* </dl>
* Any methods/properties/events in the {@code stopClass}
* or in its parent classes will be ignored in the analysis.
* <p>
* If the BeanInfo class for a Java Bean has been
* previously introspected based on the same arguments then
* the BeanInfo class is retrieved from the BeanInfo cache.
*
* @param beanClass the bean class to be analyzed
* @param stopClass the parent class at which to stop the analysis
* @param flags flags to control the introspection
* @return a BeanInfo object describing the target bean
* @throws IntrospectionException if an exception occurs during introspection
*
* @since 1.7
*/
public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass,
int flags) throws IntrospectionException {
BeanInfo bi;
if (stopClass == null && flags == USE_ALL_BEANINFO) {
// Same parameters to take advantage of caching.
bi = getBeanInfo(beanClass);
} else {
bi = (new Introspector(beanClass, stopClass, flags)).getBeanInfo();
}
return bi;
// Old behaviour: Make an independent copy of the BeanInfo.
//return new GenericBeanInfo(bi);
}
/**
* Utility method to take a string and convert it to normal Java variable
* name capitalization. This normally means converting the first
* character from upper case to lower case, but in the (unusual) special
* case when there is more than one character and both the first and
* second characters are upper case, we leave it alone.
* <p>
* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays
* as "URL".
*
* @param name The string to be decapitalized.
* @return The decapitalized version of the string.
*/
public static String decapitalize(String name) {
if (name == null || name.length() == 0) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
Character.isUpperCase(name.charAt(0))){
return name;
}
char[] chars = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
/**
* Gets the list of package names that will be used for
* finding BeanInfo classes.
*
* @return The array of package names that will be searched in
* order to find BeanInfo classes. The default value
* for this array is implementation-dependent; e.g.
* Sun implementation initially sets to {"sun.beans.infos"}.
*/
public static String[] getBeanInfoSearchPath() {
return ThreadGroupContext.getContext().getBeanInfoFinder().getPackages();
}
/**
* Change the list of package names that will be used for
* finding BeanInfo classes. The behaviour of
* this method is undefined if parameter path
* is null.
*
* <p>First, if there is a security manager, its {@code checkPropertiesAccess}
* method is called. This could result in a SecurityException.
*
* @param path Array of package names.
* @throws SecurityException if a security manager exists and its
* {@code checkPropertiesAccess} method doesn't allow setting
* of system properties.
* @see SecurityManager#checkPropertiesAccess
*/
public static void setBeanInfoSearchPath(String[] path) {
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
ThreadGroupContext.getContext().getBeanInfoFinder().setPackages(path);
}
/**
* Flush all of the Introspector's internal caches. This method is
* not normally required. It is normally only needed by advanced
* tools that update existing "Class" objects in-place and need
* to make the Introspector re-analyze existing Class objects.
*
* @since 1.2
*/
public static void flushCaches() {
ThreadGroupContext.getContext().clearBeanInfoCache();
ClassInfo.clear();
}
/**
* Flush the Introspector's internal cached information for a given class.
* This method is not normally required. It is normally only needed
* by advanced tools that update existing "Class" objects in-place
* and need to make the Introspector re-analyze an existing Class object.
*
* Note that only the direct state associated with the target Class
* object is flushed. We do not flush state for other Class objects
* with the same name, nor do we flush state for any related Class
* objects (such as subclasses), even though their state may include
* information indirectly obtained from the target Class object.
*
* @param clz Class object to be flushed.
* @throws NullPointerException If the Class object is null.
* @since 1.2
*/
public static void flushFromCaches(Class<?> clz) {
if (clz == null) {
throw new NullPointerException();
}
ThreadGroupContext.getContext().removeBeanInfo(clz);
ClassInfo.remove(clz);
}
//======================================================================
// Private implementation methods
//======================================================================
private Introspector(Class<?> beanClass, Class<?> stopClass, int flags)
throws IntrospectionException {
this.beanClass = beanClass;
// Check stopClass is a superClass of startClass.
if (stopClass != null) {
boolean isSuper = false;
for (Class<?> c = beanClass.getSuperclass(); c != null; c = c.getSuperclass()) {
if (c == stopClass) {
isSuper = true;
}
}
if (!isSuper) {
throw new IntrospectionException(stopClass.getName() + " not superclass of " +
beanClass.getName());
}
}
if (flags == USE_ALL_BEANINFO) {
explicitBeanInfo = findExplicitBeanInfo(beanClass);
}
Class<?> superClass = beanClass.getSuperclass();
if (superClass != stopClass) {
int newFlags = flags;
if (newFlags == IGNORE_IMMEDIATE_BEANINFO) {
newFlags = USE_ALL_BEANINFO;
}
superBeanInfo = getBeanInfo(superClass, stopClass, newFlags);
}
if (explicitBeanInfo != null) {
additionalBeanInfo = explicitBeanInfo.getAdditionalBeanInfo();
}
if (additionalBeanInfo == null) {
additionalBeanInfo = new BeanInfo[0];
}
}
/**
* Constructs a GenericBeanInfo class from the state of the Introspector
*/
private BeanInfo getBeanInfo() throws IntrospectionException {
// the evaluation order here is import, as we evaluate the
// event sets and locate PropertyChangeListeners before we
// look for properties.
BeanDescriptor bd = getTargetBeanDescriptor();
MethodDescriptor[] mds = getTargetMethodInfo();
EventSetDescriptor[] esds = getTargetEventInfo();
PropertyDescriptor[] pds = getTargetPropertyInfo();
int defaultEvent = getTargetDefaultEventIndex();
int defaultProperty = getTargetDefaultPropertyIndex();
return new GenericBeanInfo(bd, esds, defaultEvent, pds,
defaultProperty, mds, explicitBeanInfo);
}
/**
* Looks for an explicit BeanInfo class that corresponds to the Class.
* First it looks in the existing package that the Class is defined in,
* then it checks to see if the class is its own BeanInfo. Finally,
* the BeanInfo search path is prepended to the class and searched.
*
* @param beanClass the class type of the bean
* @return Instance of an explicit BeanInfo class or null if one isn't found.
*/
private static BeanInfo findExplicitBeanInfo(Class<?> beanClass) {
return ThreadGroupContext.getContext().getBeanInfoFinder().find(beanClass);
}
/**
* @return An array of PropertyDescriptors describing the editable
* properties supported by the target bean.
*/
private PropertyDescriptor[] getTargetPropertyInfo() {
// Check if the bean has its own BeanInfo that will provide
// explicit information.
PropertyDescriptor[] explicitProperties = null;
if (explicitBeanInfo != null) {
explicitProperties = getPropertyDescriptors(this.explicitBeanInfo);
}
if (explicitProperties == null && superBeanInfo != null) {
// We have no explicit BeanInfo properties. Check with our parent.
addPropertyDescriptors(getPropertyDescriptors(this.superBeanInfo));
}
for (int i = 0; i < additionalBeanInfo.length; i++) {
addPropertyDescriptors(additionalBeanInfo[i].getPropertyDescriptors());
}
if (explicitProperties != null) {
// Add the explicit BeanInfo data to our results.
addPropertyDescriptors(explicitProperties);
} else {
// Apply some reflection to the current class.
for (Map.Entry<String,PropertyInfo> entry : ClassInfo.get(this.beanClass).getProperties().entrySet()) {
addPropertyDescriptor(null != entry.getValue().getIndexed()
? new IndexedPropertyDescriptor(entry, this.propertyChangeSource)
: new PropertyDescriptor(entry, this.propertyChangeSource));
}
JavaBean annotation = this.beanClass.getAnnotation(JavaBean.class);
if ((annotation != null) && !annotation.defaultProperty().isEmpty()) {
this.defaultPropertyName = annotation.defaultProperty();
}
}
processPropertyDescriptors();
// Allocate and populate the result array.
PropertyDescriptor[] result =
properties.values().toArray(new PropertyDescriptor[properties.size()]);
// Set the default index.
if (defaultPropertyName != null) {
for (int i = 0; i < result.length; i++) {
if (defaultPropertyName.equals(result[i].getName())) {
defaultPropertyIndex = i;
}
}
}
return result;
}
private HashMap<String, List<PropertyDescriptor>> pdStore = new HashMap<>();
/**
* Adds the property descriptor to the list store.
*/
private void addPropertyDescriptor(PropertyDescriptor pd) {
String propName = pd.getName();
List<PropertyDescriptor> list = pdStore.get(propName);
if (list == null) {
list = new ArrayList<>();
pdStore.put(propName, list);
}
if (this.beanClass != pd.getClass0()) {
// replace existing property descriptor
// only if we have types to resolve
// in the context of this.beanClass
Method read = pd.getReadMethod();
Method write = pd.getWriteMethod();
boolean cls = true;
if (read != null) cls = cls && read.getGenericReturnType() instanceof Class;
if (write != null) cls = cls && write.getGenericParameterTypes()[0] instanceof Class;
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
Method readI = ipd.getIndexedReadMethod();
Method writeI = ipd.getIndexedWriteMethod();
if (readI != null) cls = cls && readI.getGenericReturnType() instanceof Class;
if (writeI != null) cls = cls && writeI.getGenericParameterTypes()[1] instanceof Class;
if (!cls) {
pd = new IndexedPropertyDescriptor(ipd);
pd.updateGenericsFor(this.beanClass);
}
}
else if (!cls) {
pd = new PropertyDescriptor(pd);
pd.updateGenericsFor(this.beanClass);
}
}
list.add(pd);
}
private void addPropertyDescriptors(PropertyDescriptor[] descriptors) {
if (descriptors != null) {
for (PropertyDescriptor descriptor : descriptors) {
addPropertyDescriptor(descriptor);
}
}
}
private PropertyDescriptor[] getPropertyDescriptors(BeanInfo info) {
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
int index = info.getDefaultPropertyIndex();
if ((0 <= index) && (index < descriptors.length)) {
this.defaultPropertyName = descriptors[index].getName();
}
return descriptors;
}
/**
* Populates the property descriptor table by merging the
* lists of Property descriptors.
*/
private void processPropertyDescriptors() {
if (properties == null) {
properties = new TreeMap<>();
}
List<PropertyDescriptor> list;
PropertyDescriptor pd, gpd, spd;
IndexedPropertyDescriptor ipd, igpd, ispd;
for (List<PropertyDescriptor> propertyDescriptors : pdStore.values()) {
pd = null; gpd = null; spd = null;
ipd = null; igpd = null; ispd = null;
list = propertyDescriptors;
// First pass. Find the latest getter method. Merge properties
// of previous getter methods.
for (int i = 0; i < list.size(); i++) {
pd = list.get(i);
if (pd instanceof IndexedPropertyDescriptor) {
ipd = (IndexedPropertyDescriptor)pd;
if (ipd.getIndexedReadMethod() != null) {
if (igpd != null) {
igpd = new IndexedPropertyDescriptor(igpd, ipd);
} else {
igpd = ipd;
}
}
} else {
if (pd.getReadMethod() != null) {
String pdName = pd.getReadMethod().getName();
if (gpd != null) {
// Don't replace the existing read
// method if it starts with "is"
String gpdName = gpd.getReadMethod().getName();
if (gpdName.equals(pdName) || !gpdName.startsWith(IS_PREFIX)) {
gpd = new PropertyDescriptor(gpd, pd);
}
} else {
gpd = pd;
}
}
}
}
// Second pass. Find the latest setter method which
// has the same type as the getter method.
for (int i = 0; i < list.size(); i++) {
pd = list.get(i);
if (pd instanceof IndexedPropertyDescriptor) {
ipd = (IndexedPropertyDescriptor)pd;
if (ipd.getIndexedWriteMethod() != null) {
if (igpd != null) {
if (isAssignable(igpd.getIndexedPropertyType(), ipd.getIndexedPropertyType())) {
if (ispd != null) {
ispd = new IndexedPropertyDescriptor(ispd, ipd);
} else {
ispd = ipd;
}
}
} else {
if (ispd != null) {
ispd = new IndexedPropertyDescriptor(ispd, ipd);
} else {
ispd = ipd;
}
}
}
} else {
if (pd.getWriteMethod() != null) {
if (gpd != null) {
if (isAssignable(gpd.getPropertyType(), pd.getPropertyType())) {
if (spd != null) {
spd = new PropertyDescriptor(spd, pd);
} else {
spd = pd;
}
}
} else {
if (spd != null) {
spd = new PropertyDescriptor(spd, pd);
} else {
spd = pd;
}
}
}
}
}
// At this stage we should have either PDs or IPDs for the
// representative getters and setters. The order at which the
// property descriptors are determined represent the
// precedence of the property ordering.
pd = null; ipd = null;
if (igpd != null && ispd != null) {
// Complete indexed properties set
// Merge any classic property descriptors
if ((gpd == spd) || (gpd == null)) {
pd = spd;
} else if (spd == null) {
pd = gpd;
} else if (spd instanceof IndexedPropertyDescriptor) {
pd = mergePropertyWithIndexedProperty(gpd, (IndexedPropertyDescriptor) spd);
} else if (gpd instanceof IndexedPropertyDescriptor) {
pd = mergePropertyWithIndexedProperty(spd, (IndexedPropertyDescriptor) gpd);
} else {
pd = mergePropertyDescriptor(gpd, spd);
}
if (igpd == ispd) {
ipd = igpd;
} else {
ipd = mergePropertyDescriptor(igpd, ispd);
}
if (pd == null) {
pd = ipd;
} else {
Class<?> propType = pd.getPropertyType();
Class<?> ipropType = ipd.getIndexedPropertyType();
if (propType.isArray() && propType.getComponentType() == ipropType) {
pd = pd.getClass0().isAssignableFrom(ipd.getClass0())
? new IndexedPropertyDescriptor(pd, ipd)
: new IndexedPropertyDescriptor(ipd, pd);
} else if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
pd = pd.getClass0().isAssignableFrom(ipd.getClass0())
? new PropertyDescriptor(pd, ipd)
: new PropertyDescriptor(ipd, pd);
} else {
pd = ipd;
}
}
} else if (gpd != null && spd != null) {
if (igpd != null) {
gpd = mergePropertyWithIndexedProperty(gpd, igpd);
}
if (ispd != null) {
spd = mergePropertyWithIndexedProperty(spd, ispd);
}
// Complete simple properties set
if (gpd == spd) {
pd = gpd;
} else if (spd instanceof IndexedPropertyDescriptor) {
pd = mergePropertyWithIndexedProperty(gpd, (IndexedPropertyDescriptor) spd);
} else if (gpd instanceof IndexedPropertyDescriptor) {
pd = mergePropertyWithIndexedProperty(spd, (IndexedPropertyDescriptor) gpd);
} else {
pd = mergePropertyDescriptor(gpd, spd);
}
} else if (ispd != null) {
// indexed setter
pd = ispd;
// Merge any classic property descriptors
if (spd != null) {
pd = mergePropertyDescriptor(ispd, spd);
}
if (gpd != null) {
pd = mergePropertyDescriptor(ispd, gpd);
}
} else if (igpd != null) {
// indexed getter
pd = igpd;
// Merge any classic property descriptors
if (gpd != null) {
pd = mergePropertyDescriptor(igpd, gpd);
}
if (spd != null) {
pd = mergePropertyDescriptor(igpd, spd);
}
} else if (spd != null) {
// simple setter
pd = spd;
} else if (gpd != null) {
// simple getter
pd = gpd;
}
// Very special case to ensure that an IndexedPropertyDescriptor
// doesn't contain less information than the enclosed
// PropertyDescriptor. If it does, then recreate as a
// PropertyDescriptor. See 4168833
if (pd instanceof IndexedPropertyDescriptor) {
ipd = (IndexedPropertyDescriptor)pd;
if (ipd.getIndexedReadMethod() == null && ipd.getIndexedWriteMethod() == null) {
pd = new PropertyDescriptor(ipd);
}
}
// Find the first property descriptor
// which does not have getter and setter methods.
// See regression bug 4984912.
if ( (pd == null) && (list.size() > 0) ) {
pd = list.get(0);
}
if (pd != null) {
properties.put(pd.getName(), pd);
}
}
}
private static boolean isAssignable(Class<?> current, Class<?> candidate) {
return ((current == null) || (candidate == null)) ? current == candidate : current.isAssignableFrom(candidate);
}
private PropertyDescriptor mergePropertyWithIndexedProperty(PropertyDescriptor pd, IndexedPropertyDescriptor ipd) {
Class<?> type = pd.getPropertyType();
if (type.isArray() && (type.getComponentType() == ipd.getIndexedPropertyType())) {
return pd.getClass0().isAssignableFrom(ipd.getClass0())
? new IndexedPropertyDescriptor(pd, ipd)
: new IndexedPropertyDescriptor(ipd, pd);
}
return pd;
}
/**
* Adds the property descriptor to the indexedproperty descriptor only if the
* types are the same.
*
* The most specific property descriptor will take precedence.
*/
private PropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd,
PropertyDescriptor pd) {
PropertyDescriptor result = null;
Class<?> propType = pd.getPropertyType();
Class<?> ipropType = ipd.getIndexedPropertyType();
if (propType.isArray() && propType.getComponentType() == ipropType) {
if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
result = new IndexedPropertyDescriptor(pd, ipd);
} else {
result = new IndexedPropertyDescriptor(ipd, pd);
}
} else if ((ipd.getReadMethod() == null) && (ipd.getWriteMethod() == null)) {
if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
result = new PropertyDescriptor(pd, ipd);
} else {
result = new PropertyDescriptor(ipd, pd);
}
} else {
// Cannot merge the pd because of type mismatch
// Return the most specific pd
if (pd.getClass0().isAssignableFrom(ipd.getClass0())) {
result = ipd;
} else {
result = pd;
// Try to add methods which may have been lost in the type change
// See 4168833
Method write = result.getWriteMethod();
Method read = result.getReadMethod();
if (read == null && write != null) {
read = findMethod(result.getClass0(),
GET_PREFIX + NameGenerator.capitalize(result.getName()), 0);
if (read != null) {
try {
result.setReadMethod(read);
} catch (IntrospectionException ex) {
// no consequences for failure.
}
}
}
if (write == null && read != null) {
write = findMethod(result.getClass0(),
SET_PREFIX + NameGenerator.capitalize(result.getName()), 1,
new Class<?>[] { FeatureDescriptor.getReturnType(result.getClass0(), read) });
if (write != null) {
try {
result.setWriteMethod(write);
} catch (IntrospectionException ex) {
// no consequences for failure.
}
}
}
}
}
return result;
}
// Handle regular pd merge
private PropertyDescriptor mergePropertyDescriptor(PropertyDescriptor pd1,
PropertyDescriptor pd2) {
if (pd1.getClass0().isAssignableFrom(pd2.getClass0())) {
return new PropertyDescriptor(pd1, pd2);
} else {
return new PropertyDescriptor(pd2, pd1);
}
}
// Handle regular ipd merge
private IndexedPropertyDescriptor mergePropertyDescriptor(IndexedPropertyDescriptor ipd1,
IndexedPropertyDescriptor ipd2) {
if (ipd1.getClass0().isAssignableFrom(ipd2.getClass0())) {
return new IndexedPropertyDescriptor(ipd1, ipd2);
} else {
return new IndexedPropertyDescriptor(ipd2, ipd1);
}
}
/**
* @return An array of EventSetDescriptors describing the kinds of
* events fired by the target bean.
*/
private EventSetDescriptor[] getTargetEventInfo() throws IntrospectionException {
if (events == null) {
events = new HashMap<>();
}
// Check if the bean has its own BeanInfo that will provide
// explicit information.
EventSetDescriptor[] explicitEvents = null;
if (explicitBeanInfo != null) {
explicitEvents = explicitBeanInfo.getEventSetDescriptors();
int ix = explicitBeanInfo.getDefaultEventIndex();
if (ix >= 0 && ix < explicitEvents.length) {
defaultEventName = explicitEvents[ix].getName();
}
}
if (explicitEvents == null && superBeanInfo != null) {
// We have no explicit BeanInfo events. Check with our parent.
EventSetDescriptor[] supers = superBeanInfo.getEventSetDescriptors();
for (int i = 0 ; i < supers.length; i++) {
addEvent(supers[i]);
}
int ix = superBeanInfo.getDefaultEventIndex();
if (ix >= 0 && ix < supers.length) {
defaultEventName = supers[ix].getName();
}
}
for (int i = 0; i < additionalBeanInfo.length; i++) {
EventSetDescriptor[] additional = additionalBeanInfo[i].getEventSetDescriptors();
if (additional != null) {
for (int j = 0 ; j < additional.length; j++) {
addEvent(additional[j]);
}
}
}
if (explicitEvents != null) {
// Add the explicit explicitBeanInfo data to our results.
for (int i = 0 ; i < explicitEvents.length; i++) {
addEvent(explicitEvents[i]);
}
} else {
// Apply some reflection to the current class.
for (Map.Entry<String,EventSetInfo> entry : ClassInfo.get(this.beanClass).getEventSets().entrySet()) {
// generate a list of Method objects for each of the target methods:
List<Method> methods = new ArrayList<>();
for (Method method : ClassInfo.get(entry.getValue().getListenerType()).getMethods()) {
if (isEventHandler(method)) {
methods.add(method);
}
}
addEvent(new EventSetDescriptor(
entry.getKey(),
entry.getValue(),
methods.toArray(new Method[methods.size()])));
}
JavaBean annotation = this.beanClass.getAnnotation(JavaBean.class);
if ((annotation != null) && !annotation.defaultEventSet().isEmpty()) {
this.defaultEventName = annotation.defaultEventSet();
}
}
EventSetDescriptor[] result;
if (events.size() == 0) {
result = EMPTY_EVENTSETDESCRIPTORS;
} else {
// Allocate and populate the result array.
result = new EventSetDescriptor[events.size()];
result = events.values().toArray(result);
// Set the default index.
if (defaultEventName != null) {
for (int i = 0; i < result.length; i++) {
if (defaultEventName.equals(result[i].getName())) {
defaultEventIndex = i;
}
}
}
}
return result;
}
private void addEvent(EventSetDescriptor esd) {
String key = esd.getName();
if (esd.getName().equals("propertyChange")) {
propertyChangeSource = true;
}
EventSetDescriptor old = events.get(key);
if (old == null) {
events.put(key, esd);
return;
}
EventSetDescriptor composite = new EventSetDescriptor(old, esd);
events.put(key, composite);
}
/**
* @return An array of MethodDescriptors describing the private
* methods supported by the target bean.
*/
private MethodDescriptor[] getTargetMethodInfo() {
if (methods == null) {
methods = new HashMap<>(100);
}
// Check if the bean has its own BeanInfo that will provide
// explicit information.
MethodDescriptor[] explicitMethods = null;
if (explicitBeanInfo != null) {