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                  subClass = propertyField1.getType();
61  			}
62  			//
63  			if (List.class.equals(subClass)) {
64  				try {
65  //					Field propertyField = clazz
66  //							.getDeclaredField(propertyName);
67  					
68  					Field propertyField = findField(propertyName, fields);
69  					ParameterizedType propertyGenericDataType = (ParameterizedType) propertyField.getGenericType();
70  					subClass = (Class<?>) propertyGenericDataType
71  							.getActualTypeArguments()[0];
72  //				} catch (NoSuchFieldException ex) {
73  //					throw new RuntimeException(ex);
74  				} catch (SecurityException ex) {
75  					throw new RuntimeException(ex);
76  				}
77  			}
78  			//
79  			if (!MetaInfo.class.equals(subClass)
80  					&& !Meta.class.equals(subClass)		// added coz it's a interface object
81  					&& !RichText.class.equals(subClass)		// added coz it's a interface object					
82  					&& !Class.class.equals(subClass)
83  					
84  					&& !String.class.equals(subClass)
85  					&& !Integer.class.equals(subClass)
86  					&& !Long.class.equals(subClass)
87  					&& !Boolean.class.equals(subClass)
88  					&& !boolean.class.equals(subClass)
89  					&& !int.class.equals(subClass)
90  					&& !long.class.equals(subClass)
91  					&& !Double.class.equals(subClass)
92  					&& !Float.class.equals(subClass)
93  					&& !Date.class.equals(subClass)
94  					&& !DictionaryConstants.ATTRIBUTES.equals(propertyName)
95  					&& !Enum.class.isAssignableFrom(subClass)
96  					&& !Object.class.equals(subClass)) {
97  				loadComplexStructures(subClass.getName(), complexStructures);
98  			}
99  		}
100 	}
101 	
102 	
103 	public ArrayList<Field> getAllFields(ArrayList<Field> fields, Class<?> type) {
104         Collections.addAll(fields, type.getDeclaredFields());
105 
106 	    if (type.getSuperclass() != null) {
107 	        fields = getAllFields(fields, type.getSuperclass());
108 	    }
109 
110 	    return fields;
111 	}
112 	
113 	public Field findField(String fieldName, ArrayList<Field> fields) {
114 		for (Field field : fields) {
115 			if (field.getName().equals(fieldName)) {
116 				return field;
117 			}
118 		}
119 		return null;
120 	}
121 
122 }