[
https://issues.apache.org/jira/browse/GROOVY-2807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18152332#comment-18152332
]
ASF GitHub Bot commented on GROOVY-2807:
----------------------------------------
daniellansun commented on PR #3333:
URL: https://github.com/apache/groovy/pull/3333#issuecomment-7235276447
Thanks very much for the thoughtful review and prototype, @paulk-asert!
Reusing Groovy's own dynamic selection machinery is a much cleaner direction
than maintaining a separate set of overload rules.
I've reworked the implementation in 8a8c215 along the lines you suggested:
1. **Reused Groovy's dynamic selection & coercion**: Replaced the custom
scoring, widening, and coercion logic with `ParameterTypes`, `CachedClass`,
`MetaClassHelper.calculateParameterDistance`, and `CachedClass.coerceArgument`,
extracted into a package-private `MBeanOperationResolver` to keep `GroovyMBean`
focused without adding any public API.
2. **Aligned `null` and dynamic Groovy semantics (and documented them
explicitly)**: Adopted dynamic Groovy's distance rules—so `shape(null)` now
selects `Object` over `String`, matching a regular dynamic call on a Groovy
object. Among candidates with equal distance (such as sub-interfaces or
covariant reference arrays), a strictly more specific signature wins via
`Class.isAssignableFrom`; any remaining tie or unmatched call still falls back
to the last registered operation in `MBeanInfo` order, while single-candidate
operations keep the direct fast path. This behavior (along with calling
`server().invoke(...)` when an exact signature is needed) is now documented in
`GroovyMBean#invokeMethod` Javadoc and `jmx.adoc`.
3. **Additional edge-case and performance hardening**:
- Materialized `ParameterTypes` eagerly before lock-free publication via
`AtomicReference`, and checked applicability via `CachedClass.isAssignableFrom`
(plus rejecting `null` for primitive parameters) to avoid
`ParameterTypes#isValidExactMethod`'s per-call synchronization.
- Used copy-on-write `Arrays.copyOf(..., Object[].class)` when coercing
or unwrapping `Wrapper` arguments so covariant arrays (e.g. `BigDecimal[]`,
`GString[]`) cannot trigger `ArrayStoreException`, and kept selection/coercion
under `GroovyMBean`'s existing `ignoreErrors` handling.
- Refined `ClassLoader` resolution (trying the argument's loader first,
then TCCL, `GroovyMBean`'s loader including bootstrap fallback, and other
argument loaders) while caching standard-loader positive and negative misses
(`UNRESOLVED_STANDARD`) without retaining foreign `ClassLoader` references.
4. **Expanded tests**: Updated `Groovy2807` to assert the dynamic Groovy
`null` and `Character` semantics explicitly (also fixing a latent
`MBeanInfo`-order dependency in the earlier `num(Character)` assertion) and
added coverage for SAM/`Closure`, `Wrapper`, covariant arrays,
multi-`ClassLoader` isolation, error handling, and concurrent invocations.
I've also updated the PR description so the intended dynamic Groovy
selection rules are stated clearly up front. Please let me know if you have any
further thoughts or suggestions!
> 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)