1 package org.kuali.student.r1.common.dictionary.service.impl;
2
3 import org.kuali.student.r2.common.dto.MetaInfo;
4 import org.kuali.student.r2.common.infc.Meta;
5 import org.kuali.student.r2.common.infc.RichText;
6
7 import java.beans.BeanInfo;
8 import java.beans.IntrospectionException;
9 import java.beans.Introspector;
10 import java.beans.PropertyDescriptor;
11 import java.lang.reflect.Field;
12 import java.lang.reflect.ParameterizedType;
13 import java.util.*;
14
15 @Deprecated
16 public class ComplexSubstructuresHelper {
17
18 public Set<String> getComplexStructures(String className) {
19 Set<String> complexStructures = new LinkedHashSet<String>();
20 loadComplexStructures(className, complexStructures);
21 return complexStructures;
22 }
23
24 private void loadComplexStructures(String className,
25 Set<String> complexStructures) {
26 if (!complexStructures.add(className)) {
27 return;
28 }
29 BeanInfo beanInfo;
30 Class<?> clazz;
31 try {
32 clazz = Class.forName(className);
33
34 } catch (ClassNotFoundException ex) {
35 System.out
36 .println("ComplexSubstructuresHelper: Could not process because the class must be a freestanding object: "
37 + className);
38 return;
39 }
40 try {
41 beanInfo = Introspector.getBeanInfo(clazz);
42 } catch (IntrospectionException ex) {
43 throw new RuntimeException(ex);
44 }
45
46
47 ArrayList<Field> fields = new ArrayList<Field>();
48 fields = this.getAllFields(fields, clazz);
49
50 for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
51 String propertyName = pd.getName();
52
53 Class<?> subClass = pd.getPropertyType();
54
55 Field propertyField1 = findField(propertyName, fields);
56
57
58
59 if (propertyField1 != null) {
60 Class<?> subClassField = propertyField1.getType();
61 subClass = subClassField;
62 }
63
64 if (List.class.equals(subClass)) {
65 try {
66
67
68
69 Field propertyField = findField(propertyName, fields);
70 ParameterizedType propertyGenericDataType = (ParameterizedType) propertyField.getGenericType();
71 subClass = (Class<?>) propertyGenericDataType
72 .getActualTypeArguments()[0];
73
74
75 } catch (SecurityException ex) {
76 throw new RuntimeException(ex);
77 }
78 }
79
80 if (!MetaInfo.class.equals(subClass)
81 && !Meta.class.equals(subClass)
82 && !RichText.class.equals(subClass)
83 && !Class.class.equals(subClass)
84
85 && !String.class.equals(subClass)
86 && !Integer.class.equals(subClass)
87 && !Long.class.equals(subClass)
88 && !Boolean.class.equals(subClass)
89 && !boolean.class.equals(subClass)
90 && !int.class.equals(subClass)
91 && !long.class.equals(subClass)
92 && !Double.class.equals(subClass)
93 && !Float.class.equals(subClass)
94 && !Date.class.equals(subClass)
95 && !DictionaryConstants.ATTRIBUTES.equals(propertyName)
96 && !Enum.class.isAssignableFrom(subClass)
97 && !Object.class.equals(subClass)) {
98 loadComplexStructures(subClass.getName(), complexStructures);
99 }
100 }
101 }
102
103
104 public ArrayList<Field> getAllFields(ArrayList<Field> fields, Class<?> type) {
105 for (Field field: type.getDeclaredFields()) {
106 fields.add(field);
107 }
108
109 if (type.getSuperclass() != null) {
110 fields = getAllFields(fields, type.getSuperclass());
111 }
112
113 return fields;
114 }
115
116 public Field findField(String fieldName, ArrayList<Field> fields) {
117 for (Field field : fields) {
118 if (field.getName().equals(fieldName)) {
119 return field;
120 }
121 }
122 return null;
123 }
124
125 }