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
33 public class BeanIntrospector implements Introspector
34 {
35
36 @Override
37 public Property[] getProperties(final Object introspectedObject)
38 {
39 Object introspected = introspectedObject;
40 while (introspected instanceof DelegateIntrospection)
41 {
42 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
43 }
44 Set<Property> props = new LinkedHashSet<Property>();
45 try
46 {
47 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
48
49 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
50 for (int i = 0; i < descrips.length; i++)
51 {
52 props.add(new BeanProperty(introspected, descrips[i]));
53 }
54 }
55 catch (Exception e)
56 {
57 throw new IllegalArgumentException(this + " - getProperties", e);
58 }
59 return props.toArray(new Property[props.size()]);
60 }
61
62
63 @Override
64 public Property getProperty(final Object introspectedObject, final String property)
65 {
66 Object introspected = introspectedObject;
67 while (introspected instanceof DelegateIntrospection)
68 {
69 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
70 }
71 try
72 {
73 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
74 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
75 for (int i = 0; i < descrips.length; i++)
76 {
77 if (descrips[i].getName().equals(property))
78 {
79 return new BeanProperty(introspected, descrips[i]);
80 }
81 }
82 }
83 catch (Exception e)
84 {
85 throw new IllegalArgumentException(this + " - getProperty", e);
86 }
87 throw new IllegalArgumentException("Property '" + property + "' not found for " + introspected);
88 }
89
90
91 @Override
92 public String[] getPropertyNames(final Object introspectedObject)
93 {
94 Object introspected = introspectedObject;
95 while (introspected instanceof DelegateIntrospection)
96 {
97 introspected = ((DelegateIntrospection) introspected).getParentIntrospectionObject();
98 }
99 Set<String> props = new LinkedHashSet<String>();
100 try
101 {
102 BeanInfo info = java.beans.Introspector.getBeanInfo(introspected.getClass());
103 PropertyDescriptor[] descrips = info.getPropertyDescriptors();
104 for (int i = 0; i < descrips.length; i++)
105 {
106 props.add(descrips[i].getName());
107 }
108 }
109 catch (Exception e)
110 {
111 throw new IllegalArgumentException(this + " - getPropertyNames", e);
112 }
113 return props.toArray(new String[props.size()]);
114 }
115 }