View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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   * This is the generic handler for Arrays of types.
38   * 
39   * @author pagregoire
40   */
41  public final class ArrayHandler implements IHandler {
42  
43     
44      /**
45       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
46       */
47      public Object handle(HandledBeanProperty handledBeanProperty) {
48          Object result = null;
49          final Integer ARRAY_ENTRIES = handledBeanProperty.getUsurperGeneratorSetup().getCountCallback().determineCount(handledBeanProperty);
50          try {
51              Field attribute = ReflectionUtils.getField(handledBeanProperty.getTargetObject(), handledBeanProperty.getPropertyName());
52              Class<?> usurpedClass = attribute.getType().getComponentType();
53              result = Array.newInstance(ReflectionUtils.toNotPrimitiveType(usurpedClass), ARRAY_ENTRIES);
54              HandledBeanProperty handledArrayItem = new HandledBeanProperty(handledBeanProperty.getTargetObject(), usurpedClass, handledBeanProperty.getPropertyName(), handledBeanProperty.getUsurperGeneratorSetup());
55              for (int i = 0; i < ARRAY_ENTRIES; i++) {
56                  if (handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
57                      Array.set(result, i, handledBeanProperty.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem));
58                  } else {
59                      if (usurpedClass.isEnum()) {
60                          Array.set(result, i, new EnumHandler().handle(handledArrayItem));
61                      } else {
62                          throw new NoHandlerDefinedException("no handler found for Array property <" + handledArrayItem.getPropertyName() + "> of Class <" + usurpedClass.getName() + ">.");
63                      }
64                  }
65              }
66          } catch (NoSuchFieldException e) {
67              throw new PropertyTypeHandlingException("Unable to handle property <" + handledBeanProperty.getPropertyName() + "(" + handledBeanProperty.getPropertyClass().getName() + ")> from object " + handledBeanProperty.getTargetObject(), e);
68          }
69          return result;
70      }
71  
72      /**
73       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
74       */
75      public Object handle(HandledConstructorArg handledConstructorArg) {
76          final Integer ARRAY_ENTRIES = handledConstructorArg.getUsurperGeneratorSetup().getCountCallback().determineCount(handledConstructorArg);
77  
78          Class<?> usurpedClass = handledConstructorArg.getConstructorArgClass().getComponentType();
79          Object result = Array.newInstance(usurpedClass, ARRAY_ENTRIES);
80          HandledConstructorArg handledArrayItem = new HandledConstructorArg(handledConstructorArg.getTargetConstructor(), usurpedClass, handledConstructorArg.getConstructorArgOrderingNumber(), handledConstructorArg.getUsurperGeneratorSetup());
81          for (int i = 0; i < ARRAY_ENTRIES; i++) {
82              if (handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)) != null) {
83                  Array.set(result, i, handledConstructorArg.getUsurperGeneratorSetup().getAllHandlers().get(new PropertyTypeDefinition(usurpedClass)).handle(handledArrayItem));
84              } else {
85                  if (usurpedClass.isEnum()) {
86                      Array.set(result, i, new EnumHandler().handle(handledArrayItem));
87                  } else {
88                      throw new NoHandlerDefinedException("no handler found for Array constructor arg <#" + handledArrayItem.getConstructorArgOrderingNumber() + "> of Class <" + usurpedClass.getName() + ">.");
89                  }
90              }
91          }
92          return result;
93      }
94  
95  }