paulk-asert commented on PR #3333:
URL: https://github.com/apache/groovy/pull/3333#issuecomment-7192274734
Thanks @daniellansun. GROOVY-2807 is a real bug, and selecting the overload
by signature on the client is the right fix. JMX gives us only the type names
from `MBeanInfo`, the server matches the signature exactly, and Groovy's
MetaClass dispatch never runs, so `GroovyMBean` has to choose for itself. Your
tests are very thorough and were a big help while I looked at this.
My main concern is the selection code itself. It adds about 370 lines with
its own overload rules, separate from both Java's and Groovy's. The description
calls them Java method-invocation conversion, but they don't follow the JLS
phases (widening before boxing and so on). Ties go to the last registered
operation, where Groovy would report an ambiguous call. Behaviour that differs
subtly from everything else tends to bring its own bug reports.
I tried reusing Groovy's existing selection code instead. I kept your type
loading, `Operation`, `operationsFor` and the `invokeMethod` changes, and
replaced the candidate scoring with Groovy's own: `isValidExactMethod`,
`calculateParameterDistance` and `coerceArgument`:
```java
private static Selection select(List<Operation> matches, Object[] args) {
Class[] argTypes = MetaClassHelper.convertToTypeArray(args);
Operation chosen = null;
ParameterTypes chosenTypes = null;
long best = Long.MAX_VALUE;
for (Operation operation : matches) {
ParameterTypes types = operation.parameterTypes(args); // null if a
type can't be resolved
if (types == null || !types.isValidExactMethod(argTypes) ||
nullForPrimitive(args, types)) {
continue;
}
long distance = MetaClassHelper.calculateParameterDistance(argTypes,
types);
if (distance <= best) { // a tie keeps the last registered operation
best = distance;
chosen = operation;
chosenTypes = types;
}
}
if (chosen == null) {
return new Selection(matches.get(matches.size() - 1).signature,
args);
}
CachedClass[] params = chosenTypes.getParameterTypes();
Object[] callArgs = new Object[args.length];
for (int i = 0; i < args.length; i++) {
callArgs[i] = args[i] == null ? null :
params[i].coerceArgument(args[i]);
}
return new Selection(chosen.signature, callArgs);
}
```
`nullForPrimitive` is a small helper that skips an overload when a `null`
argument meets a primitive parameter. `Operation.parameterTypes` now returns a
`ParameterTypes`, and the distance constants, widening table, wrapper maps,
`Candidate` and the custom assignability and coercion code are no longer
needed. Compared with master, the change to `GroovyMBean` shrinks from about
+404 to about +186 lines.
Against your tests it passes 131 of 133 in `groovy-jmx`. That covers
widening, arrays and JVM descriptor names, swapped parameters, the GString,
BigDecimal and BigInteger coercions, ties, types that can't be loaded, class
loader handling, duplicate signatures and the `createOperationKey` override.
The two failures are the same case: a single `null` argument with
`shape(String)` and `shape(Object)` overloads. Your tests expect `String`,
which is what Java and `@CompileStatic` choose. Dynamic Groovy chooses
`Object`, and the prototype follows it:
```groovy
class S { def shape(String s) { 'string' }; def shape(Object o) { 'object' }
}
new S().shape(null) // 'object' (dynamic)
@CompileStatic def cs(S s) { s.shape(null) } // 'string'
```
A `GroovyMBean` call is a dynamic call, so I lean towards the dynamic
behaviour. Then `bean.op(null)` picks the same overload as the same call on an
ordinary Groovy object. I can see the argument for following Java, since MBean
interfaces are usually Java, and that would be a few lines on top of the
prototype. Either way, could the description and tests state which rule is
intended, rather than leaving it implied?
Would you be up for reworking the selection along these lines? Happy to
share the full prototype diff if that helps.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]