← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 14428: cleanup in ReflectionUtils, moved all introspector code to PropertyScannerService

 

------------------------------------------------------------
revno: 14428
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-03-26 12:32:35 +0100
message:
  cleanup in ReflectionUtils, moved all introspector code to PropertyScannerService
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyScannerService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultPropertyScannerService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.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-api/src/main/java/org/hisp/dhis/schema/PropertyScannerService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyScannerService.java	2014-03-21 09:35:30 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/PropertyScannerService.java	2014-03-26 11:32:35 +0000
@@ -28,8 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import org.hisp.dhis.schema.Property;
-
 import java.util.List;
 
 /**

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2014-03-26 11:12:59 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/schema/Schema.java	2014-03-26 11:32:35 +0000
@@ -33,11 +33,14 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
 import org.hisp.dhis.common.DxfNamespaces;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.common.NameableObject;
+import org.springframework.util.StringUtils;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -202,6 +205,42 @@
         this.properties = properties;
     }
 
+    private Map<String, Property> propertyMap = Maps.newHashMap();
+
+    public boolean containsProperty( String name )
+    {
+        if ( StringUtils.isEmpty( name ) )
+        {
+            return false;
+        }
+
+        if ( propertyMap.containsKey( name ) )
+        {
+            return true;
+        }
+
+        for ( Property property : properties )
+        {
+            if ( name.equals( property.getName() ) )
+            {
+                propertyMap.put( name, property );
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    public Property getPropertyByName( String name )
+    {
+        if ( containsProperty( name ) )
+        {
+            return propertyMap.get( name );
+        }
+
+        return null;
+    }
+
     @Override
     public String toString()
     {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultPropertyScannerService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultPropertyScannerService.java	2014-03-26 11:12:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/DefaultPropertyScannerService.java	2014-03-26 11:32:35 +0000
@@ -28,9 +28,20 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
+import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.annotation.Description;
 import org.hisp.dhis.system.util.ReflectionUtils;
+import org.springframework.util.StringUtils;
 
+import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.Collection;
 import java.util.List;
 import java.util.Map;
 
@@ -48,28 +59,111 @@
         return scanClass( klass );
     }
 
-    private List<Property> scanClass( Class<?> klass )
+    // -------------------------------------------------------------------------
+    // Scanning Helpers
+    // -------------------------------------------------------------------------
+
+    private static Map<Class<?>, List<Property>> classMapCache = Maps.newHashMap();
+
+    private static List<Property> scanClass( Class<?> clazz )
     {
+        if ( classMapCache.containsKey( clazz ) )
+        {
+            return classMapCache.get( clazz );
+        }
+
         List<Property> properties = Lists.newArrayList();
-
-        Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( klass );
-
-        // for now, just use the reflection utils directly
-        for ( ReflectionUtils.PropertyDescriptor descriptor : classMap.values() )
+        List<Method> allMethods = ReflectionUtils.getAllMethods( clazz );
+
+        for ( Method method : allMethods )
         {
-            Property property = new Property( descriptor.getMethod() );
-            properties.add( property );
-
-            property.setKlass( descriptor.getClazz() );
-            property.setCollection( descriptor.isCollection() );
-            property.setIdentifiableObject( descriptor.isIdentifiableObject() );
-            property.setName( descriptor.getName() );
-            property.setXmlName( descriptor.getXmlName() );
-            property.setXmlNamespace( descriptor.getXmlNamespace() );
-            property.setXmlCollectionName( descriptor.getXmlCollectionName() );
-            property.setDescription( descriptor.getDescription() );
+            if ( method.isAnnotationPresent( JsonProperty.class ) )
+            {
+                JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
+                Property property = new Property( method );
+
+                String name = jsonProperty.value();
+
+                if ( StringUtils.isEmpty( name ) )
+                {
+                    String[] getters = new String[]{
+                        "is", "has", "get"
+                    };
+
+                    name = method.getName();
+
+                    for ( String getter : getters )
+                    {
+                        if ( name.startsWith( getter ) )
+                        {
+                            name = name.substring( getter.length() );
+                        }
+                    }
+
+                    name = StringUtils.uncapitalize( name );
+                }
+
+                if ( method.isAnnotationPresent( Description.class ) )
+                {
+                    Description description = method.getAnnotation( Description.class );
+                    property.setDescription( description.value() );
+                }
+
+                if ( method.isAnnotationPresent( JacksonXmlProperty.class ) )
+                {
+                    JacksonXmlProperty jacksonXmlProperty = method.getAnnotation( JacksonXmlProperty.class );
+
+                    if ( jacksonXmlProperty.localName().isEmpty() )
+                    {
+                        property.setXmlName( name );
+                    }
+                    else
+                    {
+                        property.setXmlName( jacksonXmlProperty.localName() );
+                    }
+
+                    property.setXmlNamespace( jacksonXmlProperty.namespace() );
+                    property.setXmlAttribute( jacksonXmlProperty.isAttribute() );
+                }
+
+                if ( method.isAnnotationPresent( JacksonXmlElementWrapper.class ) )
+                {
+                    JacksonXmlElementWrapper jacksonXmlElementWrapper = method.getAnnotation( JacksonXmlElementWrapper.class );
+                    property.setXmlCollectionName( jacksonXmlElementWrapper.localName() );
+                }
+
+                property.setName( name );
+                properties.add( property );
+
+                Class<?> returnType = method.getReturnType();
+                property.setKlass( returnType );
+
+                if ( IdentifiableObject.class.isAssignableFrom( returnType ) )
+                {
+                    property.setIdentifiableObject( true );
+                }
+                else if ( Collection.class.isAssignableFrom( returnType ) )
+                {
+                    property.setCollection( true );
+
+                    Type type = method.getGenericReturnType();
+
+                    if ( ParameterizedType.class.isInstance( type ) )
+                    {
+                        ParameterizedType parameterizedType = (ParameterizedType) type;
+                        Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
+
+                        if ( IdentifiableObject.class.isAssignableFrom( klass ) )
+                        {
+                            property.setIdentifiableObject( true );
+                        }
+                    }
+                }
+            }
         }
 
+        classMapCache.put( clazz, properties );
+
         return properties;
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java	2014-03-21 09:35:30 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/filter/DefaultFilterService.java	2014-03-26 11:32:35 +0000
@@ -133,17 +133,17 @@
         }
 
         Map<String, Object> output = Maps.newHashMap();
-        Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( object.getClass() );
+        Schema schema = schemaService.getSchema( object.getClass() );
 
         for ( String key : fieldMap.keySet() )
         {
-            if ( !classMap.containsKey( key ) )
+            if ( !schema.containsProperty( key ) )
             {
                 continue;
             }
 
             Map value = fieldMap.get( key );
-            ReflectionUtils.PropertyDescriptor descriptor = classMap.get( key );
+            Property descriptor = schema.getPropertyByName( key );
 
             Object returned = ReflectionUtils.invokeMethod( object, descriptor.getMethod() );
 
@@ -234,11 +234,11 @@
     private Map<String, Object> getIdentifiableObjectProperties( Object object, List<String> fields )
     {
         Map<String, Object> idProps = Maps.newLinkedHashMap();
-        Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( object.getClass() );
+        Schema schema = schemaService.getSchema( object.getClass() );
 
         for ( String field : fields )
         {
-            ReflectionUtils.PropertyDescriptor descriptor = classMap.get( field );
+            Property descriptor = schema.getPropertyByName( field );
 
             if ( descriptor == null )
             {
@@ -259,17 +259,22 @@
     @SuppressWarnings( "unchecked" )
     private <T extends IdentifiableObject> boolean evaluateWithFilters( T object, Filters filters )
     {
-        Map<String, ReflectionUtils.PropertyDescriptor> classMap = ReflectionUtils.getJacksonClassMap( object.getClass() );
+        Schema schema = schemaService.getSchema( object.getClass() );
 
         for ( String field : filters.getFilters().keySet() )
         {
-            if ( !classMap.containsKey( field ) )
+            if ( !schema.containsProperty( field ) )
             {
                 System.err.println( "Skipping non-existent field: " + field );
                 continue;
             }
 
-            ReflectionUtils.PropertyDescriptor descriptor = classMap.get( field );
+            Property descriptor = schema.getPropertyByName( field );
+
+            if ( descriptor == null )
+            {
+                continue;
+            }
 
             Object value = ReflectionUtils.invokeMethod( object, descriptor.getMethod() );
 

=== 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	2014-03-26 11:12:59 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2014-03-26 11:32:35 +0000
@@ -28,13 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
-import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
-import com.google.common.collect.Maps;
-import org.hisp.dhis.common.IdentifiableObject;
-import org.hisp.dhis.common.annotation.Description;
-import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.system.util.functional.Function1;
 import org.hisp.dhis.system.util.functional.Predicate;
 import org.springframework.util.StringUtils;
@@ -51,7 +44,6 @@
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 
 import static org.hisp.dhis.system.util.PredicateUtils.alwaysTrue;
@@ -505,287 +497,4 @@
             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 StringUtils.isEmpty( xmlProperty.localName() ) ? fieldName : xmlProperty.localName();
-        }
-
-        JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
-
-        if ( jsonProperty != null )
-        {
-            return StringUtils.isEmpty( jsonProperty.value() ) ? fieldName : jsonProperty.value();
-        }
-
-        return fieldName;
-    }
-
-    private static Map<Class<?>, Map<String, PropertyDescriptor>> classMapCache = Maps.newHashMap();
-
-    public static class PropertyDescriptor
-    {
-        private String name;
-
-        private String description;
-
-        private String xmlName;
-
-        private String xmlNamespace;
-
-        private boolean xmlAttribute;
-
-        private String xmlCollectionName;
-
-        private Class<?> clazz;
-
-        private Method method;
-
-        private boolean collection;
-
-        private boolean identifiableObject;
-
-        private PropertyDescriptor( Method method )
-        {
-            this.method = method;
-        }
-
-        @JsonProperty
-        public String getName()
-        {
-            return name;
-        }
-
-        public void setName( String name )
-        {
-            this.name = name;
-        }
-
-        @JsonProperty
-        public String getXmlNamespace()
-        {
-            return xmlNamespace;
-        }
-
-        public void setXmlNamespace( String xmlNamespace )
-        {
-            this.xmlNamespace = xmlNamespace;
-        }
-
-        @JsonProperty
-        public String getDescription()
-        {
-            return description;
-        }
-
-        public void setDescription( String description )
-        {
-            this.description = description;
-        }
-
-        @JsonProperty
-        public String getXmlName()
-        {
-            return xmlName;
-        }
-
-        public void setXmlName( String xmlName )
-        {
-            this.xmlName = xmlName;
-        }
-
-        @JsonProperty
-        public boolean isXmlAttribute()
-        {
-            return xmlAttribute;
-        }
-
-        public void setXmlAttribute( boolean xmlAttribute )
-        {
-            this.xmlAttribute = xmlAttribute;
-        }
-
-        @JsonProperty
-        public String getXmlCollectionName()
-        {
-            return xmlCollectionName;
-        }
-
-        public void setXmlCollectionName( String xmlCollectionName )
-        {
-            this.xmlCollectionName = xmlCollectionName;
-        }
-
-        @JsonProperty
-        public Class<?> getClazz()
-        {
-            return clazz;
-        }
-
-        public void setClazz( Class<?> clazz )
-        {
-            this.clazz = clazz;
-        }
-
-        public Method getMethod()
-        {
-            return method;
-        }
-
-        public void setMethod( Method method )
-        {
-            this.method = method;
-        }
-
-        @JsonProperty
-        public boolean isCollection()
-        {
-            return collection;
-        }
-
-        public void setCollection( boolean collection )
-        {
-            this.collection = collection;
-        }
-
-        @JsonProperty
-        public boolean isIdentifiableObject()
-        {
-            return identifiableObject;
-        }
-
-        public void setIdentifiableObject( boolean identifiableObject )
-        {
-            this.identifiableObject = identifiableObject;
-        }
-
-        @Override
-        public String toString()
-        {
-            return "PropertyDescriptor{" +
-                "name='" + name + '\'' +
-                ", description='" + description + '\'' +
-                ", xmlName='" + xmlName + '\'' +
-                ", xmlAttribute=" + xmlAttribute +
-                ", xmlCollectionName='" + xmlCollectionName + '\'' +
-                ", clazz=" + clazz +
-                ", method=" + method +
-                ", collection=" + collection +
-                ", identifiableObject=" + identifiableObject +
-                '}';
-        }
-    }
-
-    public static Map<String, PropertyDescriptor> getJacksonClassMap( Class<?> clazz )
-    {
-        if ( classMapCache.containsKey( clazz ) )
-        {
-            return classMapCache.get( clazz );
-        }
-
-        Map<String, PropertyDescriptor> output = Maps.newLinkedHashMap();
-        List<Method> allMethods = getAllMethods( clazz );
-
-        for ( Method method : allMethods )
-        {
-            if ( method.isAnnotationPresent( JsonProperty.class ) )
-            {
-                JsonProperty jsonProperty = method.getAnnotation( JsonProperty.class );
-                PropertyDescriptor descriptor = new PropertyDescriptor( method );
-
-                String name = jsonProperty.value();
-
-                if ( StringUtils.isEmpty( name ) )
-                {
-                    String[] getters = new String[]{
-                        "is", "has", "get"
-                    };
-
-                    name = method.getName();
-
-                    for ( String getter : getters )
-                    {
-                        if ( name.startsWith( getter ) )
-                        {
-                            name = name.substring( getter.length() );
-                        }
-                    }
-
-                    name = StringUtils.uncapitalize( name );
-                }
-
-                if ( method.isAnnotationPresent( Description.class ) )
-                {
-                    Description description = method.getAnnotation( Description.class );
-                    descriptor.setDescription( description.value() );
-                }
-
-                if ( method.isAnnotationPresent( JacksonXmlProperty.class ) )
-                {
-                    JacksonXmlProperty jacksonXmlProperty = method.getAnnotation( JacksonXmlProperty.class );
-
-                    if ( jacksonXmlProperty.localName().isEmpty() )
-                    {
-                        descriptor.setXmlName( name );
-                    }
-                    else
-                    {
-                        descriptor.setXmlName( jacksonXmlProperty.localName() );
-                    }
-
-                    descriptor.setXmlNamespace( jacksonXmlProperty.namespace() );
-                    descriptor.setXmlAttribute( jacksonXmlProperty.isAttribute() );
-                }
-
-                if ( method.isAnnotationPresent( JacksonXmlElementWrapper.class ) )
-                {
-                    JacksonXmlElementWrapper jacksonXmlElementWrapper = method.getAnnotation( JacksonXmlElementWrapper.class );
-                    descriptor.setXmlCollectionName( jacksonXmlElementWrapper.localName() );
-                }
-
-                descriptor.setName( name );
-                output.put( name, descriptor );
-
-                Class<?> returnType = method.getReturnType();
-                descriptor.setClazz( returnType );
-
-                if ( IdentifiableObject.class.isAssignableFrom( returnType ) )
-                {
-                    descriptor.setIdentifiableObject( true );
-                }
-                else if ( Collection.class.isAssignableFrom( returnType ) )
-                {
-                    descriptor.setCollection( true );
-
-                    Type type = method.getGenericReturnType();
-
-                    if ( ParameterizedType.class.isInstance( type ) )
-                    {
-                        ParameterizedType parameterizedType = (ParameterizedType) type;
-                        Class<?> klass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
-
-                        if ( IdentifiableObject.class.isAssignableFrom( klass ) )
-                        {
-                            descriptor.setIdentifiableObject( true );
-                        }
-                    }
-                }
-            }
-        }
-
-        classMapCache.put( clazz, output );
-
-        return output;
-    }
 }