← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13916: added new helper to ReflectionUtils, getJacksonAlias, used to get jackson alias for a getter.

 

------------------------------------------------------------
revno: 13916
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-02-03 15:04:46 +0700
message:
  added new helper to ReflectionUtils, getJacksonAlias, used to get jackson alias for a getter.
modified:
  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-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	2013-10-25 19:53:21 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-02-03 08:04:46 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import org.hisp.dhis.system.util.functional.Function1;
 import org.hisp.dhis.system.util.functional.Predicate;
 import org.springframework.util.StringUtils;
@@ -226,20 +228,25 @@
 
     public static Method findGetterMethod( String fieldName, Object target )
     {
+        return findGetterMethod( fieldName, target.getClass() );
+    }
+
+    public static Method findGetterMethod( String fieldName, Class<?> clazz )
+    {
         String[] getterNames = new String[]{
             "get",
             "is",
             "has"
         };
 
-        Field field = _findField( target.getClass(), StringUtils.uncapitalize( fieldName ) );
+        Field field = _findField( clazz, StringUtils.uncapitalize( fieldName ) );
         Method method;
 
         if ( field != null )
         {
             for ( String getterName : getterNames )
             {
-                method = _findMethod( target.getClass(), getterName + StringUtils.capitalize( field.getName() ) );
+                method = _findMethod( clazz, getterName + StringUtils.capitalize( field.getName() ) );
 
                 if ( method != null )
                 {
@@ -392,7 +399,7 @@
         return methods;
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public static <T> T invokeMethod( Object target, Method method, Object... args )
     {
         try
@@ -409,7 +416,7 @@
         }
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings( "unchecked" )
     public static <T> T getFieldObject( Field field, T target )
     {
         return (T) invokeGetterMethod( field.getName(), target );
@@ -487,4 +494,30 @@
             throw new RuntimeException( "Unknown Collection type." );
         }
     }
+
+    public static String getJacksonAlias( String fieldName, Class<?> clazz )
+    {
+        Method method = findGetterMethod( fieldName, clazz );
+
+        if ( method == null )
+        {
+            return fieldName;
+        }
+
+        JacksonXmlProperty xmlProperty = method.getAnnotation( JacksonXmlProperty.class );
+
+        if ( xmlProperty != null )
+        {
+            return xmlProperty.localName();
+        }
+
+        JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
+
+        if ( jsonProperty != null )
+        {
+            return jsonProperty.value();
+        }
+
+        return null;
+    }
 }