1 |
|
package org.kuali.student.common.dictionary.service.impl; |
2 |
|
|
3 |
|
import java.beans.BeanInfo; |
4 |
|
import java.beans.IntrospectionException; |
5 |
|
import java.beans.Introspector; |
6 |
|
import java.beans.PropertyDescriptor; |
7 |
|
import java.lang.reflect.ParameterizedType; |
8 |
|
import java.util.Date; |
9 |
|
import java.util.List; |
10 |
|
|
11 |
|
import org.kuali.student.common.dictionary.dto.DataType; |
12 |
|
import org.kuali.student.common.dictionary.dto.FieldDefinition; |
13 |
|
import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition; |
14 |
|
import org.kuali.student.common.dto.MetaInfo; |
15 |
|
|
16 |
|
|
|
|
| 86.9% |
Uncovered Elements: 11 (84) |
Complexity: 29 |
Complexity Density: 0.57 |
|
17 |
|
public class Bean2DictionaryConverter |
18 |
|
{ |
19 |
|
|
20 |
|
private Class<?> clazz; |
21 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
22 |
113
|
public Bean2DictionaryConverter (Class<?> clazz)... |
23 |
|
{ |
24 |
113
|
this.clazz = clazz; |
25 |
|
} |
26 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 5 |
Complexity Density: 0.45 |
|
27 |
113
|
public ObjectStructureDefinition convert ()... |
28 |
|
{ |
29 |
113
|
ObjectStructureDefinition os = new ObjectStructureDefinition (); |
30 |
113
|
os.setName (clazz.getName ()); |
31 |
113
|
BeanInfo beanInfo; |
32 |
113
|
try |
33 |
|
{ |
34 |
113
|
beanInfo = Introspector.getBeanInfo (clazz); |
35 |
|
} |
36 |
|
catch (IntrospectionException ex) |
37 |
|
{ |
38 |
0
|
throw new RuntimeException (ex); |
39 |
|
} |
40 |
113
|
os.setHasMetaData (calcHasMetaData (beanInfo)); |
41 |
113
|
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
42 |
|
{ |
43 |
973
|
if ( ! MetaInfo.class.equals (pd.getPropertyType ()) |
44 |
|
&& ! Class.class.equals (pd.getPropertyType ()) |
45 |
|
&& ! DictionaryConstants.ATTRIBUTES.equals (pd.getName ())) |
46 |
|
{ |
47 |
751
|
os.getAttributes ().add (calcField (clazz, pd)); |
48 |
|
} |
49 |
|
} |
50 |
113
|
return os; |
51 |
|
} |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
53 |
113
|
private boolean calcHasMetaData (BeanInfo beanInfo)... |
54 |
|
{ |
55 |
113
|
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors ()) |
56 |
|
{ |
57 |
732
|
if (MetaInfo.class.equals (pd.getPropertyType ())) |
58 |
|
{ |
59 |
50
|
return true; |
60 |
|
} |
61 |
|
|
62 |
|
} |
63 |
63
|
return false; |
64 |
|
} |
65 |
|
|
|
|
| 85.7% |
Uncovered Elements: 2 (14) |
Complexity: 4 |
Complexity Density: 0.33 |
|
66 |
751
|
private FieldDefinition calcField (Class<?> clazz, PropertyDescriptor pd)... |
67 |
|
{ |
68 |
751
|
FieldDefinition fd = new FieldDefinition (); |
69 |
751
|
fd.setName (pd.getName ()); |
70 |
751
|
Class<?> pt = pd.getPropertyType (); |
71 |
751
|
if (List.class.equals (pt)) |
72 |
|
{ |
73 |
117
|
fd.setMaxOccurs (fd.UNBOUNDED); |
74 |
117
|
try |
75 |
|
{ |
76 |
117
|
pt = |
77 |
|
(Class<?>) ((ParameterizedType) clazz.getDeclaredField (pd.getName ()).getGenericType ()).getActualTypeArguments ()[0]; |
78 |
|
} |
79 |
|
catch (NoSuchFieldException ex) |
80 |
|
{ |
81 |
0
|
throw new RuntimeException (ex); |
82 |
|
} |
83 |
|
catch (SecurityException ex) |
84 |
|
{ |
85 |
0
|
throw new RuntimeException (ex); |
86 |
|
} |
87 |
|
} |
88 |
|
else |
89 |
|
{ |
90 |
634
|
fd.setMaxOccurs (fd.SINGLE); |
91 |
|
} |
92 |
751
|
fd.setDataType (calcDataType (pt)); |
93 |
751
|
return fd; |
94 |
|
} |
95 |
|
|
|
|
| 82.2% |
Uncovered Elements: 8 (45) |
Complexity: 17 |
Complexity Density: 0.74 |
|
96 |
751
|
private DataType calcDataType (Class<?> pt)... |
97 |
|
{ |
98 |
751
|
if (int.class.equals (pt) || Integer.class.equals (pt)) |
99 |
|
{ |
100 |
11
|
return DataType.INTEGER; |
101 |
|
} |
102 |
740
|
else if (long.class.equals (pt) || Long.class.equals (pt)) |
103 |
|
{ |
104 |
5
|
return DataType.LONG; |
105 |
|
} |
106 |
735
|
else if (double.class.equals (pt) || Double.class.equals (pt)) |
107 |
|
{ |
108 |
0
|
return DataType.DOUBLE; |
109 |
|
} |
110 |
735
|
else if (float.class.equals (pt) || Float.class.equals (pt)) |
111 |
|
{ |
112 |
0
|
return DataType.FLOAT; |
113 |
|
} |
114 |
735
|
else if (boolean.class.equals (pt) || Boolean.class.equals (pt)) |
115 |
|
{ |
116 |
10
|
return DataType.BOOLEAN; |
117 |
|
} |
118 |
725
|
else if (Date.class.equals (pt)) |
119 |
|
{ |
120 |
79
|
return DataType.DATE; |
121 |
|
} |
122 |
646
|
else if (String.class.equals (pt)) |
123 |
|
{ |
124 |
513
|
return DataType.STRING; |
125 |
|
} |
126 |
133
|
else if (List.class.equals (pt)) |
127 |
|
{ |
128 |
0
|
throw new RuntimeException ("Can't have a list of lists, List<List<?>>"); |
129 |
|
} |
130 |
133
|
else if (Enum.class.isAssignableFrom (pt)) |
131 |
|
{ |
132 |
3
|
return DataType.STRING; |
133 |
|
} |
134 |
130
|
else if (Object.class.equals (pt)) |
135 |
|
{ |
136 |
1
|
return DataType.STRING; |
137 |
|
} |
138 |
129
|
else if (pt.getName ().startsWith ("org.kuali.student.")) |
139 |
|
{ |
140 |
129
|
return DataType.COMPLEX; |
141 |
|
} |
142 |
|
else |
143 |
|
{ |
144 |
0
|
throw new RuntimeException ("unknown/unhandled type of object in bean " + pt.getName ()); |
145 |
|
} |
146 |
|
} |
147 |
|
} |
148 |
|
|