1 package nl.tudelft.simulation.introspection.beans;
2
3 import java.beans.BeanInfo;
4 import java.beans.PropertyDescriptor;
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 import nl.tudelft.simulation.introspection.DelegateIntrospection;
9 import nl.tudelft.simulation.introspection.Introspector;
10 import nl.tudelft.simulation.introspection.Property;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public class BeanIntrospector implements Introspector
33 {
34 @Override
35 public Property[] getProperties(final Object introspectedObject)
36 {
37 Object introspected = introspectedObject;
38 while (introspected instanceof DelegateIntrospection)
39 {
40 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
41 }
42 Set<Property> props = new LinkedHashSet<Property>();
43 try
44 {
45 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
46
47 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
48 for (int i = 0; i < descrips.length; i++)
49 {
50 props.add(new BeanProperty(introspected, descrips[i]));
51 }
52 }
53 catch (Exception e)
54 {
55 throw new IllegalArgumentException(this + " - getProperties", e);
56 }
57 return props.toArray(new Property[props.size()]);
58 }
59
60 @Override
61 public Property getProperty(final Object introspectedObject, final String property)
62 {
63 Object introspected = introspectedObject;
64 while (introspected instanceof DelegateIntrospection)
65 {
66 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
67 }
68 try
69 {
70 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
71 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
72 for (int i = 0; i < descrips.length; i++)
73 {
74 if (descrips[i].getName().equals(property))
75 { return new BeanProperty(introspected, descrips[i]); }
76 }
77 }
78 catch (Exception e)
79 {
80 throw new IllegalArgumentException(this + " - getProperty", e);
81 }
82 throw new IllegalArgumentException("Property '" + property + "' not found for " + introspected);
83 }
84
85 @Override
86 public String[] getPropertyNames(final Object introspectedObject)
87 {
88 Object introspected = introspectedObject;
89 while (introspected instanceof DelegateIntrospection)
90 {
91 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
92 }
93 Set<String> props = new LinkedHashSet<String>();
94 try
95 {
96 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
97 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
98 for (int i = 0; i < descrips.length; i++)
99 {
100 props.add(descrips[i].getName());
101 }
102 }
103 catch (Exception e)
104 {
105 throw new IllegalArgumentException(this + " - getPropertyNames", e);
106 }
107 return props.toArray(new String[props.size()]);
108 }
109 }