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

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

commit b418ff6c5d225f976d902f0f71d25c7ee73c90e1
Author: danhaywood <d...@haywood-associates.co.uk>
AuthorDate: Tue Dec 3 07:33:38 2019 +0000

    ISIS-2195: refactoring MethodUtil#removeMethods, removing use of boolean 
(replace with enum)
    
    Also removes SpecificationTraverser ... inlines its single method
---
 .../CanBeVoid.java}                                | 21 +++---------
 .../isis/metamodel/commons/ClassExtensions.java    |  6 ++--
 .../apache/isis/metamodel/commons/MethodUtil.java  | 17 +++++-----
 .../isis/metamodel/facetapi/MethodRemover.java     | 21 ++++++------
 .../apache/isis/metamodel/facets/FacetFactory.java |  5 +--
 .../metamodel/facets/MethodRemoverConstants.java   |  3 +-
 .../CollectionAccessorFacetViaAccessorFactory.java |  5 +--
 .../RemoveStaticGettersAndSettersFacetFactory.java |  5 +--
 .../PropertyAccessorFacetViaAccessorFactory.java   |  3 +-
 .../specloader/specimpl/FacetedMethodsBuilder.java | 38 ++++++++++------------
 .../metamodel/facets/MethodRemoverForTesting.java  |  7 ++--
 11 files changed, 62 insertions(+), 69 deletions(-)

diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/traverser/SpecificationTraverser.java
 b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/CanBeVoid.java
similarity index 60%
rename from 
core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/traverser/SpecificationTraverser.java
rename to 
core/metamodel/src/main/java/org/apache/isis/metamodel/commons/CanBeVoid.java
index 6ad9798..bc7d73d 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/traverser/SpecificationTraverser.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/CanBeVoid.java
@@ -17,22 +17,9 @@
  *  under the License.
  */
 
-package org.apache.isis.metamodel.specloader.traverser;
-
-import java.lang.reflect.Method;
-import java.util.function.Consumer;
-
-public class SpecificationTraverser {
-
-    /**
-     * Traverses the return types of each method.
-     *
-     * <p>
-     * It's possible for there to be multiple return types: the generic type,
-     * and the parameterized type.
-     */
-    public void traverseTypes(final Method method, final Consumer<Class<?>> 
onTypeDiscovered) {
-        new TypeExtractorMethodReturn(method).forEach(onTypeDiscovered);
-    }
+package org.apache.isis.metamodel.commons;
 
+public enum CanBeVoid {
+    TRUE,
+    FALSE
 }
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/ClassExtensions.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/ClassExtensions.java
index 98a6c7b..245cf38 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/ClassExtensions.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/ClassExtensions.java
@@ -155,11 +155,13 @@ public final class ClassExtensions {
         return ClassUtil.defaultByPrimitiveType.get(type);
     }
 
-    public static boolean isCompatibleAsReturnType(final Class<?> 
returnTypeExtendee, final boolean canBeVoid, final Class<?> type) {
+    public static boolean isCompatibleAsReturnType(final Class<?> 
returnTypeExtendee, final CanBeVoid canBeVoid, final Class<?> type) {
+        boolean mayBeVoid = canBeVoid == CanBeVoid.TRUE;
+
         if (returnTypeExtendee == null) {
             return true;
         }
-        if (canBeVoid && (type == void.class)) {
+        if (mayBeVoid && (type == void.class)) {
             return true;
         }
 
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/MethodUtil.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/MethodUtil.java
index 798ef39..186c9cb 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/MethodUtil.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/commons/MethodUtil.java
@@ -153,13 +153,14 @@ public class MethodUtil {
      * </ul>
      * If the returnType is specified as null then the return type is ignored.
      *
-     * @param forClass
      * @param name
-     * @param returnType
-     * @param onRemoval 
+     * @param onRemoval
      * @param paramTypes
      *            the set of parameters the method should have, if null then is
      *            ignored
+     * @param forClass
+     * @param returnType
+     * @param canBeVoid
      * @return Method
      */
     public static void removeMethods(
@@ -167,24 +168,24 @@ public class MethodUtil {
             MethodScope forClass,
             String prefix,
             Class<?> returnType,
-            boolean canBeVoid,
-            int paramCount, 
+            CanBeVoid canBeVoid,
+            int paramCount,
             Consumer<Method> onMatch) {
 
         methods.removeIf(method -> 
             matches(method, forClass, prefix, returnType, canBeVoid, 
paramCount, onMatch));
         
     }
-    
+
     private static boolean matches(
             Method method,
             MethodScope forClass,
             String prefix,
             Class<?> returnType,
-            boolean canBeVoid,
+            CanBeVoid canBeVoid,
             int paramCount,
             Consumer<Method> onMatch) {
-        
+
         if (!inScope(method, forClass)) {
             return false;
         }
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facetapi/MethodRemover.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facetapi/MethodRemover.java
index ea9de98..493c025 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facetapi/MethodRemover.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facetapi/MethodRemover.java
@@ -22,6 +22,7 @@ package org.apache.isis.metamodel.facetapi;
 import java.lang.reflect.Method;
 import java.util.function.Consumer;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.methodutils.MethodScope;
 
 /**
@@ -33,27 +34,27 @@ public interface MethodRemover {
      * Locate all methods (that the implementation should somehow know about)
      * that match the criteria and remove them from the implementation's list 
so
      * that they are not considered for subsequent scans.
-     *
      * @param methodScope
      *            - whether looking for <tt>static</tt> (class) or
      *            instance-level methods.
+     * @param canBeVoid
      * @param onRemoval receives any methods that were removed
      */
     void removeMethods(
-            MethodScope methodScope, 
-            String prefix, 
-            Class<?> returnType, 
-            boolean canBeVoid, 
+            MethodScope methodScope,
+            String prefix,
+            Class<?> returnType,
+            CanBeVoid canBeVoid,
             int paramCount,
             Consumer<Method> onRemoval
-            );
+    );
 
     /*variant with noop consumer*/
     default void removeMethods(
-            MethodScope methodScope, 
-            String prefix, 
-            Class<?> returnType, 
-            boolean canBeVoid, 
+            MethodScope methodScope,
+            String prefix,
+            Class<?> returnType,
+            CanBeVoid canBeVoid,
             int paramCount) {
         
         removeMethods(methodScope, prefix, returnType, canBeVoid, paramCount, 
removedMethod -> {});
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/FacetFactory.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/FacetFactory.java
index b4afccc..eeb6f73 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/FacetFactory.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/FacetFactory.java
@@ -29,6 +29,7 @@ import java.util.function.Supplier;
 
 import org.apache.isis.commons.internal.exceptions._Exceptions;
 import org.apache.isis.commons.internal.reflection._Annotations;
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.Facet;
 import org.apache.isis.metamodel.facetapi.FacetHolder;
 import org.apache.isis.metamodel.facetapi.FeatureType;
@@ -113,7 +114,7 @@ public interface FacetFactory {
 
 
         @Override
-        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final boolean canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
+        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final CanBeVoid canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
             methodRemover.removeMethods(methodScope, prefix, returnType, 
canBeVoid, paramCount, onRemoval);
         }
 
@@ -171,7 +172,7 @@ public interface FacetFactory {
         }
 
         @Override
-        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final boolean canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
+        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final CanBeVoid canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
             methodRemover.removeMethods(methodScope, prefix, returnType, 
canBeVoid, paramCount, onRemoval);
         }
 
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/MethodRemoverConstants.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/MethodRemoverConstants.java
index 254ce21..7a29d84 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/MethodRemoverConstants.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/MethodRemoverConstants.java
@@ -22,6 +22,7 @@ package org.apache.isis.metamodel.facets;
 import java.lang.reflect.Method;
 import java.util.function.Consumer;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.MethodRemover;
 import org.apache.isis.metamodel.methodutils.MethodScope;
 
@@ -29,7 +30,7 @@ public class MethodRemoverConstants {
 
     public static MethodRemover NOOP = new MethodRemover() {
         @Override
-        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final boolean canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
+        public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final CanBeVoid canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
         }
 
         @Override
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/collections/accessor/CollectionAccessorFacetViaAccessorFactory.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/collections/accessor/CollectionAccessorFacetViaAccessorFactory.java
index 13f39b8..1ea5042 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/collections/accessor/CollectionAccessorFacetViaAccessorFactory.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/collections/accessor/CollectionAccessorFacetViaAccessorFactory.java
@@ -23,6 +23,7 @@ import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.List;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.FacetHolder;
 import org.apache.isis.metamodel.facetapi.FeatureType;
 import org.apache.isis.metamodel.facetapi.MethodRemover;
@@ -97,8 +98,8 @@ extends PropertyOrCollectionIdentifyingFacetFactoryAbstract {
         methodRemover.removeMethods(
                 MethodScope.OBJECT, 
                 MethodLiteralConstants.GET_PREFIX,
-                Collection.class, 
-                false, 
+                Collection.class,
+                CanBeVoid.FALSE,
                 0,
                 methodListToAppendTo::add
                 );
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/object/ignore/isis/RemoveStaticGettersAndSettersFacetFactory.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/object/ignore/isis/RemoveStaticGettersAndSettersFacetFactory.java
index d57f77b..7ae8721 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/object/ignore/isis/RemoveStaticGettersAndSettersFacetFactory.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/object/ignore/isis/RemoveStaticGettersAndSettersFacetFactory.java
@@ -19,6 +19,7 @@
 
 package org.apache.isis.metamodel.facets.object.ignore.isis;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.FeatureType;
 import org.apache.isis.metamodel.facets.FacetFactoryAbstract;
 import org.apache.isis.metamodel.methodutils.MethodScope;
@@ -35,8 +36,8 @@ public class RemoveStaticGettersAndSettersFacetFactory 
extends FacetFactoryAbstr
 
     @Override
     public void process(final ProcessClassContext processClassContext) {
-        processClassContext.removeMethods(MethodScope.CLASS, "get", null, 
false, 0);
-        processClassContext.removeMethods(MethodScope.CLASS, "set", null, 
false, 0);
+        processClassContext.removeMethods(MethodScope.CLASS, "get", null, 
CanBeVoid.FALSE, 0);
+        processClassContext.removeMethods(MethodScope.CLASS, "set", null, 
CanBeVoid.FALSE, 0);
     }
 
 }
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/properties/accessor/PropertyAccessorFacetViaAccessorFactory.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/properties/accessor/PropertyAccessorFacetViaAccessorFactory.java
index 6ee0210..b51f3e8 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/properties/accessor/PropertyAccessorFacetViaAccessorFactory.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/facets/properties/accessor/PropertyAccessorFacetViaAccessorFactory.java
@@ -22,6 +22,7 @@ package org.apache.isis.metamodel.facets.properties.accessor;
 import java.lang.reflect.Method;
 import java.util.List;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.FacetHolder;
 import org.apache.isis.metamodel.facetapi.FacetUtil;
 import org.apache.isis.metamodel.facetapi.FeatureType;
@@ -98,7 +99,7 @@ public class PropertyAccessorFacetViaAccessorFactory extends 
PropertyOrCollectio
     }
 
     private static void appendMatchingMethods(final MethodRemover 
methodRemover, final String prefix, final Class<?> returnType, final 
List<Method> methodListToAppendTo) {
-        methodRemover.removeMethods(MethodScope.OBJECT, prefix, returnType, 
false, 0, methodListToAppendTo::add);
+        methodRemover.removeMethods(MethodScope.OBJECT, prefix, returnType, 
CanBeVoid.FALSE, 0, methodListToAppendTo::add);
     }
 
     @Override
diff --git 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/specimpl/FacetedMethodsBuilder.java
 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/specimpl/FacetedMethodsBuilder.java
index 0cb0855..8e8d1d7 100644
--- 
a/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/specimpl/FacetedMethodsBuilder.java
+++ 
b/core/metamodel/src/main/java/org/apache/isis/metamodel/specloader/specimpl/FacetedMethodsBuilder.java
@@ -20,10 +20,7 @@
 package org.apache.isis.metamodel.specloader.specimpl;
 
 import java.lang.reflect.Method;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
+import java.util.*;
 import java.util.function.Consumer;
 import java.util.function.Predicate;
 import java.util.stream.Collectors;
@@ -34,6 +31,7 @@ import org.apache.isis.commons.exceptions.IsisException;
 import org.apache.isis.commons.internal.collections._Lists;
 import org.apache.isis.commons.internal.collections._Sets;
 import org.apache.isis.commons.internal.reflection._Annotations;
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.commons.MethodUtil;
 import org.apache.isis.metamodel.commons.ToString;
 import org.apache.isis.metamodel.exceptions.MetaModelException;
@@ -51,7 +49,7 @@ import org.apache.isis.metamodel.spec.ObjectSpecification;
 import org.apache.isis.metamodel.specloader.SpecificationLoader;
 import org.apache.isis.metamodel.specloader.classsubstitutor.ClassSubstitutor;
 import org.apache.isis.metamodel.specloader.facetprocessor.FacetProcessor;
-import org.apache.isis.metamodel.specloader.traverser.SpecificationTraverser;
+import 
org.apache.isis.metamodel.specloader.traverser.TypeExtractorMethodReturn;
 
 import lombok.val;
 import lombok.extern.log4j.Log4j2;
@@ -92,7 +90,7 @@ public class FacetedMethodsBuilder {
                 MethodScope methodScope,
                 String prefix,
                 Class<?> returnType,
-                boolean canBeVoid,
+                CanBeVoid canBeVoid,
                 int paramCount,
                 Consumer<Method> onRemoval) {
             
@@ -137,7 +135,6 @@ public class FacetedMethodsBuilder {
 
     private final FacetProcessor facetProcessor;
 
-    private final SpecificationTraverser specificationTraverser = new 
SpecificationTraverser();
     private final ClassSubstitutor classSubstitutor = new ClassSubstitutor();
 
     private final SpecificationLoader specificationLoader;
@@ -252,7 +249,7 @@ public class FacetedMethodsBuilder {
         // Ensure all return types are known
         val typesToLoad = _Sets.<Class<?>>newHashSet();
         for (val method : associationCandidateMethods) {
-            specificationTraverser.traverseTypes(method, typesToLoad::add);
+            new TypeExtractorMethodReturn(method).forEach(typesToLoad::add);
         }
         typesToLoad.remove(introspectedClass);
 
@@ -464,13 +461,13 @@ public class FacetedMethodsBuilder {
         }
 
         val typesToLoad = _Sets.<Class<?>>newHashSet();
-        specificationTraverser.traverseTypes(actionMethod, typesToLoad::add);
+        new TypeExtractorMethodReturn(actionMethod).forEach(typesToLoad::add);
 
         val specLoader = getSpecificationLoader();
 
         val anyLoadedAsNull = typesToLoad.stream()
                 .map(typeToLoad->specLoader.loadSpecification(typeToLoad, 
IntrospectionState.TYPE_INTROSPECTED))
-                .anyMatch(spec->spec==null);
+                .anyMatch(Objects::isNull);
 
         if (anyLoadedAsNull) {
             return false;
@@ -535,8 +532,12 @@ public class FacetedMethodsBuilder {
     // 
////////////////////////////////////////////////////////////////////////////
 
     /**
-     * As per {@link 
#findAndRemovePrefixedNonVoidMethods(org.apache.isis.metamodel.methodutils.MethodScope,
 String, Class, int, java.util.List)},
-     * but appends to provided {@link List} (collecting parameter pattern).
+     *
+     * @param methodScope
+     * @param prefix
+     * @param returnType
+     * @param paramCount
+     * @param onRemoved - collecting parameter
      */
     private void findAndRemovePrefixedNonVoidMethods(
             final MethodScope methodScope,
@@ -545,24 +546,19 @@ public class FacetedMethodsBuilder {
             final int paramCount,
             final Consumer<Method> onRemoved) {
         
-        findAndRemovePrefixedMethods(methodScope, prefix, returnType, false, 
paramCount, onRemoved);
+        findAndRemovePrefixedMethods(methodScope, prefix, returnType, 
CanBeVoid.FALSE, paramCount, onRemoved);
     }
 
-    /**
-     * Searches for all methods matching the prefix and returns them, also
-     * removing it from the {@link #methodsRemaining array of methods} if 
found.
-     * @param onMatch 
-     */
     private void findAndRemovePrefixedMethods(
             final MethodScope methodScope,
             final String prefix,
             final Class<?> returnType,
-            final boolean canBeVoid,
-            final int paramCount, 
+            final CanBeVoid canBeVoid,
+            final int paramCount,
             Consumer<Method> onMatch) {
         
         methodRemover.acceptRemaining(methodsRemaining->{
-            MethodUtil.removeMethods(methodsRemaining, methodScope, prefix, 
returnType, canBeVoid, paramCount, onMatch);    
+            MethodUtil.removeMethods(methodsRemaining, methodScope, prefix, 
returnType, canBeVoid, paramCount, onMatch);
         });
         
     }
diff --git 
a/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/MethodRemoverForTesting.java
 
b/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/MethodRemoverForTesting.java
index 7fe4bf5..d6ab31e 100644
--- 
a/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/MethodRemoverForTesting.java
+++ 
b/core/metamodel/src/test/java/org/apache/isis/metamodel/facets/MethodRemoverForTesting.java
@@ -24,6 +24,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.function.Consumer;
 
+import org.apache.isis.metamodel.commons.CanBeVoid;
 import org.apache.isis.metamodel.facetapi.MethodRemover;
 import org.apache.isis.metamodel.methodutils.MethodScope;
 
@@ -77,14 +78,14 @@ public class MethodRemoverForTesting implements 
MethodRemover {
         public MethodScope methodScope;
         public String prefix;
         public Class<?> returnType;
-        public boolean canBeVoid;
+        public CanBeVoid canBeVoid;
         public int paramCount;
     }
 
-    private final List<RemoveMethodsArgs> removeMethodsArgs = new 
ArrayList<RemoveMethodsArgs>();
+    private final List<RemoveMethodsArgs> removeMethodsArgs = new 
ArrayList<>();
 
     @Override
-    public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final boolean canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
+    public void removeMethods(final MethodScope methodScope, final String 
prefix, final Class<?> returnType, final CanBeVoid canBeVoid, final int 
paramCount, Consumer<Method> onRemoval) {
         removeMethodsArgs.add(new RemoveMethodsArgs(methodScope, prefix, 
returnType, canBeVoid, paramCount));
     }
 

Reply via email to