=== 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 invokeMethod( Object target, Method method, Object... args ) { try @@ -409,7 +416,7 @@ } } - @SuppressWarnings("unchecked") + @SuppressWarnings( "unchecked" ) public static 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; + } }