← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 16427: error handling if more than one getter is found for a property

 

------------------------------------------------------------
revno: 16427
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2014-08-16 22:59:51 +0700
message:
  error handling if more than one getter is found for a property
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/NodePropertyIntrospectorService.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-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/NodePropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/NodePropertyIntrospectorService.java	2014-08-16 15:11:14 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/NodePropertyIntrospectorService.java	2014-08-16 15:59:51 +0000
@@ -31,6 +31,8 @@
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 import com.google.common.primitives.Primitives;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
 import org.hisp.dhis.node.annotation.NodeAnnotation;
@@ -45,6 +47,7 @@
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -54,6 +57,8 @@
  */
 public class NodePropertyIntrospectorService extends AbstractPropertyIntrospectorService
 {
+    private static final Log log = LogFactory.getLog( NodePropertyIntrospectorService.class );
+
     @Override
     protected Map<String, Property> scanClass( Class<?> klass )
     {
@@ -204,6 +209,7 @@
     private Method getMethodWithPrefix( Class<?> klass, Field field, List<String> prefixes, boolean includeType )
     {
         String name = StringUtils.capitalize( field.getName() );
+        List<Method> methods = new ArrayList<>();
 
         for ( String prefix : prefixes )
         {
@@ -213,7 +219,7 @@
 
                 if ( method != null )
                 {
-                    return method;
+                    methods.add( method );
                 }
             }
             catch ( NoSuchMethodException ignored )
@@ -221,6 +227,13 @@
             }
         }
 
-        return null;
+        // TODO should we just return null in this case? if this happens, its clearly a mistake
+        if ( methods.size() > 1 )
+        {
+            log.error( "More than one method found for field " + field.getName() + " on class " + klass.getName()
+                + ", Methods: " + methods + ". Using method: " + methods.get( 0 ).getName() + "." );
+        }
+
+        return methods.isEmpty() ? null : methods.get( 0 );
     }
 }