View Javadoc

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  			// Debuggin  System.out.println("The Class : " + className);
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  		// Get all the fields including inherited fields...
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  //			System.out.println(propertyName);
53  			Class<?> subClass = pd.getPropertyType();
54  			// find the actual class and not the interface for the field
55  			Field propertyField1 = findField(propertyName, fields);
56              // The following was added because of this behaviour, explained in the urls under this comment
57              // https://forums.oracle.com/forums/thread.jspa?threadID=1158697
58              // http://bugs.sun.com/view_bug.do?bug_id=6794807
59  			if (propertyField1 != null) {
60  				Class<?> subClassField =  propertyField1.getType();
61  				subClass = subClassField;
62  			}
63  			//
64  			if (List.class.equals(subClass)) {
65  				try {
66  //					Field propertyField = clazz
67  //							.getDeclaredField(propertyName);
68  					
69  					Field propertyField = findField(propertyName, fields);
70  					ParameterizedType propertyGenericDataType = (ParameterizedType) propertyField.getGenericType();
71  					subClass = (Class<?>) propertyGenericDataType
72  							.getActualTypeArguments()[0];
73  //				} catch (NoSuchFieldException ex) {
74  //					throw new RuntimeException(ex);
75  				} catch (SecurityException ex) {
76  					throw new RuntimeException(ex);
77  				}
78  			}
79  			//
80  			if (!MetaInfo.class.equals(subClass)
81  					&& !Meta.class.equals(subClass)		// added coz it's a interface object
82  					&& !RichText.class.equals(subClass)		// added coz it's a interface object					
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 }