Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Let ClassBytesRepository ignore invalid classpath entries #1039

Merged
merged 2 commits into from Aug 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -85,10 +85,10 @@ class ClassBytesRepository(classPath: ClassPath, classPathDependencies: ClassPat
classBytesIndex.firstNotNullResult { it(classFilePath) }

private
fun sourceNamesFrom(jarOrDir: File): Sequence<String> =
fun sourceNamesFrom(entry: File): Sequence<String> =
when {
jarOrDir.isFile -> sourceNamesFromJar(jarOrDir)
jarOrDir.isDirectory -> sourceNamesFromDir(jarOrDir)
entry.isClassPathArchive -> sourceNamesFromJar(entry)
entry.isDirectory -> sourceNamesFromDir(entry)
else -> emptySequence()
}

Expand All @@ -107,10 +107,10 @@ class ClassBytesRepository(classPath: ClassPath, classPathDependencies: ClassPat
.map { kotlinSourceNameOf(normaliseFileSeparators(it.relativeTo(dir).path)) }

private
fun classBytesIndexFor(jarOrDir: File): ClassBytesIndex =
fun classBytesIndexFor(entry: File): ClassBytesIndex =
when {
jarOrDir.isFile -> jarClassBytesIndexFor(jarOrDir)
jarOrDir.isDirectory -> directoryClassBytesIndexFor(jarOrDir)
entry.isClassPathArchive -> jarClassBytesIndexFor(entry)
entry.isDirectory -> directoryClassBytesIndexFor(entry)
else -> { _ -> null }
}

Expand Down Expand Up @@ -138,6 +138,14 @@ class ClassBytesRepository(classPath: ClassPath, classPathDependencies: ClassPat
}


/**
* See https://docs.oracle.com/javase/8/docs/technotes/tools/findingclasses.html#userclass
*/
private
val File.isClassPathArchive
get() = extension.run { equals("jar", ignoreCase = true) || equals("zip", ignoreCase = true) }


private
val String.isClassFilePath
get() = endsWith(classFileExtension)
Expand Down
Expand Up @@ -21,6 +21,8 @@ import org.gradle.api.tasks.wrapper.Wrapper

import org.gradle.kotlin.dsl.fixtures.AbstractIntegrationTest
import org.gradle.kotlin.dsl.fixtures.DeepThought
import org.gradle.kotlin.dsl.fixtures.LightThought
import org.gradle.kotlin.dsl.fixtures.ZeroThought
import org.gradle.kotlin.dsl.fixtures.customInstallation

import org.hamcrest.CoreMatchers.equalTo
Expand Down Expand Up @@ -142,6 +144,24 @@ class ClassBytesRepositoryTest : AbstractIntegrationTest() {
}
}

@Test
fun `ignores invalid classpath entries`() {

val entries = listOf(
withClassJar("some.JAR", DeepThought::class.java),
withClassJar("some.zip", LightThought::class.java),
withClassJar("some.tar.gz", ZeroThought::class.java),
withFile("some.xml")
)

classPathBytesRepositoryFor(entries).use { repository ->
assertThat(
repository.allSourceNames,
equalTo(listOf(canonicalNameOf<DeepThought>(), canonicalNameOf<LightThought>()))
)
}
}

private
val ClassBytesRepository.allSourceNames: List<String>
get() = allClassesBytesBySourceName().map { it.first }.toList()
Expand Down