This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 1c957f1befc5e0ba86cb27993491e9be49715c4e
Author: Andi Huber <ahu...@apache.org>
AuthorDate: Thu Apr 12 10:59:50 2018 +0200

    ISIS-1932 consolidate element type inference
---
 .../isis/applib/internal/collections/_Arrays.java  |  4 +-
 .../applib/internal/collections/_Collections.java  | 48 +++++++++++++++++
 .../facets/actcoll/typeof/TypeOfFacet.java         | 13 ++---
 .../core/metamodel/services/ServicesInjector.java  |  9 ++--
 .../core/metamodel/specloader/CollectionUtils.java | 60 +---------------------
 5 files changed, 60 insertions(+), 74 deletions(-)

diff --git 
a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
 
b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
index 8009627..4c59969 100644
--- 
a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
+++ 
b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Arrays.java
@@ -55,14 +55,14 @@ public class _Arrays {
         return _Collections.isCollectionType(cls) || _Arrays.isArrayType(cls);
     }
        
-    // -- ELEMENT TYPE INFERENCE
+    // -- COMPONENT TYPE INFERENCE
     
     /**
      * Returns the inferred element type of the specified array type 
      * @param type of the array for which to infer the element type 
      * @return inferred type or null if inference fails
      */
-    public static @Nullable Class<?> inferElementTypeIfAny(@Nullable final 
Class<?> arrayType) {
+    public static @Nullable Class<?> inferComponentTypeIfAny(@Nullable final 
Class<?> arrayType) {
         if(!isArrayType(arrayType)) {
             return null;
         }
diff --git 
a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Collections.java
 
b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Collections.java
index 80aef7e..9d14b39 100644
--- 
a/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Collections.java
+++ 
b/core/applib/src/main/java/org/apache/isis/applib/internal/collections/_Collections.java
@@ -19,6 +19,9 @@
 
 package org.apache.isis.applib.internal.collections;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -209,6 +212,51 @@ public class _Collections {
        
        // -- ELEMENT TYPE INFERENCE
        
+    /**
+     * If the {@code collectionType} represents a collection then returns 
returns the inferred element type of the 
+     * specified {@code genericType}     
+     * @param collectionType
+     * @param genericType as associated with {@code collectionType} (as 
available for fields or method parameters)
+     * @return inferred type or null if inference fails
+     */
+    public static @Nullable Class<?> inferElementTypeIfAny(
+               @Nullable final Class<?> collectionType, 
+               @Nullable final Type genericType) {
+       
+       if(collectionType == null || genericType==null) {
+               return null;
+       }
+       
+       if(!isCollectionType(collectionType)) {
+               return null;
+       }
+
+        if(genericType instanceof ParameterizedType) {
+            final ParameterizedType parameterizedType = (ParameterizedType) 
genericType;
+            final Type[] actualTypeArguments = 
parameterizedType.getActualTypeArguments();
+            if(actualTypeArguments.length == 1) {
+                final Type actualTypeArgument = actualTypeArguments[0];
+                if(actualTypeArgument instanceof Class) {
+                    final Class<?> actualType = (Class<?>) actualTypeArgument;
+                    return actualType;
+                }
+            }
+        }
+        
+        return null;
+    }
+       
+    /**
+     * If the {@code field} represents a collection then returns the inferred 
element type for this collection (if any).
+     *   
+     * @param field
+     * @return inferred type or null if inference fails
+     */
+    public static @Nullable Class<?> inferElementTypeIfAny(@Nullable final 
Field field) {
+               
+        return inferElementTypeIfAny(field.getType(), field.getGenericType());
+    }
+    
        // --
        
 }
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actcoll/typeof/TypeOfFacet.java
 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actcoll/typeof/TypeOfFacet.java
index bf18c01..9614100 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actcoll/typeof/TypeOfFacet.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/actcoll/typeof/TypeOfFacet.java
@@ -30,7 +30,6 @@ import org.apache.isis.applib.internal.collections._Arrays;
 import org.apache.isis.applib.internal.collections._Collections;
 import org.apache.isis.core.metamodel.facetapi.FacetHolder;
 import org.apache.isis.core.metamodel.facets.SingleClassValueFacet;
-import org.apache.isis.core.metamodel.specloader.CollectionUtils;
 import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
 
 /**
@@ -108,7 +107,7 @@ public interface TypeOfFacet extends SingleClassValueFacet {
                 final FacetHolder holder,
                 final Class<?> type,
                 final SpecificationLoader specificationLoader) {
-            final Class<?> elementType = _Arrays.inferElementTypeIfAny(type);
+            final Class<?> elementType = _Arrays.inferComponentTypeIfAny(type);
             return elementType != null
                     ? new TypeOfFacetInferredFromArray(elementType, holder, 
specificationLoader)
                     : null;
@@ -121,13 +120,9 @@ public interface TypeOfFacet extends SingleClassValueFacet 
{
                 final Type genericParameterType,
                 final SpecificationLoader specificationLoader) {
                
-               if(!_Collections.isCollectionType(parameterType)) {
-                return null;
-            }
-               
-            final Class<?> actualType = 
CollectionUtils.inferElementTypeFromGenericType(genericParameterType);
-            return actualType != null
-                    ? new TypeOfFacetInferredFromGenerics(actualType, holder, 
specificationLoader)
+            final Class<?> elementType = 
_Collections.inferElementTypeIfAny(parameterType, genericParameterType);
+            return elementType != null
+                    ? new TypeOfFacetInferredFromGenerics(elementType, holder, 
specificationLoader)
                     : null;
         }
     }
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/ServicesInjector.java
 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/ServicesInjector.java
index 3f52cb4..fb372e0 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/ServicesInjector.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/services/ServicesInjector.java
@@ -44,7 +44,6 @@ import 
org.apache.isis.core.metamodel.exceptions.MetaModelException;
 import 
org.apache.isis.core.metamodel.services.configinternal.ConfigurationServiceInternal;
 import 
org.apache.isis.core.metamodel.services.persistsession.PersistenceSessionServiceInternal;
 import org.apache.isis.core.metamodel.spec.InjectorMethodEvaluator;
-import org.apache.isis.core.metamodel.specloader.CollectionUtils;
 import 
org.apache.isis.core.metamodel.specloader.InjectorMethodEvaluatorDefault;
 import org.apache.isis.core.metamodel.specloader.SpecificationLoader;
 import org.apache.isis.core.runtime.authentication.AuthenticationManager;
@@ -285,9 +284,9 @@ public class ServicesInjector implements 
ApplicationScopedComponent {
         }
 
         // inject matching services into a field of type Collection<T> if a 
generic type T is present
-        CollectionUtils.ifIsCollectionWithGenericTypeThen(field, 
(elementType)->{
-               
-               @SuppressWarnings("unchecked")
+        final Class<?> elementType = _Collections.inferElementTypeIfAny(field);
+               if(elementType!=null) {
+                       @SuppressWarnings("unchecked")
                        final Class<? extends Collection<Object>> 
collectionTypeToBeInjected =
                                (Class<? extends Collection<Object>>) 
typeToBeInjected;
                
@@ -297,7 +296,7 @@ public class ServicesInjector implements 
ApplicationScopedComponent {
                       
.collect(_Collections.toUnmodifiableOfType(collectionTypeToBeInjected));
              
              invokeInjectorField(field, object, collectionOfServices);
-        });
+               }
         
         for (final Object service : services) {
             final Class<?> serviceClass = service.getClass();
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/CollectionUtils.java
 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/CollectionUtils.java
index b0ea59f..ce23a27 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/CollectionUtils.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/specloader/CollectionUtils.java
@@ -19,10 +19,7 @@
 
 package org.apache.isis.core.metamodel.specloader;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
-import java.util.function.Consumer;
 
 import javax.annotation.Nullable;
 
@@ -51,67 +48,14 @@ public final class CollectionUtils {
     public static boolean isParamCollection(
                @Nullable final Class<?> parameterType,
                @Nullable final Type genericParameterType) {
-       if(_Arrays.inferElementTypeIfAny(parameterType) != null) {
+       if(_Arrays.inferComponentTypeIfAny(parameterType) != null) {
                return true;
        }
-       if(_Collections.isCollectionType(parameterType) && 
inferElementTypeFromGenericType(genericParameterType)!=null) {
+       if(_Collections.inferElementTypeIfAny(parameterType, 
genericParameterType)!=null) {
                return true;
        }
        return false;
     }
 
 
-
-    // -- ELEMENT TYPE INFERENCE (FROM GENERIC TYPE)
-    
-    /**
-     * Returns the inferred element type of the specified array type
-     * @param collectionType
-     * @param genericParameterType
-     * @return inferred type or null if inference fails
-     */
-    public static @Nullable Class<?> inferElementTypeFromGenericType(@Nullable 
final Type genericType) {
-
-       if(genericType==null) {
-               return null;
-       }
-
-        if(genericType instanceof ParameterizedType) {
-            final ParameterizedType parameterizedType = (ParameterizedType) 
genericType;
-            final Type[] actualTypeArguments = 
parameterizedType.getActualTypeArguments();
-            if(actualTypeArguments.length == 1) {
-                final Type actualTypeArgument = actualTypeArguments[0];
-                if(actualTypeArgument instanceof Class) {
-                    final Class<?> actualType = (Class<?>) actualTypeArgument;
-                    return actualType;
-                }
-            }
-        }
-        
-        return null;
-    }
-
-    // -- ELEMENT TYPE INFERENCE (FROM FIELD)
-    
-    /**
-        * If field is of type (or a sub-type of) Collection&lt;T&gt; with 
generic type T present, 
-        * then call action with the element type.
-        * @param field
-        * @param action
-        */
-    public static void ifIsCollectionWithGenericTypeThen(Field field, 
Consumer<Class<?>> action) {
-               
-               final Class<?> fieldType = field.getType();
-               
-               if(_Collections.isCollectionType(fieldType)) {
-                       
-                       final Class<?> elementType = 
inferElementTypeFromGenericType(field.getGenericType());
-                       
-                       if(elementType!=null) {
-                               action.accept(elementType);
-                       }
-        }
-               
-       }
-
 }

-- 
To stop receiving notification emails like this one, please contact
ahu...@apache.org.

Reply via email to