Clover Coverage Report - KS Contract Documentation Generator 0.0.1-SNAPSHOT
Coverage timestamp: Wed Dec 31 1969 19:00:00 EST
../../../../../img/srcFileCovDistChart0.png 0% of files have more coverage
36   110   25   12
12   85   0.69   3
3     8.33  
1    
 
  ComplexSubstructuresHelper       Line # 31 36 0% 25 51 0% 0.0
 
No Tests
 
1    /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 1.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl1.php
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
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.lang.reflect.TypeVariable;
26    import java.util.Date;
27    import java.util.LinkedHashSet;
28    import java.util.List;
29    import java.util.Set;
30   
 
31    public class ComplexSubstructuresHelper {
32   
 
33  0 toggle public Set<String> getComplexStructures(String className) {
34  0 Set<String> complexStructures = new LinkedHashSet<String>();
35  0 loadComplexStructures(className, complexStructures);
36  0 return complexStructures;
37    }
38   
 
39  0 toggle private void loadComplexStructures(String className,
40    Set<String> complexStructures) {
41  0 if (!complexStructures.add(className)) {
42  0 return;
43    }
44  0 BeanInfo beanInfo;
45  0 Class<?> clazz;
46  0 try {
47  0 clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
48    // had to change the standard Class.forName below to the above so it uses the right class loader
49    // that is defined in KSDictionaryDocMojo.java
50    // clazz = Class.forName(className);
51    } catch (ClassNotFoundException ex) {
52  0 System.out.println("ComplexSubstructuresHelper: Could not process because the class must be a freestanding object: " + className);
53  0 return;
54    }
55  0 try {
56  0 beanInfo = Introspector.getBeanInfo(clazz);
57    } catch (IntrospectionException ex) {
58  0 throw new RuntimeException(ex);
59    }
60  0 for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
61  0 Class<?> subClass = pd.getPropertyType();
62  0 if (List.class.equals(subClass)) {
63    // recursively check super classes for field if not declared on this class
64  0 subClass = getActualClassFromList(clazz, pd.getName());
65    }
66  0 if (!Class.class.equals(subClass)
67    && !String.class.equals(subClass)
68    && !Integer.class.equals(subClass)
69    && !Long.class.equals(subClass)
70    && !Boolean.class.equals(subClass)
71    && !boolean.class.equals(subClass)
72    && !int.class.equals(subClass)
73    && !long.class.equals(subClass)
74    && !Double.class.equals(subClass)
75    && !Float.class.equals(subClass)
76    && !Date.class.equals(subClass)
77    && !Enum.class.isAssignableFrom(subClass)
78    && !Object.class.equals(subClass)) {
79  0 loadComplexStructures(subClass.getName(), complexStructures);
80    }
81    }
82    }
83   
 
84  0 toggle public static Class<?> getActualClassFromList(Class<?> originalClass, String fieldName) {
85  0 if (originalClass.isInterface()) {
86  0 throw new RuntimeException("Interface used in getter, use xxxInfo instead for field: " + originalClass.getName() + "." + fieldName);
87    }
88    // recursively check super classes for field if not declared on this class
89  0 Class<?> classToCheck = originalClass;
90  0 while (true) {
91  0 try {
92  0 Field field = classToCheck.getDeclaredField(fieldName);
93  0 Type type = field.getGenericType();
94  0 ParameterizedType pt = (ParameterizedType) type;
95  0 Type actualType = pt.getActualTypeArguments()[0];
96  0 return (Class<?>) actualType;
97    } catch (NoSuchFieldException ex) {
98  0 classToCheck = classToCheck.getSuperclass();
99  0 if (classToCheck == null) {
100  0 throw new RuntimeException("No such field: " + originalClass.getName() + "." + fieldName, ex);
101    }
102  0 if (classToCheck.equals(Object.class)) {
103  0 throw new RuntimeException("No such field: " + originalClass.getName() + "." + fieldName, ex);
104    }
105    } catch (SecurityException ex) {
106  0 throw new RuntimeException(originalClass.getName() + "." + fieldName, ex);
107    }
108    }
109    }
110    }