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/12/23 10:24:43 UTC
[isis] branch master updated: ISIS-2930: cleaning up code around generic type arg detection
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 1d909d8 ISIS-2930: cleaning up code around generic type arg detection
1d909d8 is described below
commit 1d909d8d076fbb8312fc36b06c8b2174e219f86a
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Dec 23 11:24:30 2021 +0100
ISIS-2930: cleaning up code around generic type arg detection
---
.../commons/internal/collections/_Collections.java | 57 ++++++++--------
.../commons/internal/reflection/_Generics.java | 10 +++
.../commons/internal/reflection/GenericsTest.java | 76 ++++++++++++++++++++++
.../facets/actcoll/typeof/TypeOfFacet.java | 51 +++++++++------
.../CollectionAnnotationFacetFactory.java | 42 ++++--------
.../javautilcollection/CollectionFacetFactory.java | 28 ++------
6 files changed, 162 insertions(+), 102 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 cf8a4c4..201592e 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
@@ -271,20 +271,28 @@ public final class _Collections {
// -- ELEMENT TYPE INFERENCE
/**
+ * Optionally returns the inferred element type for given {@code cls}, based on whether
+ * it represents a collection and inference is possible.
+ */
+ public static Optional<Class<?>> inferElementType(final @NonNull Class<?> cls) {
+ return _Collections.isCollectionType(cls)
+ || _Collections.isCanType(cls)
+ ? _Generics.streamGenericTypeArgumentsOfType(cls)
+ .findFirst()
+ : Optional.empty();
+ }
+
+ /**
* Optionally returns the inferred element type for given {@code param}, based on whether
* it represents a collection and inference is possible.
*/
public static Optional<Class<?>> inferElementType(final @NonNull Parameter param) {
val parameterType = param.getType();
-
- if (_Collections.isCollectionType(parameterType)
- || _Collections.isCanType(parameterType)) {
-
- return _Generics.streamGenericTypeArgumentsOfParameter(param)
- .findFirst();
- }
-
- return Optional.empty();
+ return _Collections.isCollectionType(parameterType)
+ || _Collections.isCanType(parameterType)
+ ? _Generics.streamGenericTypeArgumentsOfParameter(param)
+ .findFirst()
+ : Optional.empty();
}
/**
@@ -293,17 +301,12 @@ public final class _Collections {
* it represents a collection and inference is possible.
*/
public static Optional<Class<?>> inferElementType(final @NonNull Method method) {
-
val returnType = method.getReturnType();
-
- if (_Collections.isCollectionType(returnType)
- || _Collections.isCanType(returnType)) {
-
- return _Generics.streamGenericTypeArgumentsOfMethodReturnType(method)
- .findFirst();
- }
-
- return Optional.empty();
+ return _Collections.isCollectionType(returnType)
+ || _Collections.isCanType(returnType)
+ ? _Generics.streamGenericTypeArgumentsOfMethodReturnType(method)
+ .findFirst()
+ : Optional.empty();
}
/**
@@ -311,17 +314,12 @@ public final class _Collections {
* it represents a collection and inference is possible.
*/
public static Optional<Class<?>> inferElementType(final @NonNull Field field) {
-
val fieldType = field.getType();
-
- if (_Collections.isCollectionType(fieldType)
- || _Collections.isCanType(fieldType)) {
-
- return _Generics.streamGenericTypeArgumentsOfField(field)
- .findFirst();
- }
-
- return Optional.empty();
+ return _Collections.isCollectionType(fieldType)
+ || _Collections.isCanType(fieldType)
+ ? _Generics.streamGenericTypeArgumentsOfField(field)
+ .findFirst()
+ : Optional.empty();
}
// -- TO STRING
@@ -338,4 +336,5 @@ 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 041d9ed..502dadb 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
@@ -79,6 +79,16 @@ public final class _Generics {
// -- SHORTCUTS
/**
+ * Returns a Stream of the actual type arguments for given type {@code cls}.
+ * @implNote always returns {@link Stream#empty()}, as we don't know how to do this
+ */
+ public static Stream<Class<?>> streamGenericTypeArgumentsOfType(
+ final @NonNull Class<?> cls) {
+ // maybe the best one could do is to extract any bounds on the type argument
+ return Stream.empty();
+ }
+
+ /**
* Returns a Stream of the actual type arguments for given {@code field}.
*/
public static Stream<Class<?>> streamGenericTypeArgumentsOfField(
diff --git a/commons/src/test/java/org/apache/isis/commons/internal/reflection/GenericsTest.java b/commons/src/test/java/org/apache/isis/commons/internal/reflection/GenericsTest.java
new file mode 100644
index 0000000..2ff5549
--- /dev/null
+++ b/commons/src/test/java/org/apache/isis/commons/internal/reflection/GenericsTest.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.isis.commons.internal.reflection;
+
+import java.util.ArrayList;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal._Constants;
+
+import lombok.val;
+
+class GenericsTest {
+
+ Class<?> typeUnderTest;
+ Can<String> fieldUnderTest;
+ Can<String> methodUnderTest(){ return null; };
+
+ @BeforeEach
+ void setUp() throws Exception {
+ typeUnderTest = (new ArrayList<String>()).getClass();
+ }
+
+ @Test
+ void streamGenericTypeArgumentsOfType() {
+
+ val actualTypeArgs = _Generics.streamGenericTypeArgumentsOfType(typeUnderTest)
+ .collect(Can.toCan());
+
+ // impossible, so empty
+ assertEquals(Can.empty(), actualTypeArgs);
+ }
+
+ @Test
+ void streamGenericTypeArgumentsOfMethodReturnType() throws NoSuchMethodException, SecurityException {
+
+ val methodUnderTest = this.getClass().getDeclaredMethod("methodUnderTest", _Constants.emptyClasses);
+
+ val actualTypeArgs = _Generics.streamGenericTypeArgumentsOfMethodReturnType(methodUnderTest)
+ .collect(Can.toCan());
+
+ assertEquals(Can.of(String.class), actualTypeArgs);
+ }
+
+ @Test
+ void streamGenericTypeArgumentsOfField() throws NoSuchFieldException, SecurityException {
+
+ val fieldUnderTest = this.getClass().getDeclaredField("fieldUnderTest");
+
+ val actualTypeArgs = _Generics.streamGenericTypeArgumentsOfField(fieldUnderTest)
+ .collect(Can.toCan());
+
+ assertEquals(Can.of(String.class), actualTypeArgs);
+ }
+
+}
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 2ea1008..8be3e85 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
@@ -42,16 +42,15 @@ public interface TypeOfFacet extends SingleClassValueFacet {
// -- FACTORIES
- static TypeOfFacet inferredFromArray(
- final Class<?> elementType,
+ static Optional<TypeOfFacet> inferFromObjectType(
+ final Class<?> cls,
final FacetHolder holder) {
- return new TypeOfFacetFromArray(elementType, holder);
- }
- static TypeOfFacet inferredFromGenerics(
- final Class<?> elementType,
- final FacetHolder holder) {
- return new TypeOfFacetFromGenerics(elementType, holder);
+ return _Arrays.isArrayType(cls)
+ ? _Arrays.inferComponentType(cls)
+ .map(elementType->inferredFromArray(elementType, holder))
+ : _Collections.inferElementType(cls)
+ .map(elementType->inferredFromGenerics(elementType, holder));
}
static Optional<TypeOfFacet> inferFromParameterType(
@@ -60,13 +59,11 @@ public interface TypeOfFacet extends SingleClassValueFacet {
val paramType = param.getType();
- if (_Arrays.isArrayType(paramType)) {
- return _Arrays.inferComponentType(paramType)
- .map(elementType->inferredFromArray(elementType, holder));
- }
-
- return _Collections.inferElementType(param)
- .map(elementType->inferredFromGenerics(elementType, holder));
+ return _Arrays.isArrayType(paramType)
+ ? _Arrays.inferComponentType(paramType)
+ .map(elementType->inferredFromArray(elementType, holder))
+ : _Collections.inferElementType(paramType)
+ .map(elementType->inferredFromGenerics(elementType, holder));
}
static Optional<TypeOfFacet> inferFromMethodReturnType(
@@ -75,13 +72,25 @@ public interface TypeOfFacet extends SingleClassValueFacet {
val returnType = method.getReturnType();
- if (_Arrays.isArrayType(returnType)) {
- return _Arrays.inferComponentType(returnType)
- .map(elementType->inferredFromArray(elementType, holder));
- }
+ return _Arrays.isArrayType(returnType)
+ ? _Arrays.inferComponentType(returnType)
+ .map(elementType->inferredFromArray(elementType, holder))
+ : _Collections.inferElementType(returnType)
+ .map(elementType->inferredFromGenerics(elementType, holder));
+ }
+
+ // -- INTERNAL
- return _Collections.inferElementType(method)
- .map(elementType->inferredFromGenerics(elementType, holder));
+ private static TypeOfFacet inferredFromArray(
+ final Class<?> elementType,
+ final FacetHolder holder) {
+ return new TypeOfFacetFromArray(elementType, holder);
+ }
+
+ private static TypeOfFacet inferredFromGenerics(
+ final Class<?> elementType,
+ final FacetHolder holder) {
+ return new TypeOfFacetFromGenerics(elementType, holder);
}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/collection/CollectionAnnotationFacetFactory.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/collection/CollectionAnnotationFacetFactory.java
index 638d7ae..cd4ee72 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/collection/CollectionAnnotationFacetFactory.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/collection/CollectionAnnotationFacetFactory.java
@@ -25,9 +25,7 @@ import javax.inject.Inject;
import org.apache.isis.applib.annotation.Collection;
import org.apache.isis.applib.annotation.SemanticsOf;
import org.apache.isis.applib.events.domain.CollectionDomainEvent;
-import org.apache.isis.commons.internal.base._Optionals;
import org.apache.isis.commons.internal.collections._Collections;
-import org.apache.isis.commons.internal.reflection._Generics;
import org.apache.isis.core.metamodel.context.MetaModelContext;
import org.apache.isis.core.metamodel.facetapi.FeatureType;
import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
@@ -163,45 +161,27 @@ extends FacetFactoryAbstract {
val facetHolder = processMethodContext.getFacetHolder();
val method = processMethodContext.getMethod();
+// _Debug.onCondition(method.getName().equals("getRoles"), ()->{
+// System.err.printf("!!!!!!!!!! %s%n", method.getName());
+// });
+
val methodReturnType = method.getReturnType();
if (!_Collections.isCollectionOrArrayType(methodReturnType)) {
return;
}
- addFacetIfPresent(_Optionals.orNullable(
-
+ addFacetIfPresent(
// check for @Collection(typeOf=...)
TypeOfFacetForCollectionAnnotation
.create(collectionIfAny, facetHolder)
-
- ,
-
- // else infer from return type
- ()-> method.getReturnType().isArray()
- ? TypeOfFacet.inferredFromArray(
- method.getReturnType().getComponentType(),
- facetHolder)
- : null
- ,
-
- // else infer from generic return type
- ()->inferFromGenericReturnType(processMethodContext)
- .orElse(null)
-
- ));
+ .or(
+ // else infer from return type
+ ()-> TypeOfFacet.inferFromMethodReturnType(
+ method,
+ facetHolder))
+ );
}
- private Optional<TypeOfFacet> inferFromGenericReturnType(final ProcessMethodContext processMethodContext) {
-
- val facetHolder = processMethodContext.getFacetHolder();
- val method = processMethodContext.getMethod();
-
- return _Generics.streamGenericTypeArgumentsOfMethodReturnType(method)
- .findFirst()
- .map(elementType->TypeOfFacet.inferredFromGenerics(elementType, facetHolder));
- }
-
-
}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/javautilcollection/CollectionFacetFactory.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/javautilcollection/CollectionFacetFactory.java
index e092f05..3a216ed 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/javautilcollection/CollectionFacetFactory.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/facets/collections/javautilcollection/CollectionFacetFactory.java
@@ -38,33 +38,19 @@ extends FacetFactoryAbstract {
}
@Override
- public void process(final ProcessClassContext processClassContaxt) {
+ public void process(final ProcessClassContext processClassContext) {
- if (_Collections.isCollectionType(processClassContaxt.getCls())) {
- processCollectionType(processClassContaxt);
- } else if (_Arrays.isArrayType(processClassContaxt.getCls())) {
- processAsArrayType(processClassContaxt);
- }
-
- }
-
- // -- HELPER
-
- private void processCollectionType(final ProcessClassContext processClassContext) {
val cls = processClassContext.getCls();
val facetHolder = processClassContext.getFacetHolder();
- facetHolder.addFacet(new JavaCollectionFacet(facetHolder));
- //TODO facetHolder.addFacet(TypeOfFacet.inferredFromGenerics(Object.class, facetHolder));
- }
+ if (_Collections.isCollectionType(cls)) {
+ addFacet(new JavaCollectionFacet(facetHolder));
+ } else if (_Arrays.isArrayType(cls)) {
+ addFacet(new JavaArrayFacet(facetHolder));
+ }
- private void processAsArrayType(final ProcessClassContext processClassContext) {
- val cls = processClassContext.getCls();
- val facetHolder = processClassContext.getFacetHolder();
+ addFacetIfPresent(TypeOfFacet.inferFromObjectType(cls, facetHolder));
- facetHolder.addFacet(new JavaArrayFacet(facetHolder));
- facetHolder.addFacet(TypeOfFacet.inferredFromArray(cls.getComponentType(), facetHolder));
}
-
}