SLF4J groovy注释编译错误。 'log'在静态范围内找到,但不引用局部变量,静态字段或类

时间:2014-02-20 11:28:13

标签: groovy slf4j

我一直在盯着这个代码块一小段时间,试图弄清楚为什么在编译时没有获取日志。我正在使用gradle构建并依赖于此列出依赖项:

apply plugin: 'groovy'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.codehaus.groovy:groovy:2.2.1'
    compile 'org.slf4j:slf4j-api:1.7.6'
    provided 'org.projectlombok:lombok:1.12.4'
    runtime 'ch.qos.logback:logback-core:1.1.1'
    runtime 'ch.qos.logback:logback-classic:1.1.1'
    testCompile 'junit:junit:4.11'
}

我的'代码块'是一个名为FilesUtil的注释为@groovy.logging.util.SLF4J的类,其中包含一个static方法,该方法使用log变量作为注释javadoc建议

import groovy.util.logging.Slf4j

import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes

import static java.nio.file.FileVisitResult.CONTINUE;

@Slf4j
class FilesUtil {
    def static deleteDirectory(Path path) {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file,
                                             BasicFileAttributes attrs) throws IOException {
                log.trace("deleting file: $file")
                System.out.println("Deleting file: " + file)
                Files.delete(file);
                log.trace("deleted file: $file")
                return CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir,
                                                      IOException exc) throws IOException {
                if (exc == null) {
                    log.trace("deleting directory: $dir")
                    Files.delete(dir);
                    log.trace("deleted directory: $dir")
                    return CONTINUE;
                } else {
                    throw exc;
                }
            }
        })
    }
} 

在调用gradlew build(使用gradle包装器)后,我得到4个类似的错误,如下所示:

startup failed:
C:\Users\Macindows\IdeaProjects\corporate-git\subprojects\core\src\main\groovy\com\thenaglecode\corporategit\core\util\files\FilesUtil.groovy: 26: 
Apparent variable 'log' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
    You attempted to reference a variable in the binding or an instance variable from a static context.
    You misspelled a classname or statically imported field. Please check the spelling.
    You attempted to use a method 'log' but left out brackets in a place not allowed by the grammar.
    @ line 26, column 17.
               log.trace("deleting file: $file")
               ^

沃利在哪里? (我找不到的错误...... Waldo为你而美国民众)

1 个答案:

答案 0 :(得分:2)

因为你有一个内部匿名类,所以似乎没有看到生成的log变量。如果您将log.trace更改为FilesUtil.log.trace,则似乎有效。

或者如果您使用Map作为SimpleFileVisitor的代理,它似乎也有效:

    Files.walkFileTree(path, [ visitFile: { Path file, BasicFileAttributes attrs ->
            log.trace("deleting file: $file")
            System.out.println("Deleting file: " + file)
            //Files.delete(file);
            log.trace("deleted file: $file")
            return CONTINUE;
        },
        postVisitDirectory: { Path dir, IOException exc ->
            if (exc == null) {
                log.trace("deleting directory: $dir")
                //Files.delete(dir);
                log.trace("deleted directory: $dir")
                return CONTINUE;
            } else {
                throw exc;
            }
        } ] as SimpleFileVisitor )

不确定根本原因或者目前是否是一个错误...没有太多时间思考atm; - )