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

Eric Milles edited comment on GROOVY-7439 at 12/19/21, 11:21 PM:
-----------------------------------------------------------------

The logging transformation is transferred to the helper class 
{{TTest$Trait$Helper}}, where it is applied.  One approach that makes this work 
and is minimally invasive is to add a delegate method to the trait class:
{code:groovy}
@Slf4j('LOG')
trait T {
  @groovy.transform.Generated
  org.slf4j.Logger getLOG() {
    return T$Trait$Helper.@LOG
  }
}
{code}

org.codehaus.groovy.transform.LogASTTransformation:
{code:java}
            @Override
            public void visitClass(final ClassNode node) {
                FieldNode logField = node.getField(logFieldName);
                if (logField != null && logField.getOwner().equals(node)) {
                    ...
                } else if (logField != null && 
!Modifier.isPrivate(logField.getModifiers())) {
                    ...
                } else {
                    if (loggingStrategy instanceof LoggingStrategyV2) {
                        ...
                    } else {
                        ...
                    }
                    if (node.getName().endsWith("$Trait$Helper")) {
                        addGeneratedMethod(node.getOuterClass(), "get" + 
logFieldName, ACC_PUBLIC, logNode.getType(), Parameter.EMPTY_ARRAY, null, new 
ReturnStatement(new AttributeExpression(new ClassExpression(node), new 
ConstantExpression(logFieldName))));
                    }
                }
                ...
            }
{code}

Logging appears to come from the helper class, not the class that has the trait 
applied:
{code}
[main] INFO bugs.T7439$Trait$Helper - Logging test1 ...
{code}


was (Author: emilles):
The logging transformation is transferred to the helper class 
{{TTest$Trait$Helper}}, where it is applied.  One approach that makes this work 
and is minimally invasive is to add a delegate method to the trait class:
{code:groovy}
@Slf4j('LOG')
trait T {
  @groovy.transform.Generated
  org.slf4j.Logger getLOG() {
    return T$Trait$Helper.@LOG
  }
}
{code}

org.codehaus.groovy.transform.LogASTTransformation:
{code:java}
            @Override
            public void visitClass(final ClassNode node) {
                FieldNode logField = node.getField(logFieldName);
                if (logField != null && logField.getOwner().equals(node)) {
                    ...
                } else if (logField != null && 
!Modifier.isPrivate(logField.getModifiers())) {
                    ...
                } else {
                    if (loggingStrategy instanceof LoggingStrategyV2) {
                        ...
                    } else {
                        ...
                    }
                    if (node.getName().endsWith("$Trait$Helper")) {
                        addGeneratedMethod(node.getOuterClass(), "get" + 
logFieldName, ACC_PUBLIC, logNode.getType(), Parameter.EMPTY_ARRAY, null, new 
ReturnStatement(new AttributeExpression(new ClassExpression(node), new 
ConstantExpression(logFieldName))));
                    }
                }
                ...
            }
{code}

> Compilation errors using groovy trait and @CompileStatic and @Slf4j("LOG")
> --------------------------------------------------------------------------
>
>                 Key: GROOVY-7439
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7439
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.3
>         Environment: Windows 8.1
> Oracle JDK 8.u45
>            Reporter: Allen Arakaki
>            Assignee: Eric Milles
>            Priority: Major
>              Labels: traits
>
> Compilation errors using groovy trait and @CompileStatic and @Slf4j("LOG")
> Easy to reproduce:
> ------------------------------------------------
> {code}
> import org.slf4j.Logger
> import groovy.transform.CompileStatic
> import groovy.util.logging.Slf4j
> @Slf4j("LOG")
> @CompileStatic
> trait TTest {
>   void test1() {
>     LOG.debug("Logging test1 ...")
>   }
>   void test2() {
>     //((Logger)LOG).debug("Logging test2 ...")
>   }
> }
> {code}
> Results in the following:
> | Error Compilation error: startup failed:
> ..\src\groovy\TTest.groovy: 10: [Static type checking] - Cannot find matching 
> method java.lang.Object#debug(java.lang.String). Please check if the declared 
> type is right and if the method exists.
> @ line 10, column 5.
> LOG.debug("Logging test1 ...")
> ^
> {code}
> import org.slf4j.Logger
> import groovy.transform.CompileStatic
> import groovy.util.logging.Slf4j
> @Slf4j("LOG")
> @CompileStatic
> trait TTest {
>   void test1() {
>     //LOG.debug("Logging test1 ...")
>   }
>   void test2() {
>     ((Logger)LOG).debug("Logging test2 ...")
>   }
> }
> {code}
> Results in the following error:
> | Error Compilation error: startup failed:
> ..\src\groovy\TTest.groovy: -1: Access to TTest#LOG is forbidden @ line -1, 
> column -1.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to