You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2018/10/20 10:45:55 UTC
[1/2] groovy git commit: Trivial refactoring: extract method
`getMetaClass`
Repository: groovy
Updated Branches:
refs/heads/master f6b996f11 -> d0cd0538b
Trivial refactoring: extract method `getMetaClass`
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/4cb1c7cf
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/4cb1c7cf
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/4cb1c7cf
Branch: refs/heads/master
Commit: 4cb1c7cf9324cf825f0981dd18c71fc036985eed
Parents: f6b996f
Author: Daniel Sun <su...@apache.org>
Authored: Sat Oct 20 17:55:35 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Sat Oct 20 17:55:35 2018 +0800
----------------------------------------------------------------------
src/main/groovy/groovy/lang/MetaClassImpl.java | 37 ++++++++++++---------
1 file changed, 21 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/4cb1c7cf/src/main/groovy/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/MetaClassImpl.java b/src/main/groovy/groovy/lang/MetaClassImpl.java
index f20d455..28512a5 100644
--- a/src/main/groovy/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/groovy/lang/MetaClassImpl.java
@@ -1140,17 +1140,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
//
// unwrap(arguments);
- MetaMethod method = null;
- if (CLOSURE_CALL_METHOD.equals(methodName) && object instanceof GeneratedClosure) {
- method = getMethodWithCaching(sender, "doCall", arguments, isCallToSuper);
- }
- if (method==null) {
- method = getMethodWithCaching(sender, methodName, arguments, isCallToSuper);
- }
- MetaClassHelper.unwrap(arguments);
-
- if (method == null)
- method = tryListParamMetaMethod(sender, methodName, isCallToSuper, arguments);
+ MetaMethod method = getMetaMethod(sender, object, methodName, isCallToSuper, arguments);
final boolean isClosure = object instanceof Closure;
if (isClosure) {
@@ -1235,17 +1225,17 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
break;
default:
- if (method == null && owner != closure) {
- MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
- method = ownerMetaClass.pickMethod(methodName, argClasses);
- if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
- }
if (method == null && delegate != closure && delegate != null) {
MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
method = delegateMetaClass.pickMethod(methodName, argClasses);
if (method != null)
return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
}
+ if (method == null && owner != closure) {
+ MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
+ method = ownerMetaClass.pickMethod(methodName, argClasses);
+ if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+ }
if (method == null && resolveStrategy != Closure.TO_SELF) {
// still no methods found, test if delegate or owner are GroovyObjects
// and invoke the method on them if so.
@@ -1299,6 +1289,21 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
}
+ private MetaMethod getMetaMethod(Class sender, Object object, String methodName, boolean isCallToSuper, Object... arguments) {
+ MetaMethod method = null;
+ if (CLOSURE_CALL_METHOD.equals(methodName) && object instanceof GeneratedClosure) {
+ method = getMethodWithCaching(sender, "doCall", arguments, isCallToSuper);
+ }
+ if (method==null) {
+ method = getMethodWithCaching(sender, methodName, arguments, isCallToSuper);
+ }
+ MetaClassHelper.unwrap(arguments);
+
+ if (method == null)
+ method = tryListParamMetaMethod(sender, methodName, isCallToSuper, arguments);
+ return method;
+ }
+
private MetaMethod tryListParamMetaMethod(Class sender, String methodName, boolean isCallToSuper, Object[] arguments) {
MetaMethod method = null;
if (arguments.length == 1 && arguments[0] instanceof List) {
[2/2] groovy git commit: Minor refactoring: remove duplicated code of
`MetaClassImpl`
Posted by su...@apache.org.
Minor refactoring: remove duplicated code of `MetaClassImpl`
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/d0cd0538
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/d0cd0538
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/d0cd0538
Branch: refs/heads/master
Commit: d0cd0538b88e844096aeae64bcb2bd138546f856
Parents: 4cb1c7c
Author: Daniel Sun <su...@apache.org>
Authored: Sat Oct 20 18:45:37 2018 +0800
Committer: Daniel Sun <su...@apache.org>
Committed: Sat Oct 20 18:45:37 2018 +0800
----------------------------------------------------------------------
src/main/groovy/groovy/lang/MetaClassImpl.java | 64 +++++++++++++--------
1 file changed, 39 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/d0cd0538/src/main/groovy/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/groovy/lang/MetaClassImpl.java b/src/main/groovy/groovy/lang/MetaClassImpl.java
index 28512a5..20a373b 100644
--- a/src/main/groovy/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/groovy/lang/MetaClassImpl.java
@@ -1191,17 +1191,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
}
break;
case Closure.DELEGATE_FIRST:
- if (method == null && delegate != closure && delegate != null) {
- MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
- method = delegateMetaClass.pickMethod(methodName, argClasses);
- if (method != null)
- return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
- }
- if (method == null && owner != closure) {
- MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
- method = ownerMetaClass.pickMethod(methodName, argClasses);
- if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+ Object result = invokeMethod(method, delegate, closure, methodName, argClasses, originalArguments, owner);
+ if (InvokeMethodResult.NONE != result) {
+ return result;
}
+
if (method == null && resolveStrategy != Closure.TO_SELF) {
// still no methods found, test if delegate or owner are GroovyObjects
// and invoke the method on them if so.
@@ -1225,17 +1219,11 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
break;
default:
- if (method == null && delegate != closure && delegate != null) {
- MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
- method = delegateMetaClass.pickMethod(methodName, argClasses);
- if (method != null)
- return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
- }
- if (method == null && owner != closure) {
- MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
- method = ownerMetaClass.pickMethod(methodName, argClasses);
- if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+ Object r = invokeMethod(method, delegate, closure, methodName, argClasses, originalArguments, owner);
+ if (InvokeMethodResult.NONE != r) {
+ return r;
}
+
if (method == null && resolveStrategy != Closure.TO_SELF) {
// still no methods found, test if delegate or owner are GroovyObjects
// and invoke the method on them if so.
@@ -1905,7 +1893,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
// special cases
//----------------------------------------------------------------------
if (method == null) {
- /** todo these special cases should be special MetaClasses maybe */
+ /* todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
MetaClass mc = registry.getMetaClass(Class.class);
return mc.getProperty(Class.class, object, name, useSuper, false);
@@ -2018,7 +2006,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
//----------------------------------------------------------------------
// special cases
//----------------------------------------------------------------------
- /** todo these special cases should be special MetaClasses maybe */
+ /* todo these special cases should be special MetaClasses maybe */
if (theClass != Class.class && object instanceof Class) {
return new MetaProperty(name, Object.class) {
public Object getProperty(Object object) {
@@ -2315,7 +2303,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
* This will build up the property map (Map of MetaProperty objects, keyed on
* property name).
*
- * @param propertyDescriptors
+ * @param propertyDescriptors the property descriptors
*/
@SuppressWarnings("unchecked")
private void setupProperties(PropertyDescriptor[] propertyDescriptors) {
@@ -3007,7 +2995,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
if (url != null) {
try {
- /**
+ /*
* todo there is no CompileUnit in scope so class name
* checking won't work but that mostly affects the bytecode
* generation rather than viewing the AST
@@ -3251,7 +3239,7 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
* name.
*
* @param methodOrList the possible methods to choose from
- * @param arguments
+ * @param arguments the arguments
*/
protected Object chooseMethod(String methodName, Object methodOrList, Class[] arguments) {
Object method = chooseMethodInternal(methodName, methodOrList, arguments);
@@ -4038,4 +4026,30 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
return null;
}
}
+
+ private Object invokeMethod(MetaMethod method,
+ Object delegate,
+ Closure closure,
+ String methodName,
+ Class[] argClasses,
+ Object[] originalArguments,
+ Object owner) {
+ if (method == null && delegate != closure && delegate != null) {
+ MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
+ method = delegateMetaClass.pickMethod(methodName, argClasses);
+ if (method != null)
+ return delegateMetaClass.invokeMethod(delegate, methodName, originalArguments);
+ }
+ if (method == null && owner != closure) {
+ MetaClass ownerMetaClass = lookupObjectMetaClass(owner);
+ method = ownerMetaClass.pickMethod(methodName, argClasses);
+ if (method != null) return ownerMetaClass.invokeMethod(owner, methodName, originalArguments);
+ }
+
+ return InvokeMethodResult.NONE;
+ }
+
+ private enum InvokeMethodResult {
+ NONE
+ }
}