You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2023/07/25 16:05:33 UTC

[groovy] branch meta-member updated (7bc43ac891 -> dc7fbc4b66)

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

emilles pushed a change to branch meta-member
in repository https://gitbox.apache.org/repos/asf/groovy.git


 discard 7bc43ac891 introduce interface `MetaMember`
     new dc7fbc4b66 introduce interface `MetaMember`

This update added new revisions after undoing existing revisions.
That is to say, some revisions that were in the old version of the
branch are not in the new version.  This situation occurs
when a user --force pushes a change and generates a repository
containing something like this:

 * -- * -- B -- O -- O -- O   (7bc43ac891)
            \
             N -- N -- N   refs/heads/meta-member (dc7fbc4b66)

You should already have received notification emails for all of the O
revisions, and so the following emails describe only the N revisions
from the common base, B.

Any revisions marked "omit" are not gone; other references still
refer to them.  Any revisions marked "discard" are gone forever.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 src/main/java/groovy/lang/MetaMember.java | 3 +++
 1 file changed, 3 insertions(+)


[groovy] 01/01: introduce interface `MetaMember`

Posted by em...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch meta-member
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit dc7fbc4b66565ac7511e492af0fb6b8e2885db5b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jul 25 10:48:33 2023 -0500

    introduce interface `MetaMember`
---
 .../java/groovy/lang/ClosureInvokingMethod.java    | 14 +---
 src/main/java/groovy/lang/ExpandoMetaClass.java    |  7 +-
 src/main/java/groovy/lang/MetaClassImpl.java       | 74 ++++++++++------------
 .../lang/MetaMember.java}                          | 39 +++++++++---
 src/main/java/groovy/lang/MetaMethod.java          | 35 +---------
 src/main/java/groovy/lang/MetaProperty.java        |  4 +-
 .../groovy/reflection/CachedConstructor.java       | 43 ++++++++-----
 .../codehaus/groovy/reflection/CachedField.java    |  9 ---
 .../codehaus/groovy/reflection/CachedMethod.java   | 15 ++---
 .../runtime/metaclass/ClosureMetaMethod.java       |  3 +-
 10 files changed, 106 insertions(+), 137 deletions(-)

diff --git a/src/main/java/groovy/lang/ClosureInvokingMethod.java b/src/main/java/groovy/lang/ClosureInvokingMethod.java
index afdb0aa492..b9e22b3332 100644
--- a/src/main/java/groovy/lang/ClosureInvokingMethod.java
+++ b/src/main/java/groovy/lang/ClosureInvokingMethod.java
@@ -24,23 +24,11 @@ package groovy.lang;
  * @see groovy.lang.ExpandoMetaClass
  * @since 1.5
  */
-public interface ClosureInvokingMethod {
+public interface ClosureInvokingMethod extends MetaMember {
 
     /**
      * Returns the original closure that this method invokes
      * @return The closure
      */
     Closure getClosure();
-
-    /**
-     * Is it a static method?
-     * @return True if it is
-     */
-    boolean isStatic();
-
-    /**
-     * The method name
-     * @return The method name
-     */
-    String getName();
 }
diff --git a/src/main/java/groovy/lang/ExpandoMetaClass.java b/src/main/java/groovy/lang/ExpandoMetaClass.java
index 2d413b3b17..c6905bbff3 100644
--- a/src/main/java/groovy/lang/ExpandoMetaClass.java
+++ b/src/main/java/groovy/lang/ExpandoMetaClass.java
@@ -976,11 +976,8 @@ public class ExpandoMetaClass extends MetaClassImpl implements GroovyObject {
         MetaBeanProperty beanProperty = (MetaBeanProperty) propertyCache.get(propertyName);
         if (beanProperty==null) {
             MetaProperty metaProperty = super.getMetaProperty(propertyName);
-            if (metaProperty instanceof MetaBeanProperty) {
-                boolean staticProp = Modifier.isStatic(metaProperty.getModifiers());
-                if (isStatic==staticProp) {
-                    beanProperty = (MetaBeanProperty) metaProperty;
-                }
+            if (metaProperty instanceof MetaBeanProperty && isStatic == metaProperty.isStatic()) {
+                beanProperty = (MetaBeanProperty) metaProperty;
             }
         }
         if (beanProperty == null) {
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index f98b0cd6ea..7e56bf68a7 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -3223,14 +3223,14 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
     }
 
     private Object chooseMethodInternal(String methodName, Object methodOrList, Class[] arguments) throws MethodSelectionException {
-        if (methodOrList instanceof MetaMethod) {
+        if (methodOrList instanceof ParameterTypes) {
             if (((ParameterTypes) methodOrList).isValidMethod(arguments)) {
                 return methodOrList;
             }
             return null;
         }
 
-        FastArray methods = (FastArray) methodOrList;
+        var methods = (FastArray) methodOrList;
         if (methods == null) return null;
         int methodCount = methods.size();
         if (methodCount <= 0) {
@@ -3242,45 +3242,42 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
             }
             return null;
         }
-        Object answer;
         if (arguments == null || arguments.length == 0) {
-            answer = MetaClassHelper.chooseEmptyMethodParams(methods);
-        } else {
-            Object matchingMethods = null;
+            var method = MetaClassHelper.chooseEmptyMethodParams(methods);
+            if (method != null) {
+                return method;
+            }
+            throw new MethodSelectionException(methodName, methods, arguments);
+        }
 
-            final int len = methods.size;
-            Object[] data = methods.getArray();
-            for (int i = 0; i != len; ++i) {
-                Object method = data[i];
-                if (((ParameterTypes) method).isValidMethod(arguments)) {
-                    if (matchingMethods == null) {
-                        matchingMethods = method;
-                    } else if (matchingMethods instanceof ArrayList) {
-                        ((ArrayList) matchingMethods).add(method);
-                    } else {
-                        List arr = new ArrayList(4);
-                        arr.add(matchingMethods);
-                        arr.add(method);
-                        matchingMethods = arr;
-                    }
+        List<MetaMember> matchingMethods = new ArrayList<>(methodCount);
+        Object[] methodArray = methods.getArray();
+        for (int i = 0; i < methodCount; i += 1) {
+            var method = (ParameterTypes & MetaMember) methodArray[i];
+            if (method.isValidMethod(arguments)) {
+                matchingMethods.add(method);
+/*
+                if (matchingMethods == null) {
+                    matchingMethods = method;
+                } else if (matchingMethods instanceof List) {
+                    List<Object> list = (List<Object>) matchingMethods;
+                    list.add(method);
+                } else {
+                    List<Object> list = new ArrayList<>(4);
+                    list.add(matchingMethods);
+                    list.add(method);
+                    matchingMethods = list;
                 }
+*/
             }
-            if (matchingMethods == null) {
-                return null;
-            } else if (!(matchingMethods instanceof ArrayList)) {
-                return matchingMethods;
-            }
-            return chooseMostSpecificParams(methodName, (List) matchingMethods, arguments);
-
         }
-        if (answer != null) {
-            return answer;
+        methodCount = matchingMethods.size();
+        if (methodCount == 0) {
+            return null;
+        } else if (methodCount == 1) {
+            return matchingMethods.get(0);
         }
-        throw new MethodSelectionException(methodName, methods, arguments);
-    }
-
-    private Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments) {
-        return doChooseMostSpecificParams(theClass.getName(), name, matchingMethods, arguments, false);
+        return doChooseMostSpecificParams(theClass.getName(), methodName, matchingMethods, arguments, false);
     }
 
     protected static Object doChooseMostSpecificParams(String theClassName, String name, List matchingMethods, Class[] arguments, boolean checkParametersCompatible) {
@@ -3296,14 +3293,13 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
         }
 
         int size = matches.size();
-        if (1 == size) {
+        if (size == 1) {
             return matches.getFirst();
         }
-        if (0 == size) {
+        if (size == 0) {
             return null;
         }
-
-        //more than one matching method found --> ambiguous!
+        // more than one matching method found --> ambiguous!
         throw new GroovyRuntimeException(createErrorMessageForAmbiguity(theClassName, name, arguments, matches));
     }
 
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/MethodHelper.java b/src/main/java/groovy/lang/MetaMember.java
similarity index 52%
rename from src/main/java/org/codehaus/groovy/runtime/metaclass/MethodHelper.java
rename to src/main/java/groovy/lang/MetaMember.java
index 0b0110bab7..cf07f2bfc7 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/MethodHelper.java
+++ b/src/main/java/groovy/lang/MetaMember.java
@@ -16,23 +16,42 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-package org.codehaus.groovy.runtime.metaclass;
+package groovy.lang;
 
-import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 
 /**
- * Some reflection helper methods
+ * @since 5.0.0
  */
-public class MethodHelper {
+public interface MetaMember {
 
-    public static boolean isStatic(Method method) {
-        int flags = Modifier.STATIC;
-        return (method.getModifiers() & (flags)) == flags;
+    String getName();
+
+    int getModifiers();
+
+    default boolean isFinal() {
+        return (getModifiers() & Modifier.FINAL) != 0;
+    }
+
+    default boolean isPrivate() {
+        return (getModifiers() & Modifier.PRIVATE) != 0;
+    }
+
+    default boolean isProtected() {
+        return (getModifiers() & Modifier.PROTECTED) != 0;
     }
 
-    public static boolean isPublic(Method method) {
-        int flags = Modifier.PUBLIC;
-        return (method.getModifiers() & (flags)) == flags;
+    default boolean isPublic() {
+        return (getModifiers() & Modifier.PUBLIC) != 0;
     }
+
+    default boolean isStatic() {
+        return (getModifiers() & Modifier.STATIC) != 0;
+    }
+
+    default boolean isSynthetic() {
+        return (getModifiers() & /*Modifier.SYNTHETIC*/0x1000) != 0;
+    }
+
+    // getDeclaringClass()->Class cannot be included because MetaMethod declares getDeclaringClass()->CachedClass
 }
diff --git a/src/main/java/groovy/lang/MetaMethod.java b/src/main/java/groovy/lang/MetaMethod.java
index 1afcc7c1cf..e7c043c24a 100644
--- a/src/main/java/groovy/lang/MetaMethod.java
+++ b/src/main/java/groovy/lang/MetaMethod.java
@@ -30,7 +30,8 @@ import java.lang.reflect.Modifier;
  * Represents a Method on a Java object a little like {@link java.lang.reflect.Method}
  * except without using reflection to invoke the method
  */
-public abstract class MetaMethod extends ParameterTypes implements Cloneable {
+public abstract class MetaMethod extends ParameterTypes implements MetaMember, Cloneable {
+
     public static final MetaMethod[] EMPTY_ARRAY = new MetaMethod[0];
     private String signature;
     private String mopName;
@@ -161,14 +162,6 @@ public abstract class MetaMethod extends ParameterTypes implements Cloneable {
         }
     }
 
-    /**
-     * Returns whether this method is static.
-     * @return true if this method is static
-     */
-    public boolean isStatic() {
-        return (getModifiers() & Modifier.STATIC) != 0;
-    }
-
     /**
      * Returns whether this method is abstract.
      * @return true if this method is abstract
@@ -185,30 +178,6 @@ public abstract class MetaMethod extends ParameterTypes implements Cloneable {
         return (getModifiers() & (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC && getDeclaringClass().isInterface;
     }
 
-    /**
-     * Returns whether this method is private.
-     * @return true if this method is private
-     */
-    public final boolean isPrivate() {
-        return (getModifiers() & Modifier.PRIVATE) != 0;
-    }
-
-    /**
-     * Returns whether this method is protected.
-     * @return true if this method is protected
-     */
-    public final boolean isProtected() {
-        return (getModifiers() & Modifier.PROTECTED) != 0;
-    }
-
-    /**
-     * Returns whether this method is public.
-     * @return true if this method is public
-     */
-    public final boolean isPublic() {
-        return (getModifiers() & Modifier.PUBLIC) != 0;
-    }
-
     /**
      * @param method the method to compare against
      * @return true if the given method has the same name, parameters, return type
diff --git a/src/main/java/groovy/lang/MetaProperty.java b/src/main/java/groovy/lang/MetaProperty.java
index 39bb4dd877..28fadb98c7 100644
--- a/src/main/java/groovy/lang/MetaProperty.java
+++ b/src/main/java/groovy/lang/MetaProperty.java
@@ -25,7 +25,7 @@ import static org.apache.groovy.util.BeanUtils.capitalize;
 /**
  * Represents a property on a bean which may have a getter and/or a setter
  */
-public abstract class MetaProperty {
+public abstract class MetaProperty implements MetaMember {
 
     public static final String PROPERTY_SET_PREFIX = "set";
 
@@ -60,6 +60,7 @@ public abstract class MetaProperty {
      *
      * @return the name of the property
      */
+    @Override
     public String getName() {
         return name;
     }
@@ -75,6 +76,7 @@ public abstract class MetaProperty {
      * Returns the access modifier.
      * @return Modifier.PUBLIC
      */
+    @Override
     public int getModifiers() {
         return Modifier.PUBLIC;
     }
diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedConstructor.java b/src/main/java/org/codehaus/groovy/reflection/CachedConstructor.java
index c4261f31a2..25ac75a516 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedConstructor.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedConstructor.java
@@ -19,6 +19,7 @@
 package org.codehaus.groovy.reflection;
 
 import groovy.lang.GroovyRuntimeException;
+import groovy.lang.MetaMember;
 import org.codehaus.groovy.runtime.FormatHelper;
 import org.codehaus.groovy.runtime.InvokerInvocationException;
 
@@ -26,18 +27,17 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Modifier;
 
-import static org.codehaus.groovy.reflection.ReflectionUtils.makeAccessibleInPrivilegedAction;
+public class CachedConstructor extends ParameterTypes implements MetaMember {
 
-public class CachedConstructor extends ParameterTypes {
     private final CachedClass clazz;
     private final Constructor cachedConstructor;
 
-    public CachedConstructor(CachedClass clazz, final Constructor c) {
-        this.cachedConstructor = c;
+    public CachedConstructor(final CachedClass clazz, final Constructor c) {
         this.clazz = clazz;
+        this.cachedConstructor = c;
     }
 
-    public CachedConstructor(Constructor c) {
+    public CachedConstructor(final Constructor c) {
         this(ReflectionCache.getCachedClass(c.getDeclaringClass()), c);
     }
 
@@ -46,6 +46,28 @@ public class CachedConstructor extends ParameterTypes {
         return cachedConstructor.getParameterTypes();
     }
 
+    @Override
+    public String getName() {
+        return cachedConstructor.getName();
+    }
+
+    @Override
+    public int getModifiers() {
+        return cachedConstructor.getModifiers();
+    }
+
+    @Override
+    public boolean isSynthetic() {
+        return cachedConstructor.isSynthetic();
+    }
+
+    @Override
+    public String toString() {
+        return cachedConstructor.toString();
+    }
+
+    //--------------------------------------------------------------------------
+
     public static CachedConstructor find(Constructor constructor) {
         CachedConstructor[] constructors = ReflectionCache.getCachedClass(constructor.getDeclaringClass()).getConstructors();
         for (CachedConstructor cachedConstructor : constructors) {
@@ -96,15 +118,6 @@ public class CachedConstructor extends ParameterTypes {
                 setReason ? e : null);
     }
 
-    @Override
-    public String toString() {
-        return cachedConstructor.toString();
-    }
-
-    public int getModifiers () {
-        return cachedConstructor.getModifiers();
-    }
-
     public CachedClass getCachedClass() {
         return clazz;
     }
@@ -125,7 +138,7 @@ public class CachedConstructor extends ParameterTypes {
     private boolean makeAccessibleDone = false;
     private void makeAccessibleIfNecessary() {
         if (!makeAccessibleDone) {
-            makeAccessibleInPrivilegedAction(cachedConstructor);
+            ReflectionUtils.makeAccessibleInPrivilegedAction(cachedConstructor);
             makeAccessibleDone = true;
         }
     }
diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedField.java b/src/main/java/org/codehaus/groovy/reflection/CachedField.java
index 58b8431097..86cbb75d0b 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedField.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedField.java
@@ -24,7 +24,6 @@ import groovy.lang.MetaProperty;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
 import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
 
 import static org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType;
 
@@ -52,14 +51,6 @@ public class CachedField extends MetaProperty {
         return field.getDeclaringClass();
     }
 
-    public boolean isFinal() {
-        return Modifier.isFinal(getModifiers());
-    }
-
-    public boolean isStatic() {
-        return Modifier.isStatic(getModifiers());
-    }
-
     /**
      * {@inheritDoc}
      */
diff --git a/src/main/java/org/codehaus/groovy/reflection/CachedMethod.java b/src/main/java/org/codehaus/groovy/reflection/CachedMethod.java
index 94d855e786..d0cb91e451 100644
--- a/src/main/java/org/codehaus/groovy/reflection/CachedMethod.java
+++ b/src/main/java/org/codehaus/groovy/reflection/CachedMethod.java
@@ -28,7 +28,6 @@ import org.codehaus.groovy.runtime.callsite.CallSiteGenerator;
 import org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite;
 import org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite;
 import org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite;
-import org.codehaus.groovy.runtime.metaclass.MethodHelper;
 
 import java.lang.annotation.Annotation;
 import java.lang.ref.SoftReference;
@@ -220,6 +219,11 @@ public class CachedMethod extends MetaMethod implements Comparable {
         return getName() + getDescriptor();
     }
 
+    @Override
+    public boolean isSynthetic() {
+        return cachedMethod.isSynthetic();
+    }
+
     public CachedMethod getTransformedMethod() {
         return transformedMethod;
     }
@@ -228,15 +232,6 @@ public class CachedMethod extends MetaMethod implements Comparable {
         this.transformedMethod = transformedMethod;
     }
 
-    @Override
-    public boolean isStatic() {
-        return MethodHelper.isStatic(cachedMethod);
-    }
-
-    public boolean isSynthetic() {
-        return cachedMethod.isSynthetic();
-    }
-
     //--------------------------------------------------------------------------
 
     public CallSite createPogoMetaMethodSite(final CallSite site, final MetaClassImpl metaClass, final Class[] params) {
diff --git a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
index 616652fe31..5859fceebe 100644
--- a/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
+++ b/src/main/java/org/codehaus/groovy/runtime/metaclass/ClosureMetaMethod.java
@@ -58,7 +58,6 @@ public class ClosureMetaMethod extends MetaMethod implements ClosureInvokingMeth
         this.declaringClass = ReflectionCache.getCachedClass(declaringClass);
     }
 
-
     @Override
     public int getModifiers() {
         return Modifier.PUBLIC;
@@ -88,7 +87,7 @@ public class ClosureMetaMethod extends MetaMethod implements ClosureInvokingMeth
         return doCall.invoke(cloned, arguments);
     }
 
-  /**
+    /**
      * Retrieves the closure that is invoked by this MetaMethod
      *
      * @return The closure