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

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

jdaugherty commented on PR #3086:
URL: https://github.com/apache/groovy/pull/3086#issuecomment-6929761783

   @blackdrag Thanks for working through the Hibernate proxy rules and the 
closure and trait cases. I pushed 049e02fd57, which adds a "Metaclass shared 
with another class" section to the metaprogramming guide, after 
`ExpandoMetaClass`, backed by a spec test in `ExpandoMetaClassTest`. It 
documents only what this PR restores:
   
   - the receiver must be an instance of the class the metaclass belongs to, 
usually a subclass that stands in for it;
   - method calls and property access select that class's methods and 
properties, including those added to its metaclass at runtime, and an override 
in the receiver's class runs when one of them is selected;
   - a change to the metaclass applies to later calls on the receiver.
   
   It says that what the metaclass answers for members only the receiver's 
class has is not specified: methods and properties only the subclass declares, 
`super` calls in the subclass, private members and fields, and the members that 
closures and trait methods resolve. Your closure and trait examples, and the 
4.0 fallback from my last comment, are all in that group. Defining them would 
be the new feature you describe, so I'd leave that to its own issue rather than 
document today's `MissingMethodException` here.
   
   The example calls the same site before and after replacing the method, so it 
fails on a build without this fix.
   
   @daniellansun The three items from your Nov 8 review went into 08b2089eec a 
few minutes after it, as described in my reply to Paul above: the cold-tier 
wrapper carries and checks the metaclass owner's `SwitchPoint`; the test covers 
the real method called first and then replaced, with the cold tier off and on 
(in a fresh JVM), the registry-answering proxy, and `DelegatingMetaClass` as a 
control; and the table has every cell filled. 049e02fd57 only adds the 
documentation. Could you take another look?
   
   The 6.0.x backport branch 
[`GROOVY-12558-6_0_X`](https://github.com/jdaugherty/groovy/tree/GROOVY-12558-6_0_X)
 has the same commit (4f2c9dc7ac).
   




> Indy: call site keeps a method replaced on a metaclass the receiver shares 
> with another class
> ---------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12558
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12558
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 6.0.0
>            Reporter: James Daugherty
>            Priority: Major
>
> On Groovy 6 with invokedynamic, a call site whose receiver uses the metaclass 
> of another class keeps invoking a method after that metaclass replaces it. 
> Groovy 5, and Groovy 6 with classic call sites, invoke the replacement. This 
> is a regression from GROOVY-12191 (6.0.0-beta-1).
> Such receivers are common in tests: Spock's {{Mock()}} generates a subclass 
> of the mocked type whose {{getMetaClass()}} answers the mocked type's 
> metaclass, so a feature that replaces a method on the mock's metaclass 
> changes the {{ExpandoMetaClass}} of the mocked type, and many proxy libraries 
> behave the same way. A specification that replaces such a method in one 
> feature and runs the same code under test in a later feature gets the earlier 
> feature's closure: a {{0 * collaborator.update(*_)}} interaction fails with 
> "Too many invocations", or an expected exception is never thrown.
> *Reproducer:* 
> https://github.com/jdaugherty/groovy-indy-shared-metaclass-switchpoint-reproducer
>  ({{./run.sh 5.1.3 6.0.0}}). {{GreeterProxy extends Greeter}} and is given 
> the {{ExpandoMetaClass}} of {{Greeter}}, as a mock or proxy subclass is. One 
> call site invokes {{greet()}} and one {{+=}} site invokes {{plus()}}; each 
> method is replaced on the metaclass between two calls of the same site:
> {noformat}
> == Groovy 5.1.3
>    indy=true: method call after replacing the method:   second (OK)
>    indy=true: compound assignment after replacing plus: second (OK)
> == Groovy 6.0.0
>    indy=true: method call after replacing the method:   first (STALE: the 
> call site still invokes the replaced method)
>    indy=true: compound assignment after replacing plus: first (STALE: the 
> call site still invokes the replaced method)
>    indy=false: method call after replacing the method:   second (OK)
>    indy=false: compound assignment after replacing plus: second (OK)
> {noformat}
> *Cause:* GROOVY-12191 replaced the global indy SwitchPoint with per-class 
> domains. A call site is guarded by the SwitchPoint of the receiver's class 
> ({{Selector.MethodSelector.captureTokenAndGetMetaClass()}}, 
> {{IndyInvalidation.classSwitchPointFor(args[0])}}; likewise in 
> {{IndyCompoundAssign.resolve}}). A change to a metaclass retires the domain 
> of the class the metaclass belongs to, {{MetaClass.getTheClass()}} 
> ({{IndyInvalidation.switchPointForMetaClass}} / {{invalidateMetaClass}}). 
> When the receiver's class is not the metaclass's class, as for 
> {{GreeterProxy}} and the {{Greeter}} metaclass it uses, the change retires 
> {{Greeter}}'s domain while the site waits on {{GreeterProxy}}'s. The receiver 
> metaclass guard ({{SAME_MC}}) does not catch it either, because replacing an 
> {{ExpandoMetaClass}} method keeps the same metaclass instance.
> *Proposed fix:* when the selected metaclass belongs to a class other than the 
> receiver's, also capture that class's SwitchPoint before selection and guard 
> the site with both. That applies to {{Selector}} and to 
> {{IndyCompoundAssign}}, which captures its token the same way. A patch with 
> tests follows as a pull request against master, with a backport for the 6.0.x 
> line.



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

Reply via email to