| 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.basic; |
| 24 | |
|
| 25 | |
import java.lang.reflect.Array; |
| 26 | |
import java.lang.reflect.Field; |
| 27 | |
|
| 28 | |
import org.org.usurper.handlers.IHandler; |
| 29 | |
import org.org.usurper.handlers.exceptions.NoHandlerDefinedException; |
| 30 | |
import org.org.usurper.handlers.exceptions.PropertyTypeHandlingException; |
| 31 | |
import org.org.usurper.model.HandledBeanProperty; |
| 32 | |
import org.org.usurper.model.HandledConstructorArg; |
| 33 | |
import org.org.usurper.model.PropertyTypeDefinition; |
| 34 | |
import org.org.usurper.utils.ReflectionUtils; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | 355 | public final class ArrayHandler implements IHandler { |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public Object handle(HandledBeanProperty handledBeanProperty) { |
| 48 | 11 | Object result = null; |
| 49 | 11 | final Integer ARRAY_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty); |
| 50 | |
try { |
| 51 | 11 | Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName()); |
| 52 | 11 | Class<?> usurpedClass = attribute.getType().getComponentType(); |
| 53 | 11 | result = Array.newInstance(ReflectionUtils.toNotPrimitiveType(usurpedClass), ARRAY_ENTRIES); |
| 54 | 11 | HandledBeanProperty handledArrayItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup()); |
| 55 | 121 | for (int i = 0; i < ARRAY_ENTRIES; i++) { |
| 56 | 110 | if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) { |
| 57 | 100 | Array.set(result, i, handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem)); |
| 58 | |
} else { |
| 59 | 10 | if (usurpedClass.isEnum()) { |
| 60 | 10 | Array.set(result, i, new EnumHandler().handle(handledArrayItem)); |
| 61 | |
} else { |
| 62 | 0 | throw new NoHandlerDefinedException("no handler found for Array property <" + handledArrayItem.getPropertyName() + "> of Class <" + usurpedClass.getName() + ">."); |
| 63 | |
} |
| 64 | |
} |
| 65 | |
} |
| 66 | 0 | } catch (NoSuchFieldException e) { |
| 67 | 0 | throw new PropertyTypeHandlingException("Unable to handle property <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e); |
| 68 | 11 | } |
| 69 | 11 | return result; |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public Object handle(HandledConstructorArg handledConstructorArg) { |
| 76 | 1 | final Integer ARRAY_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg); |
| 77 | |
|
| 78 | 1 | Class<?> usurpedClass = handledConstructorArg.getConstructorArgClass().getComponentType(); |
| 79 | 1 | Object result = Array.newInstance(usurpedClass, ARRAY_ENTRIES); |
| 80 | 1 | HandledConstructorArg handledArrayItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup()); |
| 81 | 11 | for (int i = 0; i < ARRAY_ENTRIES; i++) { |
| 82 | 10 | if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) { |
| 83 | 10 | Array.set(result, i, handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem)); |
| 84 | |
} else { |
| 85 | 0 | if (usurpedClass.isEnum()) { |
| 86 | 0 | Array.set(result, i, new EnumHandler().handle(handledArrayItem)); |
| 87 | |
} else { |
| 88 | 0 | throw new NoHandlerDefinedException("no handler found for Array constructor arg <#" + handledArrayItem.getConstructorArgOrderingNumber() + "> of Class <" + usurpedClass.getName() + ">."); |
| 89 | |
} |
| 90 | |
} |
| 91 | |
} |
| 92 | 1 | return result; |
| 93 | |
} |
| 94 | |
|
| 95 | |
} |