← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7179: added more reflection utils

 

------------------------------------------------------------
revno: 7179
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2012-06-04 15:38:24 +0200
message:
  added more reflection utils
modified:
  dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java'
--- dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-06-04 13:02:01 +0000
+++ dhis-2/dhis-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata/importers/DefaultIdentifiableObjectImporter.java	2012-06-04 13:38:24 +0000
@@ -753,15 +753,8 @@
                 if ( objects != null && !objects.isEmpty() )
                 {
                     collectionFields.put( field, objects );
-
-                    if ( List.class.isAssignableFrom( field.getType() ) )
-                    {
-                        ReflectionUtils.invokeSetterMethod( field.getName(), object, new ArrayList<Object>() );
-                    }
-                    else if ( Set.class.isAssignableFrom( field.getType() ) )
-                    {
-                        ReflectionUtils.invokeSetterMethod( field.getName(), object, new HashSet<Object>() );
-                    }
+                    Collection<Object> emptyCollection = ReflectionUtils.newCollectionInstance( field.getType() );
+                    ReflectionUtils.invokeSetterMethod( field.getName(), object, emptyCollection );
                 }
             }
         } );
@@ -776,21 +769,7 @@
         for ( Field field : collectionFields.keySet() )
         {
             Collection<Object> collection = collectionFields.get( field );
-            Collection<Object> objects;
-
-            if ( List.class.isAssignableFrom( field.getType() ) )
-            {
-                objects = new ArrayList<Object>();
-            }
-            else if ( Set.class.isAssignableFrom( field.getType() ) )
-            {
-                objects = new HashSet<Object>();
-            }
-            else
-            {
-                log.warn( "Unknown Collection type." );
-                continue;
-            }
+            Collection<Object> objects = ReflectionUtils.newCollectionInstance( field.getType() );
 
             for ( Object idObject : collection )
             {

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2012-06-04 13:02:01 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2012-06-04 13:38:24 +0000
@@ -35,10 +35,7 @@
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.*;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
+import java.util.*;
 
 /**
  * @author Lars Helge Overland
@@ -456,4 +453,20 @@
 
         return fields;
     }
+
+    public static <E> Collection<E> newCollectionInstance( Class<?> clazz )
+    {
+        if ( List.class.isAssignableFrom( clazz ) )
+        {
+            return new ArrayList<E>();
+        }
+        else if ( Set.class.isAssignableFrom( clazz ) )
+        {
+            return new HashSet<E>();
+        }
+        else
+        {
+            throw new RuntimeException( "Unknown Collection type." );
+        }
+    }
 }