You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/08/27 08:30:52 UTC

[causeway] branch 3556-generic.bound.action updated: CAUSEWAY-3556: completely ignore bridge methods

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

ahuber pushed a commit to branch 3556-generic.bound.action
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/3556-generic.bound.action by this push:
     new 54a57a0d11 CAUSEWAY-3556: completely ignore bridge methods
54a57a0d11 is described below

commit 54a57a0d11ecf1b85f678d53dac654329c0ec0e7
Author: andi-huber <ah...@apache.org>
AuthorDate: Sun Aug 27 10:30:48 2023 +0200

    CAUSEWAY-3556: completely ignore bridge methods
---
 .../commons/internal/reflection/_ClassCache.java   |  5 +-
 .../commons/internal/reflection/_Reflect.java      | 81 +++++++++++++++++++++-
 .../metamodel/facets/FacetFactoryTestAbstract.java | 13 +++-
 .../ParameterAnnotationFacetFactoryTest.java       | 23 +++++-
 4 files changed, 116 insertions(+), 6 deletions(-)

diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
index dda6a7c8bb..171840687a 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_ClassCache.java
@@ -276,7 +276,10 @@ public final class _ClassCache implements AutoCloseable {
 
                 // process public only
                 for(val method : publicMethods) {
-                    if(Modifier.isStatic(method.getModifiers())) continue;
+                    if(Modifier.isStatic(method.getModifiers())
+                            || method.isBridge()) {
+                        continue;
+                    }
 
                     val key = MethodKey.of(type, method);
                     putIntoMapHonoringOverridingRelation(model.publicMethodsByKey, key, method);
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_Reflect.java b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_Reflect.java
index 8a32e6b4df..ed28f4b38d 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_Reflect.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/reflection/_Reflect.java
@@ -47,10 +47,12 @@ import org.springframework.util.ClassUtils;
 
 import org.apache.causeway.commons.collections.Can;
 import org.apache.causeway.commons.functional.Try;
+import org.apache.causeway.commons.internal.assertions._Assert;
 import org.apache.causeway.commons.internal.base._NullSafe;
 import org.apache.causeway.commons.internal.base._Strings;
 import org.apache.causeway.commons.internal.collections._Arrays;
 import org.apache.causeway.commons.internal.context._Context;
+import org.apache.causeway.commons.internal.exceptions._Exceptions;
 import org.apache.causeway.commons.internal.functions._Predicates;
 
 import lombok.NonNull;
@@ -108,11 +110,42 @@ public final class _Reflect {
         return shareSameTypeHierarchy(a.getReturnType(), b.getReturnType());
     }
 
+    /**
+     * Weak variant of {@link #methodsSame(Method, Method)},
+     * that relaxes parameter checks,
+     * if generic parameter bounds are detected on any of the provided methods
+     */
+    public boolean methodsWeaklySame(final Method a, final Method b) {
+        if(!a.getName().equals(b.getName())) {
+            return false;
+        }
+        if(a.getParameterCount()!=b.getParameterCount()) {
+            return false;
+        }
+        if(hasGenericParam(a)
+                || hasGenericParam(b)) {
+            if(!_Arrays.testAllMatch(a.getParameters(), b.getParameters(),
+                    (p1, p2)->shareSameTypeHierarchy(p1.getType(), p2.getType()))) {
+                return false;
+            }
+        } else {
+            if(!_Arrays.testAllMatch(a.getParameters(), b.getParameters(),
+                    (p1, p2)->p1.getType().equals(p2.getType()))) {
+                return false;
+            }
+        }
+        return shareSameTypeHierarchy(a.getReturnType(), b.getReturnType());
+    }
+
     public boolean shareSameTypeHierarchy(final @NonNull Class<?> a, final @NonNull Class<?> b) {
         return a.isAssignableFrom(b)
                 || b.isAssignableFrom(a);
     }
 
+    public boolean shareSameTypeHierarchy(final @NonNull Method a, final @NonNull Method b) {
+        return shareSameTypeHierarchy(a.getDeclaringClass(), b.getDeclaringClass());
+    }
+
     /**
      * If a and b are related, such that one overrides the other,
      * that one which is overriding the other is returned.
@@ -304,7 +337,8 @@ public final class _Reflect {
 
         return streamTypeHierarchy(type, InterfacePolicy.INCLUDE)
                 .filter(t->!t.equals(Object.class)) // do not process Object class.
-                .flatMap(t->streamMethods(t, ignoreAccess));
+                .flatMap(t->streamMethods(t, ignoreAccess))
+                .filter(m->!m.isBridge()); // don't include bridge methods
     }
 
     // -- SUPER CLASSES
@@ -815,4 +849,49 @@ public final class _Reflect {
         return false;
     }
 
+    /**
+     * Will fail if methods are spread across different branches of the shared type hierarchy.
+     * @param methods assumed to pass checks #methodsSame and #shareSameTypeHierarchy
+     * @return the most specific with the type hierarchy
+     */
+    public static Optional<Method> mostSpecificMethodOf(
+            final @NonNull Can<Method> methods) {
+
+        switch(methods.getCardinality()) {
+        case ZERO:
+            return Optional.empty();
+        case ONE:
+            return methods.getFirst();
+        case MULTIPLE:
+            break; // fall through
+        }
+
+        // assert all methods share the same type hierarchy
+        val first = methods.getFirstElseFail();
+        methods
+            .stream()
+            .skip(1)
+            .forEach(next->{
+                _Assert.assertTrue(
+                        methodsWeaklySame(first, next));
+                _Assert.assertTrue(shareSameTypeHierarchy(first, next));
+            });
+
+        return methods
+            .stream()
+            .distinct()
+            .max((a, b)->{
+                val clsA = a.getDeclaringClass();
+                val clsB = b.getDeclaringClass();
+                if(clsA.equals(clsB)) {
+                    throw _Exceptions.unrecoverable("methods declared on the same type "
+                            + "cannot pass methodsWeaklySame check "
+                            + "and at the same time be distinct");
+                }
+                return clsA.isAssignableFrom(clsB)
+                        ? -1
+                        : 1;
+            });
+    }
+
 }
diff --git a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java
index d7a05a0380..01355f66c6 100644
--- a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java
+++ b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/FacetFactoryTestAbstract.java
@@ -36,6 +36,7 @@ import org.apache.causeway.applib.services.i18n.TranslationService;
 import org.apache.causeway.applib.services.iactnlayer.InteractionService;
 import org.apache.causeway.commons.internal.assertions._Assert;
 import org.apache.causeway.commons.internal.reflection._MethodFacades;
+import org.apache.causeway.commons.internal.reflection._Reflect;
 import org.apache.causeway.core.metamodel._testing.MetaModelContext_forTesting;
 import org.apache.causeway.core.metamodel._testing.MethodRemover_forTesting;
 import org.apache.causeway.core.metamodel.context.HasMetaModelContext;
@@ -282,7 +283,17 @@ implements HasMetaModelContext {
 
         val declaringClass = scenario.declaringClass();
         val memberId = scenario.actionName();
-        val actionMethod = _Utils.findMethodByNameOrFail(declaringClass, memberId);
+
+        val matchingActionMethods = _Utils.findMethodsByName(declaringClass, memberId);
+
+        //TODO[CAUSEWAY-3556] this logic should be moved to the _ClassCache
+        val actionMethod = matchingActionMethods.isCardinalityMultiple()
+                ? _Reflect.mostSpecificMethodOf(matchingActionMethods).orElseThrow()
+                : matchingActionMethods.getSingletonOrFail();
+
+        //_Assert.assertEquals(1, matchingActionMethods.size(), ()->String.format("action by id '%s' could not be resolved", memberId));
+
+        //val actionMethod = matchingActionMethods.getSingletonOrFail();
         val paramTypes = actionMethod.getParameterTypes();
         val facetHolder = actionFacetHolder(declaringClass, memberId, paramTypes);
         val facetedMethod = FacetedMethod.createForAction(getMetaModelContext(), declaringClass, memberId, paramTypes);
diff --git a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/parameter/ParameterAnnotationFacetFactoryTest.java b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/parameter/ParameterAnnotationFacetFactoryTest.java
index 659826447c..8a77e0ebf4 100644
--- a/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/parameter/ParameterAnnotationFacetFactoryTest.java
+++ b/core/metamodel/src/test/java/org/apache/causeway/core/metamodel/facets/param/parameter/ParameterAnnotationFacetFactoryTest.java
@@ -19,11 +19,11 @@
 package org.apache.causeway.core.metamodel.facets.param.parameter;
 
 import java.util.regex.Pattern;
+import java.util.stream.Stream;
 
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.condition.DisabledIfSystemProperty;
 import org.springframework.lang.Nullable;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -35,6 +35,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import org.apache.causeway.applib.annotation.Optionality;
 import org.apache.causeway.applib.annotation.Parameter;
 import org.apache.causeway.applib.spec.Specification;
+import org.apache.causeway.commons.internal.reflection._Reflect;
 import org.apache.causeway.core.metamodel.facets.FacetFactoryTestAbstract;
 import org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacet;
 import org.apache.causeway.core.metamodel.facets.objectvalue.mandatory.MandatoryFacet.Semantics;
@@ -129,8 +130,6 @@ extends FacetFactoryTestAbstract {
             test(Customer.class);
         }
 
-        //TODO[CAUSEWAY-3556] enable before merge into master
-        @DisabledIfSystemProperty(named = "isRunningWithSurefire", matches = "true")
         @Test
         public void withGenericallyOverwrittenAnnotation() {
 
@@ -147,6 +146,24 @@ extends FacetFactoryTestAbstract {
                         final String name) { }
             }
 
+            //TODO[CAUSEWAY-3556] debugging
+            {
+                Stream.concat(Stream.of(Base.class.getMethods()), Stream.of(Customer.class.getMethods()))
+                //Stream.of(Customer.class.getMethods())
+                    .filter(method->method.getName().equals("someAction"))
+                    .forEach(method->System.err.printf("method: %s%n"
+                            + "\tgeneric->%b%n"
+                            + "\tbridge->%b%n"
+                            + "\tsynth->%b%n"
+                            + "\tdeclaringCls->%s%n",
+                            method,
+                            _Reflect.hasGenericBounds(method),
+                            method.isBridge(),
+                            method.isSynthetic(),
+                            method.getDeclaringClass()
+                            ));
+            }
+
             test(Customer.class);
         }
     }