[
https://issues.apache.org/jira/browse/GROOVY-12601?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Scott Murphy Heiberg updated GROOVY-12601:
------------------------------------------
Description:
{code: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'
assert D.sm() == 'trait'
{code}
Actual: {{C.sm()}} returns {{'C'}} and {{D.sm()}} returns {{'D'}}. The trait
calls {{Class.getName()}}. The same happens for other {{Class}} and DGM names
such as {{getSimpleName}}, {{isInterface}}, {{getModifiers}} and {{dump}}. A
{{@Virtual static Object cast(Object)}} fails with a {{ClassCastException}}. It
also happens from instance trait methods and for a {{@Virtual}} static of a
super trait.
Expected: {{'impl'}} and {{'trait'}}, as dynamic Groovy and {{@TypeChecked}}
give.
The trait transformation rewrites the call to a call on the static self (a
Class) and marks it for dynamic dispatch. Static type checking finds
{{Class.getName()}} on that receiver, so the trait type checking extension is
never asked and static compilation binds the call to the {{Class}} method.
Seen in 6.0.0 (the first version with @Virtual).
PR: https://github.com/apache/groovy/pull/3128
was:
{code: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'
assert D.sm() == 'trait'
{code}
Actual: {{C.sm()}} returns {{'C'}} and {{D.sm()}} returns {{'D'}}. The trait
calls {{Class.getName()}}. The same happens for other {{Class}} and DGM names
such as {{getSimpleName}}, {{isInterface}}, {{getModifiers}} and {{dump}}. A
{{@Virtual static Object cast(Object)}} fails with a {{ClassCastException}}. It
also happens from instance trait methods and for a {{@Virtual}} static of a
super trait.
Expected: {{'impl'}} and {{'trait'}}, as dynamic Groovy and {{@TypeChecked}}
give.
The trait transformation rewrites the call to a call on the static self (a
Class) and marks it for dynamic dispatch. Static type checking finds
{{Class.getName()}} on that receiver, so the trait type checking extension is
never asked and static compilation binds the call to the {{Class}} method.
Seen in 6.0.0 (the first version with @Virtual).
> SC calls the java.lang.Class method instead of a @Virtual trait static method
> of the same name
> ----------------------------------------------------------------------------------------------
>
> Key: GROOVY-12601
> URL: https://issues.apache.org/jira/browse/GROOVY-12601
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code: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'
> assert D.sm() == 'trait'
> {code}
> Actual: {{C.sm()}} returns {{'C'}} and {{D.sm()}} returns {{'D'}}. The trait
> calls {{Class.getName()}}. The same happens for other {{Class}} and DGM names
> such as {{getSimpleName}}, {{isInterface}}, {{getModifiers}} and {{dump}}. A
> {{@Virtual static Object cast(Object)}} fails with a {{ClassCastException}}.
> It also happens from instance trait methods and for a {{@Virtual}} static of
> a super trait.
> Expected: {{'impl'}} and {{'trait'}}, as dynamic Groovy and {{@TypeChecked}}
> give.
> The trait transformation rewrites the call to a call on the static self (a
> Class) and marks it for dynamic dispatch. Static type checking finds
> {{Class.getName()}} on that receiver, so the trait type checking extension is
> never asked and static compilation binds the call to the {{Class}} method.
> Seen in 6.0.0 (the first version with @Virtual).
> PR: https://github.com/apache/groovy/pull/3128
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)