codeconsole opened a new pull request, #3128:
URL: https://github.com/apache/groovy/pull/3128
Under `@CompileStatic`, a call from trait code to a `@Virtual` static method
whose name matches a `java.lang.Class` method binds to the `Class` method:
```groovy
import groovy.transform.Virtual
@groovy.transform.CompileStatic
trait T {
@Virtual static String getName() { 'trait' }
static String sm() { getName() }
}
class C implements T { static String getName() { 'impl' } }
class D implements T {}
assert C.sm() == 'impl' // 'C'
assert D.sm() == 'trait' // 'D'
```
The same happens for `getSimpleName`, `isInterface`, `getModifiers` and DGM
methods such as `dump()`. A `@Virtual static Object cast(Object)` fails with a
`ClassCastException`, because the call takes `Class<C>.cast`'s return type. It
also happens for calls from instance trait methods and for a `@Virtual` static
of a super trait, and in 6.0.0. Dynamic Groovy and `@TypeChecked` run the
implementing class's method.
The trait transformation rewrites the call to `$static$self.getName()` (or
`((Class) $self.getClass()).getName()`) and marks it for dynamic dispatch.
`TraitTypeCheckingExtension.handleMissingMethod` honours that mark only when no
method is found, and here the type checker finds `Class.getName()` on the
`Class<T>` receiver.
`TraitTypeCheckingExtension.onMethodSelection` now makes a call carrying
that mark dynamic even when a method was selected, with the return type of the
`@Virtual` method. The type checker treats the replaced target as an extension
intervention, so static compilation emits a dynamic call through the
implementing class.
The property form (`name` for `@Virtual static String getName()`) is handled
with #3121 (GROOVY-12594), which adds property-syntax support for `@Virtual`
static accessors.
## Tests
`VirtualAnnotationTest.virtualStatic_namedLikeClassMethod` and
`virtualStatic_ofSuperTrait_namedLikeClassMethod` run dynamic, `@TypeChecked`
and `@CompileStatic`. Their `@CompileStatic` variants fail without the change.
The `groovy.transform.stc`, `org.codehaus.groovy.classgen.asm.sc`,
`org.codehaus.groovy.transform.traitx` and `gls` packages,
`groovy.ThisAndSuperTest` and `TraitsSpecificationTest` pass. The Checkstyle
and CodeNarc gates pass.
--
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]