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 2017/11/30 07:08:47 UTC
[1/3] groovy git commit: Minor refactoring
Repository: groovy
Updated Branches:
refs/heads/GROOVY_2_6_X 9a426e998 -> fe10d3909
Minor refactoring
(cherry picked from commit f9656fb)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/1fe0e235
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/1fe0e235
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/1fe0e235
Branch: refs/heads/GROOVY_2_6_X
Commit: 1fe0e2359f8358348a977464d565d70dc537dd76
Parents: 9a426e9
Author: sunlan <su...@apache.org>
Authored: Thu Nov 30 13:35:15 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Nov 30 15:08:16 2017 +0800
----------------------------------------------------------------------
src/main/groovy/lang/MetaClassImpl.java | 52 +++++++++++++-------
.../runtime/metaclass/ClosureMetaClass.java | 41 +--------------
2 files changed, 34 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/1fe0e235/src/main/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/MetaClassImpl.java b/src/main/groovy/lang/MetaClassImpl.java
index f76a0b2..d60f252 100644
--- a/src/main/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/lang/MetaClassImpl.java
@@ -3310,43 +3310,57 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
private Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments) {
+ return doChooseMostSpecificParams(theClass.getName(), name, matchingMethods, arguments, false);
+ }
+ protected static Object doChooseMostSpecificParams(String theClassName, String name, List matchingMethods, Class[] arguments, boolean checkParametersCompatible) {
long matchesDistance = -1;
LinkedList matches = new LinkedList();
for (Object method : matchingMethods) {
- ParameterTypes paramTypes = (ParameterTypes) method;
- long dist = MetaClassHelper.calculateParameterDistance(arguments, paramTypes);
+ final ParameterTypes parameterTypes = (ParameterTypes) method;
+ if (checkParametersCompatible && !MetaClassHelper.parametersAreCompatible(arguments, parameterTypes.getNativeParameterTypes())) continue;
+ long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes);
if (dist == 0) return method;
- if (matches.isEmpty()) {
- matches.add(method);
- matchesDistance = dist;
- } else if (dist < matchesDistance) {
- matchesDistance = dist;
- matches.clear();
- matches.add(method);
- } else if (dist == matchesDistance) {
- matches.add(method);
- }
-
+ matchesDistance = handleMatches(matchesDistance, matches, method, dist);
}
- if (matches.size() == 1) {
+
+ int size = matches.size();
+ if (1 == size) {
return matches.getFirst();
}
- if (matches.isEmpty()) {
+ if (0 == size) {
return null;
}
//more than one matching method found --> ambiguous!
+ throw new GroovyRuntimeException(createErrorMessageForAmbiguity(theClassName, name, arguments, matches));
+ }
+
+ protected static String createErrorMessageForAmbiguity(String theClassName, String name, Class[] arguments, LinkedList matches) {
StringBuilder msg = new StringBuilder("Ambiguous method overloading for method ");
- msg.append(theClass.getName()).append("#").append(name)
+ msg.append(theClassName).append("#").append(name)
.append(".\nCannot resolve which method to invoke for ")
.append(InvokerHelper.toString(arguments))
.append(" due to overlapping prototypes between:");
- for (final Object matche : matches) {
- Class[] types = ((ParameterTypes) matche).getNativeParameterTypes();
+ for (final Object match : matches) {
+ CachedClass[] types = ((ParameterTypes) match).getParameterTypes();
msg.append("\n\t").append(InvokerHelper.toString(types));
}
- throw new GroovyRuntimeException(msg.toString());
+ return msg.toString();
+ }
+
+ protected static long handleMatches(long matchesDistance, LinkedList matches, Object method, long dist) {
+ if (matches.isEmpty()) {
+ matches.add(method);
+ matchesDistance = dist;
+ } else if (dist < matchesDistance) {
+ matchesDistance = dist;
+ matches.clear();
+ matches.add(method);
+ } else if (dist == matchesDistance) {
+ matches.add(method);
+ }
+ return matchesDistance;
}
private static boolean isGenericGetMethod(MetaMethod method) {
http://git-wip-us.apache.org/repos/asf/groovy/blob/1fe0e235/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java b/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
index d7383f2..c418392 100644
--- a/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
+++ b/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
@@ -168,49 +168,10 @@ public final class ClosureMetaClass extends MetaClassImpl {
}
private Object chooseMostSpecificParams(String name, List matchingMethods, Class[] arguments) {
- long matchesDistance = -1;
- LinkedList matches = new LinkedList();
- for (Iterator iter = matchingMethods.iterator(); iter.hasNext();) {
- Object method = iter.next();
- final ParameterTypes parameterTypes = (ParameterTypes) method;
- Class[] paramTypes = parameterTypes.getNativeParameterTypes();
- if (!MetaClassHelper.parametersAreCompatible(arguments, paramTypes)) continue;
- long dist = MetaClassHelper.calculateParameterDistance(arguments, parameterTypes);
- if (dist == 0) return method;
- if (matches.isEmpty()) {
- matches.add(method);
- matchesDistance = dist;
- } else if (dist < matchesDistance) {
- matchesDistance = dist;
- matches.clear();
- matches.add(method);
- } else if (dist == matchesDistance) {
- matches.add(method);
- }
-
- }
- if (matches.size() == 1) {
- return matches.getFirst();
- }
- if (matches.isEmpty()) {
- return null;
- }
-
- // more than one matching method found --> ambiguous!
- String msg = "Ambiguous method overloading for method ";
- msg += theClass.getName() + "#" + name;
- msg += ".\nCannot resolve which method to invoke for ";
- msg += InvokerHelper.toString(arguments);
- msg += " due to overlapping prototypes between:";
- for (Object match : matches) {
- CachedClass[] types = ((ParameterTypes) match).getParameterTypes();
- msg += "\n\t" + InvokerHelper.toString(types);
- }
- throw new GroovyRuntimeException(msg);
+ return doChooseMostSpecificParams(theClass.getName(), name, matchingMethods, arguments, true);
}
}
-
public ClosureMetaClass(MetaClassRegistry registry, Class theClass) {
super(registry, theClass);
}
[3/3] groovy git commit: Minor refactoring
Posted by su...@apache.org.
Minor refactoring
(cherry picked from commit 00b564e)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/fe10d390
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/fe10d390
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/fe10d390
Branch: refs/heads/GROOVY_2_6_X
Commit: fe10d3909269f344c697ef3f9441786238ed3156
Parents: 03acd2e
Author: sunlan <su...@apache.org>
Authored: Thu Nov 30 14:59:12 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Nov 30 15:08:24 2017 +0800
----------------------------------------------------------------------
.../groovy/classgen/InnerClassCompletionVisitor.java | 11 -----------
.../org/codehaus/groovy/classgen/InnerClassVisitor.java | 12 ------------
.../groovy/classgen/InnerClassVisitorHelper.java | 12 ++++++++++++
3 files changed, 12 insertions(+), 23 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/fe10d390/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java b/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
index b03d6fd..a8d84c8 100644
--- a/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
+++ b/src/main/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
@@ -356,17 +356,6 @@ public class InnerClassCompletionVisitor extends InnerClassVisitorHelper impleme
}
}
- private static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
- if (cn.isEnum() || cn.isInterface()) return false;
- if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false;
- if (!(cn instanceof InnerClassNode)) return false;
- InnerClassNode innerClass = (InnerClassNode) cn;
- // scope != null means aic, we don't handle that here
- if (innerClass.getVariableScope() != null) return false;
- // static inner classes don't need this$0
- return (innerClass.getModifiers() & ACC_STATIC) == 0;
- }
-
private void addThisReference(ConstructorNode node) {
if (!shouldHandleImplicitThisForInnerClass(classNode)) return;
Statement code = node.getCode();
http://git-wip-us.apache.org/repos/asf/groovy/blob/fe10d390/src/main/org/codehaus/groovy/classgen/InnerClassVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/InnerClassVisitor.java b/src/main/org/codehaus/groovy/classgen/InnerClassVisitor.java
index a235f86..87815a8 100644
--- a/src/main/org/codehaus/groovy/classgen/InnerClassVisitor.java
+++ b/src/main/org/codehaus/groovy/classgen/InnerClassVisitor.java
@@ -100,18 +100,6 @@ public class InnerClassVisitor extends InnerClassVisitorHelper implements Opcode
processingObjInitStatements = false;
}
- private static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
- if (cn.isEnum() || cn.isInterface()) return false;
- if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false;
-
- if (!(cn instanceof InnerClassNode)) return false;
- InnerClassNode innerClass = (InnerClassNode) cn;
- // scope != null means aic, we don't handle that here
- if (innerClass.getVariableScope() != null) return false;
- // static inner classes don't need this$0
- return (innerClass.getModifiers() & ACC_STATIC) == 0;
- }
-
@Override
protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
this.currentMethod = node;
http://git-wip-us.apache.org/repos/asf/groovy/blob/fe10d390/src/main/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java b/src/main/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
index 8cbe6cc..7ed0629 100644
--- a/src/main/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
+++ b/src/main/org/codehaus/groovy/classgen/InnerClassVisitorHelper.java
@@ -128,4 +128,16 @@ public abstract class InnerClassVisitorHelper extends ClassCodeVisitorSupport {
new BinaryExpression(fe, Token.newSymbol(Types.ASSIGN, -1, -1), ve)
));
}
+
+ protected static boolean shouldHandleImplicitThisForInnerClass(ClassNode cn) {
+ if (cn.isEnum() || cn.isInterface()) return false;
+ if ((cn.getModifiers() & Opcodes.ACC_STATIC) != 0) return false;
+
+ if (!(cn instanceof InnerClassNode)) return false;
+ InnerClassNode innerClass = (InnerClassNode) cn;
+ // scope != null means aic, we don't handle that here
+ if (innerClass.getVariableScope() != null) return false;
+ // static inner classes don't need this$0
+ return (innerClass.getModifiers() & Opcodes.ACC_STATIC) == 0;
+ }
}
[2/3] groovy git commit: Minor refactoring
Posted by su...@apache.org.
Minor refactoring
(cherry picked from commit 4b24970)
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/03acd2ed
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/03acd2ed
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/03acd2ed
Branch: refs/heads/GROOVY_2_6_X
Commit: 03acd2ed07cdd7cab6527481d405ec695a9f404a
Parents: 1fe0e23
Author: sunlan <su...@apache.org>
Authored: Thu Nov 30 14:45:23 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Thu Nov 30 15:08:20 2017 +0800
----------------------------------------------------------------------
src/main/groovy/lang/MetaClassImpl.java | 27 +--
.../runtime/metaclass/ClosureMetaClass.java | 166 +++++++++----------
2 files changed, 93 insertions(+), 100 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/03acd2ed/src/main/groovy/lang/MetaClassImpl.java
----------------------------------------------------------------------
diff --git a/src/main/groovy/lang/MetaClassImpl.java b/src/main/groovy/lang/MetaClassImpl.java
index d60f252..af984c9 100644
--- a/src/main/groovy/lang/MetaClassImpl.java
+++ b/src/main/groovy/lang/MetaClassImpl.java
@@ -1306,21 +1306,26 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
MetaMethod method = null;
if (arguments.length == 1 && arguments[0] instanceof List) {
Object[] newArguments = ((List) arguments[0]).toArray();
- method = getMethodWithCaching(sender, methodName, newArguments, isCallToSuper);
- if (method != null) {
- method = new TransformMetaMethod(method) {
- public Object invoke(Object object, Object[] arguments) {
- Object firstArgument = arguments[0];
- List list = (List) firstArgument;
- arguments = list.toArray();
- return super.invoke(object, arguments);
- }
- };
- }
+ method = createTransformMetaMethod(getMethodWithCaching(sender, methodName, newArguments, isCallToSuper));
}
return method;
}
+ protected MetaMethod createTransformMetaMethod(MetaMethod method) {
+ if (method == null) {
+ return null;
+ }
+
+ return new TransformMetaMethod(method) {
+ public Object invoke(Object object, Object[] arguments) {
+ Object firstArgument = arguments[0];
+ List list = (List) firstArgument;
+ arguments = list.toArray();
+ return super.invoke(object, arguments);
+ }
+ };
+ }
+
private Object invokePropertyOrMissing(Object object, String methodName, Object[] originalArguments, boolean fromInsideClass, boolean isCallToSuper) {
// if no method was found, try to find a closure defined as a field of the class and run it
Object value = null;
http://git-wip-us.apache.org/repos/asf/groovy/blob/03acd2ed/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java b/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
index c418392..1a59c0b 100644
--- a/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
+++ b/src/main/org/codehaus/groovy/runtime/metaclass/ClosureMetaClass.java
@@ -47,7 +47,6 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -147,10 +146,8 @@ public final class ClosureMetaClass extends MetaClassImpl {
return MetaClassHelper.chooseMostGeneralMethodWith1NullParam(methods);
} else {
List matchingMethods = new ArrayList();
-
- final int len = methods.size();
final Object[] data = methods.getArray();
- for (int i = 0; i != len; ++i) {
+ for (int i = 0, len = methods.size(); i != len; ++i) {
Object method = data[i];
// making this false helps find matches
@@ -158,11 +155,14 @@ public final class ClosureMetaClass extends MetaClassImpl {
matchingMethods.add(method);
}
}
- if (matchingMethods.isEmpty()) {
+
+ int size = matchingMethods.size();
+ if (0 == size) {
return null;
- } else if (matchingMethods.size() == 1) {
+ } else if (1 == size) {
return matchingMethods.get(0);
}
+
return chooseMostSpecificParams(CLOSURE_DO_CALL_METHOD, matchingMethods, arguments);
}
}
@@ -251,17 +251,7 @@ public final class ClosureMetaClass extends MetaClassImpl {
if (method == null && arguments.length == 1 && arguments[0] instanceof List) {
Object[] newArguments = ((List) arguments[0]).toArray();
Class[] newArgClasses = MetaClassHelper.convertToTypeArray(newArguments);
- method = pickClosureMethod(newArgClasses);
- if (method != null) {
- method = new TransformMetaMethod(method) {
- public Object invoke(Object object, Object[] arguments) {
- Object firstArgument = arguments[0];
- List list = (List) firstArgument;
- arguments = list.toArray();
- return super.invoke(object, arguments);
- }
- };
- }
+ method = createTransformMetaMethod(pickClosureMethod(newArgClasses));
}
if (method == null) throw new MissingMethodException(methodName, theClass, arguments, false);
}
@@ -275,84 +265,82 @@ public final class ClosureMetaClass extends MetaClassImpl {
MissingMethodException last = null;
Object callObject = object;
- if (method == null) {
- final Object owner = closure.getOwner();
- final Object delegate = closure.getDelegate();
- final Object thisObject = closure.getThisObject();
- final int resolveStrategy = closure.getResolveStrategy();
- boolean invokeOnDelegate = false;
- boolean invokeOnOwner = false;
- boolean ownerFirst = true;
-
- switch (resolveStrategy) {
- case Closure.TO_SELF:
- break;
- case Closure.DELEGATE_ONLY:
- method = getDelegateMethod(closure, delegate, methodName, argClasses);
- callObject = delegate;
- if (method == null) {
- invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
- }
- break;
- case Closure.OWNER_ONLY:
+ final Object owner = closure.getOwner();
+ final Object delegate = closure.getDelegate();
+ final Object thisObject = closure.getThisObject();
+ final int resolveStrategy = closure.getResolveStrategy();
+ boolean invokeOnDelegate = false;
+ boolean invokeOnOwner = false;
+ boolean ownerFirst = true;
+
+ switch (resolveStrategy) {
+ case Closure.TO_SELF:
+ break;
+ case Closure.DELEGATE_ONLY:
+ method = getDelegateMethod(closure, delegate, methodName, argClasses);
+ callObject = delegate;
+ if (method == null) {
+ invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
+ }
+ break;
+ case Closure.OWNER_ONLY:
+ method = getDelegateMethod(closure, owner, methodName, argClasses);
+ callObject = owner;
+ if (method == null) {
+ invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
+ }
+
+ break;
+ case Closure.DELEGATE_FIRST:
+ method = getDelegateMethod(closure, delegate, methodName, argClasses);
+ callObject = delegate;
+ if (method == null) {
method = getDelegateMethod(closure, owner, methodName, argClasses);
callObject = owner;
- if (method == null) {
- invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
- }
-
- break;
- case Closure.DELEGATE_FIRST:
- method = getDelegateMethod(closure, delegate, methodName, argClasses);
- callObject = delegate;
- if (method == null) {
- method = getDelegateMethod(closure, owner, methodName, argClasses);
- callObject = owner;
- }
- if (method == null) {
- invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
- invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
- ownerFirst = false;
+ }
+ if (method == null) {
+ invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
+ invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
+ ownerFirst = false;
+ }
+ break;
+ default: // owner first
+ // owner first means we start with the outer most owner that is not a generated closure
+ // this owner is equal to the this object, so we check that one first.
+ method = getDelegateMethod(closure, thisObject, methodName, argClasses);
+ callObject = thisObject;
+ if (method == null) {
+ // try finding a delegate that has that method... we start from
+ // outside building a stack and try each delegate
+ LinkedList list = new LinkedList();
+ for (Object current = closure; current != thisObject;) {
+ if (!(current instanceof Closure)) break;
+ Closure currentClosure = (Closure) current;
+ if (currentClosure.getDelegate() != null) list.add(current);
+ current = currentClosure.getOwner();
}
- break;
- default: // owner first
- // owner first means we start with the outer most owner that is not a generated closure
- // this owner is equal to the this object, so we check that one first.
- method = getDelegateMethod(closure, thisObject, methodName, argClasses);
- callObject = thisObject;
- if (method == null) {
- // try finding a delegate that has that method... we start from
- // outside building a stack and try each delegate
- LinkedList list = new LinkedList();
- for (Object current = closure; current != thisObject;) {
- if (!(current instanceof Closure)) break;
- Closure currentClosure = (Closure) current;
- if (currentClosure.getDelegate() != null) list.add(current);
- current = currentClosure.getOwner();
- }
- while (!list.isEmpty() && method == null) {
- Closure closureWithDelegate = (Closure) list.removeLast();
- Object currentDelegate = closureWithDelegate.getDelegate();
- method = getDelegateMethod(closureWithDelegate, currentDelegate, methodName, argClasses);
- callObject = currentDelegate;
- }
- }
- if (method == null) {
- invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
- invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
- }
- }
- if (method == null && (invokeOnOwner || invokeOnDelegate)) {
- try {
- if (ownerFirst) {
- return invokeOnDelegationObjects(invokeOnOwner, owner, invokeOnDelegate, delegate, methodName, arguments);
- } else {
- return invokeOnDelegationObjects(invokeOnDelegate, delegate, invokeOnOwner, owner, methodName, arguments);
+ while (!list.isEmpty() && method == null) {
+ Closure closureWithDelegate = (Closure) list.removeLast();
+ Object currentDelegate = closureWithDelegate.getDelegate();
+ method = getDelegateMethod(closureWithDelegate, currentDelegate, methodName, argClasses);
+ callObject = currentDelegate;
}
- } catch (MissingMethodException mme) {
- last = mme;
}
+ if (method == null) {
+ invokeOnDelegate = delegate != closure && (delegate instanceof GroovyObject);
+ invokeOnOwner = owner != closure && (owner instanceof GroovyObject);
+ }
+ }
+ if (method == null && (invokeOnOwner || invokeOnDelegate)) {
+ try {
+ if (ownerFirst) {
+ return invokeOnDelegationObjects(invokeOnOwner, owner, invokeOnDelegate, delegate, methodName, arguments);
+ } else {
+ return invokeOnDelegationObjects(invokeOnDelegate, delegate, invokeOnOwner, owner, methodName, arguments);
+ }
+ } catch (MissingMethodException mme) {
+ last = mme;
}
}