Skip to content

Commit

Permalink
Merge pull request #356 from rhoughton-pivot/issues/fixAntlrParsing
Browse files Browse the repository at this point in the history
Fixes NPE caused by antlr plugin
  • Loading branch information
OdysseusLives authored Sep 27, 2021
2 parents 330ce70 + b9535f5 commit 1910128
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,33 +297,36 @@ class DependencyService {
def classpath = sourceSetClasspath(conf)
def references = new DependencyReferences()

sourceSetOutput(confName).files.findAll { it.exists() }.each { output ->
def artifactsByClass = artifactsByClass(conf)
def compiledSourceClassLoader = new URLClassLoader((classpath + output)
.collect { it.toURI().toURL() } as URL[], null as ClassLoader)

Files.walkFileTree(output.toPath(), new SimpleFileVisitor<Path>() {
@Override
FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().name.endsWith('.class')) {
try {
def visitor = new DependencyClassVisitor(artifactsByClass, compiledSourceClassLoader)
file.newInputStream().withCloseable { inputStream ->
new ClassReader(inputStream).accept(visitor, ClassReader.SKIP_DEBUG)

references.direct.addAll(visitor.directReferences)
references.indirect.addAll(visitor.indirectReferences)
def confSourceSet = sourceSetOutput(confName)
if (confSourceSet != null) {
confSourceSet.files.findAll { it.exists() }.each { output ->
def artifactsByClass = artifactsByClass(conf)
def compiledSourceClassLoader = new URLClassLoader((classpath + output)
.collect { it.toURI().toURL() } as URL[], null as ClassLoader)

Files.walkFileTree(output.toPath(), new SimpleFileVisitor<Path>() {
@Override
FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toFile().name.endsWith('.class')) {
try {
def visitor = new DependencyClassVisitor(artifactsByClass, compiledSourceClassLoader)
file.newInputStream().withCloseable { inputStream ->
new ClassReader(inputStream).accept(visitor, ClassReader.SKIP_DEBUG)

references.direct.addAll(visitor.directReferences)
references.indirect.addAll(visitor.indirectReferences)
}
} catch (Throwable t) {
// see https://github.com/nebula-plugins/gradle-lint-plugin/issues/88
// type annotations can cause ArrayIndexOutOfBounds in ASM:
// http://forge.ow2.org/tracker/index.php?func=detail&aid=317615&group_id=23&atid=100023
logger.debug("unable to read class ${file.toFile().name}", t)
}
} catch (Throwable t) {
// see https://github.com/nebula-plugins/gradle-lint-plugin/issues/88
// type annotations can cause ArrayIndexOutOfBounds in ASM:
// http://forge.ow2.org/tracker/index.php?func=detail&aid=317615&group_id=23&atid=100023
logger.debug("unable to read class ${file.toFile().name}", t)
}
return FileVisitResult.CONTINUE
}
return FileVisitResult.CONTINUE
}
})
})
}
}
return references
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,41 @@ class UnusedDependencyRuleSpec extends IntegrationTestKitSpec {
}
}
@Issue("351")
def 'fully parses project that uses antlr'() {
setup:
def expectedWarnings = [
'warning unused-dependency this dependency is unused and can be removed'
]
when:
buildFile.text = """\
plugins {
id 'antlr'
id 'nebula.lint'
id 'java'
}
gradleLint.rules = ['unused-dependency']
repositories { mavenCentral() }
dependencies {
antlr 'antlr:antlr:2.7.7'
implementation 'com.google.guava:guava:18.0'
implementation 'org.apache.httpcomponents:httpclient:4.5.3'
}""".stripMargin()
writeJavaSourceFile(main)
def result = runTasks('compileJava', 'autoLintGradle')
then:
expectedWarnings.each {
assert result.output.contains(it)
}
}
def dependencies(File _buildFile, String... confs = ['compile', 'testCompile', 'implementation', 'testImplementation', 'api']) {
_buildFile.text.readLines()
.collect { it.trim() }
Expand Down

0 comments on commit 1910128

Please sign in to comment.