← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21451: move method utility from Jackson2 introspector to ReflectionUtils

 

------------------------------------------------------------
revno: 21451
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-12-15 08:46:35 +0100
message:
  move method utility from Jackson2 introspector to ReflectionUtils
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.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-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2015-12-15 07:38:12 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/schema/Jackson2PropertyIntrospectorService.java	2015-12-15 07:46:35 +0000
@@ -32,7 +32,6 @@
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
-import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import com.google.common.primitives.Primitives;
@@ -50,7 +49,6 @@
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
@@ -58,8 +56,6 @@
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
-import static org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods;
-
 /**
  * Default PropertyIntrospectorService implementation that uses Reflection and Jackson annotations
  * for reading in properties.
@@ -274,16 +270,9 @@
         return StringUtils.uncapitalize( name );
     }
 
-    private Multimap<String, Method> getMultimap( Class<?> klass )
-    {
-        Multimap<String, Method> methods = ArrayListMultimap.create();
-        Arrays.asList( getUniqueDeclaredMethods( klass ) ).forEach( method -> methods.put( method.getName(), method ) );
-        return methods;
-    }
-
     private List<Property> collectProperties( Class<?> klass )
     {
-        Multimap<String, Method> multimap = getMultimap( klass );
+        Multimap<String, Method> multimap = ReflectionUtils.getMethodsMultimap( klass );
         List<Property> properties = new ArrayList<>();
 
         Map<String, Method> methodMap = multimap.keySet().stream()

=== 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	2015-12-15 07:38:12 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ReflectionUtils.java	2015-12-15 07:46:35 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
 import javassist.util.proxy.ProxyFactory;
 import org.hibernate.collection.spi.PersistentCollection;
 import org.springframework.util.StringUtils;
@@ -42,12 +44,11 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
-import java.util.Map;
 import java.util.Set;
 import java.util.function.Predicate;
+import java.util.stream.Collectors;
 
 /**
  * @author Lars Helge Overland
@@ -331,7 +332,6 @@
     public static boolean isType( Field field, Class<?> clazz )
     {
         Class<?> type = field.getType();
-
         return clazz.isAssignableFrom( type );
     }
 
@@ -380,14 +380,7 @@
     public static List<String> getAllFieldNames( Class<?> klass )
     {
         List<Field> fields = getAllFields( klass );
-        List<String> fieldNames = new ArrayList<>();
-
-        for ( Field field : fields )
-        {
-            fieldNames.add( field.getName() );
-        }
-
-        return fieldNames;
+        return fields.stream().map( Field::getName ).collect( Collectors.toList() );
     }
 
     private static Method _findMethod( Class<?> clazz, String name )
@@ -417,34 +410,6 @@
         return null;
     }
 
-    public static Map<String, Method> getMethodMap( Class<?> klass )
-    {
-        Map<String, Method> methodMap = new HashMap<>();
-        List<Method> methods = getAllMethods( klass );
-
-        for ( Method method : methods )
-        {
-            methodMap.put( method.getName(), method );
-        }
-
-        return methodMap;
-    }
-
-    public static List<Method> getAllMethods( Class<?> clazz )
-    {
-        Class<?> searchType = clazz;
-        List<Method> methods = new ArrayList<>();
-
-        while ( searchType != null )
-        {
-            Method[] methodArray = (searchType.isInterface() ? searchType.getMethods() : searchType.getDeclaredMethods());
-            methods.addAll( Arrays.asList( methodArray ) );
-            searchType = searchType.getSuperclass();
-        }
-
-        return methods;
-    }
-
     @SuppressWarnings( "unchecked" )
     public static <T> T invokeMethod( Object target, Method method, Object... args )
     {
@@ -523,4 +488,28 @@
 
         return klass;
     }
+
+    /**
+     * Get all uniquely declared methods on a given Class, if methods are overriden only the topmost method is returned.
+     *
+     * @param klass Class
+     * @return List of uniquely declared methods
+     */
+    public static List<Method> getMethods( Class<?> klass )
+    {
+        return Arrays.asList( org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods( klass ) );
+    }
+
+    /**
+     * Returns a multimap of the mapping method-name -> [methods]. Useful to find overloaded methods in a class hierarchy.
+     *
+     * @param klass Class
+     * @return Multimap of method-name -> [methods]
+     */
+    public static Multimap<String, Method> getMethodsMultimap( Class<?> klass )
+    {
+        Multimap<String, Method> methods = ArrayListMultimap.create();
+        getMethods( klass ).forEach( method -> methods.put( method.getName(), method ) );
+        return methods;
+    }
 }