[
https://issues.apache.org/jira/browse/GROOVY-5359?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18053952#comment-18053952
]
ASF GitHub Bot commented on GROOVY-5359:
----------------------------------------
eric-milles opened a new pull request, #2372:
URL: https://github.com/apache/groovy/pull/2372
`MetaClassImpl#invokeStaticMethod` checked for closure property before super
class method followed by callable property. As noted in the issue ticket, the
property check can be expensive. Therefore, I am proposing: check class for
method, check super class for method, then check for callable property.
I think this is compatible with STC, which checks for callable property
here:
https://github.com/apache/groovy/blob/c8ed37ef49d68b9bd57ef572d8ffea42ff57c70f/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java#L3818
```java
public Object invokeStaticMethod(final Object object, final String
methodName, final Object[] arguments) {
// initialize and redirect until object class is theClass
Object[] nonNullArguments = arguments != null ? arguments.clone() :
EMPTY_ARGUMENTS;
MetaMethod method = retrieveStaticMethod(methodName,
nonNullArguments);
if (method != null) {
MetaClassHelper.unwrap(nonNullArguments);
return method.doMethodInvoke(object, nonNullArguments);
}
Class<?>[] argumentTypes =
MetaClassHelper.convertToTypeArray(nonNullArguments);
MetaClassHelper.unwrap(nonNullArguments);
for (var superClass = theClass.getSuperclass(); superClass != null;
superClass = superClass.getSuperclass()) {
MetaClass mc = registry.getMetaClass(superClass);
method = mc.getStaticMetaMethod(methodName, argumentTypes);
if (method != null) return method.doMethodInvoke(object,
nonNullArguments);
}
Object propertyValue = null; // GROOVY-3284, GROOVY-3422, GROOVY-9779
try {
propertyValue = getProperty(theClass, theClass, methodName,
false, false);
} catch (MissingPropertyException ignore) {
}
if (propertyValue != null) {
if (propertyValue instanceof Closure closure) {
return closure.getMetaClass().invokeMethod(theClass,
closure, DO_CALL_METHOD, arguments, false, false);
} else {
return
registry.getMetaClass(propertyValue.getClass()).invokeMethod(propertyValue,
CALL_METHOD, arguments);
}
}
return invokeStaticMissingMethod(theClass, methodName,
nonNullArguments);
}
```
> static propertyMissing catches inherited static methods
> -------------------------------------------------------
>
> Key: GROOVY-5359
> URL: https://issues.apache.org/jira/browse/GROOVY-5359
> Project: Groovy
> Issue Type: Improvement
> Components: groovy-runtime
> Reporter: OC
> Assignee: Eric Milles
> Priority: Minor
>
> When static missing properties are catched, the handler is called also when
> an inherited static method is called.
> It is not a serious problem, but it feels weird. Either it should be changed
> (if possible with reasonably small effort), or thoroughly documented.
> Also, it should be documented that if the handler throws a
> MissingPropertyException, it gets silently eaten up and the static method is
> called all right. On the other hand, any other exception goes all the way up
> to the user-level harness if any, which is generally rather undesirable.
> Here's a code sample which illustrates the problem, along with the proper
> exception thrown so as the method is properly called:
> {code}
> class Foo {
> static def inheritedStaticMethod() {
> println "Allright, Kilroy's here"
> }
> }
> class Test extends Foo {
> static def main(av) {
> // need to catch all class-level missing properties
> Object.metaClass.static.propertyMissing={
> println "Property $it of ${delegate.simpleName} missing."
> // simulated 'found valid dynamic property' case
> if (it.startsWith('f')) return "(${delegate.simpleName}.$it OK)"
> // oops, we did not find valid d.p, so we
> throw new MissingPropertyException("No valid DP for
> ${delegate.simpleName}.$it")
> }
> println "Checking the handler: ${Object.foo} ${Foo.foo} ${Test.foo}
> ${String.foo}"
> inheritedStaticMethod()
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)