簡體   English   中英

SLF4J groovy注釋編譯錯誤。 'log'在靜態范圍內找到,但不引用局部變量,靜態字段或類

[英]SLF4J groovy annotation compilation error. 'log' was found in a static scope but doesn't refer to a local variable, static field or class

我一直在盯着這段代碼一小段時間試圖弄清楚為什么日志在編譯時沒有被提取。 我正在使用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為你美國民眾)

因為你有一個內部匿名類,所以似乎沒有看到生成的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 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM