| 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 | 1 | private final static Integer NUMBER_OF_TYPE_ARGUMENTS = 1; |
| 51 | |
|
| 52 | |
private static Set<PropertyTypeDefinition> handledTypes; |
| 53 | |
|
| 54 | |
static { |
| 55 | 1 | handledTypes = new HashSet<PropertyTypeDefinition>(); |
| 56 | 1 | handledTypes.add(new PropertyTypeDefinition(List.class)); |
| 57 | 1 | handledTypes.add(new PropertyTypeDefinition(Set.class)); |
| 58 | 1 | } |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
public ListAndSetPropertyTypeHandler() { |
| 64 | 1 | super(handledTypes); |
| 65 | 1 | } |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
@SuppressWarnings("unchecked") |
| 71 | |
public Object handle(HandledBeanProperty handledBeanProperty) { |
| 72 | 7 | Collection result = null; |
| 73 | 7 | final Integer COLLECTION_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty); |
| 74 | |
try { |
| 75 | 7 | Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName()); |
| 76 | 7 | Class<?> usurpedClass = Object.class; |
| 77 | 7 | Class<?> entityClass = attribute.getType(); |
| 78 | 7 | usurpedClass = (Class) ReflectionUtils.getGenericTypes(attribute)[NUMBER_OF_TYPE_ARGUMENTS - 1]; |
| 79 | 7 | if (entityClass.isAssignableFrom(List.class)) { |
| 80 | 4 | result = new ArrayList(COLLECTION_ENTRIES); |
| 81 | 3 | } else if (entityClass.isAssignableFrom(Set.class)) { |
| 82 | 3 | result = new HashSet(COLLECTION_ENTRIES); |
| 83 | |
} |
| 84 | 7 | HandledBeanProperty handledCollectionItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup()); |
| 85 | 77 | for (int i = 0; i < COLLECTION_ENTRIES; i++) { |
| 86 | 70 | if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) { |
| 87 | 50 | result.add(handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem)); |
| 88 | |
} else { |
| 89 | 20 | if (usurpedClass.isEnum()) { |
| 90 | 20 | result.add(handledBeanProperty.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem)); |
| 91 | |
} else { |
| 92 | 0 | throw new NoHandlerDefinedException("no handler found for Class <" + new PropertyTypeDefinition(usurpedClass) + "> of List / Set."); |
| 93 | |
} |
| 94 | |
} |
| 95 | |
} |
| 96 | 0 | } catch (NoSuchFieldException e) { |
| 97 | 0 | throw new PropertyTypeHandlingException("Unable to handle field <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e); |
| 98 | 7 | } |
| 99 | 7 | return result; |
| 100 | |
} |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
@SuppressWarnings("unchecked") |
| 106 | |
public Object handle(HandledConstructorArg handledConstructorArg) { |
| 107 | 3 | Collection result = null; |
| 108 | 3 | final Integer COLLECTION_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg); |
| 109 | 3 | Class<?> collectionClass = handledConstructorArg.getConstructorArgClass(); |
| 110 | 3 | Class<?> usurpedClass = (Class<?>) ReflectionUtils.getGenericTypes(handledConstructorArg.getTargetConstructor(), handledConstructorArg.getConstructorArgOrderingNumber())[NUMBER_OF_TYPE_ARGUMENTS - 1]; |
| 111 | 3 | if (collectionClass.isAssignableFrom(List.class)) { |
| 112 | 2 | result = new ArrayList(COLLECTION_ENTRIES); |
| 113 | 1 | } else if (collectionClass.isAssignableFrom(Set.class)) { |
| 114 | 1 | result = new HashSet(COLLECTION_ENTRIES); |
| 115 | |
} |
| 116 | 3 | HandledConstructorArg handledCollectionItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup()); |
| 117 | 33 | for (int i = 0; i < COLLECTION_ENTRIES; i++) { |
| 118 | 30 | if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) { |
| 119 | 30 | result.add(handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledCollectionItem)); |
| 120 | |
} else { |
| 121 | 0 | if (usurpedClass.isEnum()) { |
| 122 | 0 | result.add(handledConstructorArg.getUsurperGeneratorSetup().getEnumHandler().handle(handledCollectionItem)); |
| 123 | |
} else { |
| 124 | 0 | throw new NoHandlerDefinedException("no handler found for Class <" + usurpedClass.getName() + "> of List / Set."); |
| 125 | |
} |
| 126 | |
} |
| 127 | |
} |
| 128 | 3 | return result; |
| 129 | |
} |
| 130 | |
|
| 131 | |
} |