1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.datadictionary.util; |
17 | |
|
18 | |
import java.beans.BeanInfo; |
19 | |
import java.beans.IntrospectionException; |
20 | |
import java.beans.Introspector; |
21 | |
import java.beans.PropertyDescriptor; |
22 | |
import java.lang.reflect.Field; |
23 | |
import java.lang.reflect.ParameterizedType; |
24 | |
import java.lang.reflect.Type; |
25 | |
import java.util.Date; |
26 | |
import java.util.LinkedHashSet; |
27 | |
import java.util.List; |
28 | |
import java.util.Set; |
29 | |
|
30 | 0 | public class ComplexSubstructuresHelper { |
31 | |
|
32 | |
public Set<String> getComplexStructures(String className) { |
33 | 0 | Set<String> complexStructures = new LinkedHashSet<String>(); |
34 | 0 | loadComplexStructures(className, complexStructures); |
35 | 0 | return complexStructures; |
36 | |
} |
37 | |
|
38 | |
private void loadComplexStructures(String className, |
39 | |
Set<String> complexStructures) { |
40 | 0 | if (!complexStructures.add(className)) { |
41 | 0 | return; |
42 | |
} |
43 | |
BeanInfo beanInfo; |
44 | |
Class<?> clazz; |
45 | |
try { |
46 | 0 | clazz = Class.forName(className); |
47 | 0 | } catch (ClassNotFoundException ex) { |
48 | 0 | System.out.println("ComplexSubstructuresHelper: Could not process because the class must be a freestanding object: " + className); |
49 | 0 | return; |
50 | 0 | } |
51 | |
try { |
52 | 0 | beanInfo = Introspector.getBeanInfo(clazz); |
53 | 0 | } catch (IntrospectionException ex) { |
54 | 0 | throw new RuntimeException(ex); |
55 | 0 | } |
56 | 0 | for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { |
57 | 0 | Class<?> subClass = pd.getPropertyType(); |
58 | 0 | if (List.class.equals(subClass)) { |
59 | |
|
60 | 0 | subClass = getActualClassFromList(clazz, pd.getName()); |
61 | |
} |
62 | 0 | if (!Class.class.equals(subClass) |
63 | |
&& !String.class.equals(subClass) |
64 | |
&& !Integer.class.equals(subClass) |
65 | |
&& !Long.class.equals(subClass) |
66 | |
&& !Boolean.class.equals(subClass) |
67 | |
&& !boolean.class.equals(subClass) |
68 | |
&& !int.class.equals(subClass) |
69 | |
&& !long.class.equals(subClass) |
70 | |
&& !Double.class.equals(subClass) |
71 | |
&& !Float.class.equals(subClass) |
72 | |
&& !Date.class.equals(subClass) |
73 | |
&& !Enum.class.isAssignableFrom(subClass) |
74 | |
&& !Object.class.equals(subClass)) { |
75 | 0 | loadComplexStructures(subClass.getName(), complexStructures); |
76 | |
} |
77 | |
} |
78 | 0 | } |
79 | |
|
80 | |
public static Class<?> getActualClassFromList(Class<?> classToCheck, String fieldName) { |
81 | |
|
82 | |
while (true) { |
83 | |
try { |
84 | 0 | Field field = classToCheck.getDeclaredField(fieldName); |
85 | 0 | Type type = field.getGenericType(); |
86 | 0 | ParameterizedType pt = (ParameterizedType) type; |
87 | 0 | Type actualType = pt.getActualTypeArguments()[0]; |
88 | 0 | return (Class<?>) actualType; |
89 | 0 | } catch (NoSuchFieldException ex) { |
90 | 0 | classToCheck = classToCheck.getSuperclass(); |
91 | 0 | if (classToCheck == null) { |
92 | 0 | throw new RuntimeException(ex); |
93 | |
} |
94 | 0 | if (classToCheck.equals(Object.class)) { |
95 | 0 | throw new RuntimeException(ex); |
96 | |
} |
97 | 0 | } catch (SecurityException ex) { |
98 | 0 | throw new RuntimeException(ex); |
99 | 0 | } |
100 | |
} |
101 | |
} |
102 | |
} |