You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "João Nelas (Jira)" <ji...@apache.org> on 2021/07/29 09:41:00 UTC
[jira] [Commented] (GROOVY-10144) Calling method on specific trait
fails when on different files
[ https://issues.apache.org/jira/browse/GROOVY-10144?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17389782#comment-17389782 ]
João Nelas commented on GROOVY-10144:
-------------------------------------
After some experiments I've discovered that I can make the separate files version work if I add a direct import for the trait to the entry script.
With diff_files.groovy importing the T trait:
{code:java}
import T;
new A().exec();
{code}
everything works as expected:
{code:java}
>groovy diff_files.groovy
A - exec
T - exec
{code}
I believe this is caused by a failure from the parser to get the annotations for the type.
I've took some threaddumps of the moment where the annotations are filled on
{{org.codehaus.groovy.ast.AnnotatedNode}}, and the diff_files.groovy version skips over the {{asBoolean(ctx.TRAIT())}} check on AstBuilder.java (line 1160 on my sources):
{code:java}
if (isInterfaceWithDefaultMethods || asBoolean(ctx.TRAIT())) {
classNode.addAnnotation(new AnnotationNode(ClassHelper.makeCached(Trait.class)));
}
{code}
and the first {{addAnnotation}} call is further down the {{visitClassDeclaration}} method as a result of calling {{visitTypeList}} (line 1182):
{code:java}
classNode.setSuperClass(superClass);
classNode.setInterfaces(this.visitTypeList(ctx.is));
this.initUsingGenerics(classNode);
{code}
I believe it is a bug of the antlr4 parser.
> Calling method on specific trait fails when on different files
> --------------------------------------------------------------
>
> Key: GROOVY-10144
> URL: https://issues.apache.org/jira/browse/GROOVY-10144
> Project: Groovy
> Issue Type: Bug
> Components: parser-antlr4
> Affects Versions: 3.0.7
> Environment: macOS 10.15.7, groovy 3.0.7, AdoptOpenJDK)(build 1.8.0_282-b08)
> Reporter: João Nelas
> Priority: Major
>
> When calling a method on a specific trait, via the manual conflict resolution syntax, it fails with {{The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.}}.
> T.groovy
> {code:groovy}
> trait T {
> void exec() {
> println("T - exec");
> }
> }
> {code}
> A.groovy
> {code:groovy}
> class A implements T{
> void exec() {
> println("A - exec");
> T.super.exec();
> }
> }
> {code}
> diff_files.groovy
> {code:groovy}
> import A;
> new A().exec();
> {code}
> Running {{diff_files.groovy}} gives an error:
> {noformat}
> > groovy diff_files.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> file:/Users/joao/tmp/groovy_trait/A.groovy: 6: The usage of 'Class.this' and 'Class.super' is only allowed in nested/inner classes.
> @ line 6, column 10.
> T.super.exec();
> ^
> 1 error
> {noformat}
> If everything is included on the same file
> onefile.groovy
> {code:groovy}
> trait T {
> void exec() {
> println("T - exec");
> }
> }
> class A implements T{
> void exec() {
> println("A - exec");
> T.super.exec();
> }
> }
> new A().exec();
> {code}
> everything works as expected:
> {noformat}
> >groovy onefile.groovy
> A - exec
> T - exec
> {noformat}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)