I looked at this block of code for a good hour, trying to understand why the log is not matched at compile time. I use gradle to build and have the dependencies listed as such:
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'
}
My “code block” is a class with a name FilesUtilannotated with help @groovy.logging.util.SLF4Jthat contains one method staticthat uses a variable log, as the javadoc annotation suggests
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;
}
}
})
}
}
When called gradlew build(using the gradle shell) I get 4 similar errors, for example:
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")
^
Where is Wally? (a mistake I cannot find ... Waldo is for you, American people)