[ 
https://issues.apache.org/jira/browse/GROOVY-2807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151458#comment-18151458
 ] 

ASF GitHub Bot commented on GROOVY-2807:
----------------------------------------

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.
   




> GroovyMBean does not support the use of overloaded MBean operations that have 
> the same number of parameters
> -----------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-2807
>                 URL: https://issues.apache.org/jira/browse/GROOVY-2807
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jmx
>            Reporter: Meyrick Kirby
>            Assignee: Daniel Sun
>            Priority: Major
>         Attachments: GroovyMBean.java
>
>
> I've been using groovy.util.GroovyMBean to access MBeans running on the JBoss 
> 4 server.  Some of the operations/methods of the MBeans are overloaded, e.g.:
> {code}
> void redeploy(java.lang.String url)
> void redeploy(java.net.URL url)
> void redeploy(org.jboss.deployment.DeploymentInfo sdi)
> {code}
> Currently it's impossible to invoke the first 2.
> The problem is, when the GroovyMBean is constructed it stores a signature for 
> each operation from its specified MBean in a HashMap, using the operation 
> name and the number of parameters as its key.  Later, when the invokeMethod 
> method is invoked, it uses the method name and the number of parameters to 
> retrieve the signature to invoke the operation on the MBean.  Thus for 
> overloaded operations with the same number of parameters, only the last 
> operation found will be stored in the HashMap.
> If another variant of the operation is invoked on the GroovyMBean then an 
> incorrect signature will be passed to the MBeanServer which results in an 
> exception occurring on the server.
> The fix I've used (suggested by a comment in the source code)  is to 
> construct a key out of the name of the operation and the sum of the hash 
> codes of the class names of the parameters.
> The problem is, this required 2 new createOperationKey methods:
> {code}
> String createOperationKey(MBeanOperationInfo opInfo)
> String createOperationKey(String method, Object[] args)
> {code}
> The first is invoked by the constructor, and the second that is invoked by 
> the invokeMethod method.  The original createOperationKey method is removed.  
> This might create backwards compatibility problems.
> I've included an adjusted GroovyMBean class, although temporarily in a new 
> package (groovy.util.adjusted).
> This issue is an extension of issue GROOVY-1291



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to