1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.org.usurper.handlers.defaults;
24
25 import java.lang.reflect.Field;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 import org.org.usurper.handlers.basic.AbstractPropertyTypeHandler;
33 import org.org.usurper.handlers.exceptions.NoHandlerDefinedException;
34 import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException;
35 import org.org.usurper.model.HandledBeanProperty;
36 import org.org.usurper.model.HandledConstructorArg;
37 import org.org.usurper.model.PropertyTypeDefinition;
38 import org.org.usurper.utils.ReflectionUtils;
39
40
41
42
43
44
45
46
47
48 public class ListAndSetPropertyTypeHandler extends AbstractPropertyTypeHandler {
49
50 private final static Integer NUMBER_OF_TYPE_ARGUMENTS = 1;
51
52 private static Set<PropertyTypeDefinition> handledTypes;
53
54 static {
55 handledTypes = new HashSet<PropertyTypeDefinition>();
56 handledTypes.add(new PropertyTypeDefinition(List.class));
57 handledTypes.add(new PropertyTypeDefinition(Set.class));
58 }
59
60
61
62
63 public ListAndSetPropertyTypeHandler() {
64 super(handledTypes);
65 }
66
67
68
69
70 @SuppressWarnings("unchecked")
71 public Object handle(HandledBeanProperty handledBeanProperty) {
72 Collection result = null;
73 final Integer COLLECTION_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty);
74 try {
75 Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName());
76 Class<?> usurpedClass = Object.class;
77 Class<?> entityClass = attribute.getType();
78 usurpedClass = (Class) ReflectionUtils.getGenericTypes(attribute)[NUMBER_OF_TYPE_ARGUMENTS - 1];
79 if (entityClass.isAssignableFrom(List.class)) {
80 result = new ArrayList(COLLECTION_ENTRIES);
81 } else if (entityClass.isAssignableFrom(Set.class)) {
82 result = new HashSet(COLLECTION_ENTRIES);
83 }
84 HandledBeanProperty handledCollectionItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
85 for (int i = 0; i < COLLECTION_ENTRIES; i++) {
86 if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
87 result.add(handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem));
88 } else {
89 if (usurpedClass.isEnum()) {
90 result.add(handledBeanProperty.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem));
91 } else {
92 throw new NoHandlerDefinedException("no handler found for Class <" + new PropertyTypeDefinition(usurpedClass) + "> of List / Set.");
93 }
94 }
95 }
96 } catch (NoSuchFieldException e) {
97 throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
98 }
99 return result;
100 }
101
102
103
104
105 @SuppressWarnings("unchecked")
106 public Object handle(HandledConstructorArg handledConstructorArg) {
107 Collection result = null;
108 final Integer COLLECTION_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg);
109 Class<?> collectionClass = handledConstructorArg.getConstructorArgClass();
110 Class<?> usurpedClass = (Class<?>) ReflectionUtils.getGenericTypes(handledConstructorArg.getTargetConstructor(), handledConstructorArg.getConstructorArgOrderingNumber())[NUMBER_OF_TYPE_ARGUMENTS - 1];
111 if (collectionClass.isAssignableFrom(List.class)) {
112 result = new ArrayList(COLLECTION_ENTRIES);
113 } else if (collectionClass.isAssignableFrom(Set.class)) {
114 result = new HashSet(COLLECTION_ENTRIES);
115 }
116 HandledConstructorArg handledCollectionItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
117 for (int i = 0; i < COLLECTION_ENTRIES; i++) {
118 if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
119 result.add(handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem));
120 } else {
121 if (usurpedClass.isEnum()) {
122 result.add(handledConstructorArg.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem));
123 } else {
124 throw new NoHandlerDefinedException("no handler found for Class <" + usurpedClass.getName() + "> of List / Set.");
125 }
126 }
127 }
128 return result;
129 }
130
131 }