You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2021/05/04 11:37:24 UTC

[isis] branch master updated: ISIS-2642: _Generics: consolidate _Collections

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


The following commit(s) were added to refs/heads/master by this push:
     new eaa0944  ISIS-2642: _Generics: consolidate _Collections
eaa0944 is described below

commit eaa0944b66b03915ce8817a8307560141071d3b2
Author: Andi Huber <ah...@apache.org>
AuthorDate: Tue May 4 13:37:07 2021 +0200

    ISIS-2642: _Generics: consolidate _Collections
---
 .../commons/internal/collections/_Collections.java | 96 +++++-----------------
 .../commons/internal/reflection/_Generics.java     | 10 ++-
 .../isis/core/metamodel/facets/FacetedMethod.java  | 10 +--
 3 files changed, 35 insertions(+), 81 deletions(-)

diff --git a/commons/src/main/java/org/apache/isis/commons/internal/collections/_Collections.java b/commons/src/main/java/org/apache/isis/commons/internal/collections/_Collections.java
index 62d239f..9685e6a 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/collections/_Collections.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/collections/_Collections.java
@@ -22,8 +22,6 @@ package org.apache.isis.commons.internal.collections;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Parameter;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -276,14 +274,17 @@ public final class _Collections {
 
     // -- ELEMENT TYPE INFERENCE
 
-    
     public static Optional<Class<?>> inferElementType(final @NonNull Parameter param) {
-        final Class<?> parameterType = param.getType();
-        final Class<?> declaringType = param.getDeclaringExecutable().getDeclaringClass();
-        final Type genericParameterType = param.getParameterizedType();
+        val parameterType = param.getType();
         
-        //TODO use _Generics instead
-        return inferElementType(parameterType, genericParameterType);
+        if (_Collections.isCollectionType(parameterType) 
+                || _Collections.isCanType(parameterType)) {
+            
+            return _Generics.streamGenericTypeArgumentsOfParameter(param)
+                    .findFirst();
+        }
+
+        return Optional.empty();
     }
     
     public static Optional<Class<?>> inferElementType(final @NonNull Method method) {
@@ -300,78 +301,27 @@ public final class _Collections {
         return Optional.empty();
     }
     
-    
     /**
-     * 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)
+     * 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
      */
-    private static Optional<Class<?>> inferElementType(
-            final @Nullable Class<?> collectionType,
-            final @Nullable Type genericType) {
-
-        if(collectionType == null || genericType==null) {
-            return Optional.empty();
-        }
-
-        if(!isCollectionType(collectionType) && !isCanType(collectionType)) {
-            return Optional.empty();
-        }
+    public static Optional<Class<?>> inferElementType(final @NonNull Field field) {
+        
+        val fieldType = field.getType();
         
-        if(genericType instanceof ParameterizedType) {
-            final ParameterizedType parameterizedType = (ParameterizedType) genericType;
-            final Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
-            if(actualTypeArguments.length == 1) {
-                // handle e.g. List<Sometype>
-                final Type actualTypeArgument = actualTypeArguments[0];
-                if(actualTypeArgument instanceof Class) {
-                    return Optional.of((Class<?>) actualTypeArgument);
-                }
-                // also handle e.g. List<Sometype<T>>
-                if(actualTypeArgument instanceof ParameterizedType) {
-                    final Type innerParameterizedType = ((ParameterizedType) actualTypeArgument).getRawType();
-                    if(innerParameterizedType instanceof Class) {
-                        return Optional.of((Class<?>) innerParameterizedType);
-                    }
-                }
-            }
+        if (_Collections.isCollectionType(fieldType) 
+                || _Collections.isCanType(fieldType)) {
+            
+            return _Generics.streamGenericTypeArgumentsOfField(field)
+                    .findFirst();
         }
 
         return Optional.empty();
     }
 
-    /**
-     * @param collectionType
-     * @param genericType
-     * @return optionally the inferred element type, 
-     * based on whether parameter is a (collection or array) and has an infer-able element type
-     */
-    public static Optional<Class<?>> inferElementTypeFromArrayOrCollection(
-            final @Nullable Class<?> collectionType,
-            final @Nullable Type genericType) {
-
-        val fromArray = _Arrays.inferComponentType(collectionType);
-        if(fromArray.isPresent()) {
-            return fromArray;
-        }
-        return _Collections.inferElementType(collectionType, genericType);
-    }
-
-    /**
-     * 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 Optional<Class<?>> inferElementType(final @Nullable Field field) {
-        if(field==null) {
-            return null;
-        }
-        return inferElementType(field.getType(), field.getGenericType());
-    }
-
     // -- TO STRING
 
     public static String toStringJoining(
@@ -386,8 +336,4 @@ public final class _Collections {
         return toStringJoining(collection, "\n");
     }
 
-
-
-    
-
 }
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Generics.java b/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Generics.java
index d551981..cd8c276 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Generics.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/reflection/_Generics.java
@@ -20,6 +20,7 @@ package org.apache.isis.commons.internal.reflection;
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Parameter;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.lang.reflect.TypeVariable;
@@ -93,6 +94,14 @@ public final class _Generics {
         return streamGenericTypeArgumentsOf(method.getDeclaringClass(), method.getGenericReturnType());
     }
     
+    public static Stream<Class<?>> streamGenericTypeArgumentsOfParameter(
+            final @NonNull Parameter param) {
+        return streamGenericTypeArgumentsOf(
+                param.getDeclaringExecutable().getDeclaringClass(), 
+                param.getParameterizedType());
+    }
+    
+    
     // -- HELPER
     
     private static Stream<Class<?>> streamClassesOfType(
@@ -128,5 +137,4 @@ public final class _Generics {
         return Stream.empty();
     }
     
-    
 }
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/FacetedMethod.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/FacetedMethod.java
index 38488c8..92751f2 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/FacetedMethod.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/FacetedMethod.java
@@ -20,12 +20,12 @@
 package org.apache.isis.core.metamodel.facets;
 
 import java.lang.reflect.Method;
-import java.lang.reflect.Type;
 import java.util.Collections;
 import java.util.List;
 
 import org.apache.isis.applib.Identifier;
 import org.apache.isis.applib.id.LogicalType;
+import org.apache.isis.commons.internal.collections._Arrays;
 import org.apache.isis.commons.internal.collections._Collections;
 import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.core.metamodel.commons.StringExtensions;
@@ -112,12 +112,12 @@ public class FacetedMethod extends TypedHolderDefault implements IdentifiedHolde
         for(val param : actionMethod.getParameters()) {
             
             final Class<?> parameterType = param.getType();
-            final Type genericParameterType = param.getParameterizedType();
             
             final FeatureType featureType =
-                    _Collections.inferElementTypeFromArrayOrCollection(parameterType, genericParameterType).isPresent()
-                        ? FeatureType.ACTION_PARAMETER_COLLECTION
-                        : FeatureType.ACTION_PARAMETER_SCALAR;
+                    _Collections.inferElementType(param).isPresent()
+                        || _Arrays.inferComponentType(parameterType).isPresent()
+                    ? FeatureType.ACTION_PARAMETER_COLLECTION
+                    : FeatureType.ACTION_PARAMETER_SCALAR;
 
             val facetedMethodParam = 
                     new FacetedMethodParameter(featureType, declaringType, actionMethod, parameterType);