Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve jdeps tracking and add test cases #1115

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 53 additions & 32 deletions src/main/kotlin/io/bazel/kotlin/plugin/jdeps/JdepsGenExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor
import org.jetbrains.kotlin.extensions.StorageComponentContainerContributor
import org.jetbrains.kotlin.load.java.descriptors.JavaMethodDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaPropertyDescriptor
import org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaClassDescriptor
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaClass
import org.jetbrains.kotlin.load.java.structure.impl.classFiles.BinaryJavaField
Expand All @@ -45,6 +46,7 @@ import org.jetbrains.kotlin.types.typeUtil.supertypes
import java.io.BufferedOutputStream
import java.io.File
import java.nio.file.Paths
import java.util.*

/**
* Kotlin compiler extension that tracks classes (and corresponding classpath jars) needed to
Expand Down Expand Up @@ -105,6 +107,18 @@ class JdepsGenExtension(
)
}
}

fun getResourceName(descriptor: DeclarationDescriptorWithSource): String? {
if (descriptor.containingDeclaration is LazyJavaClassDescriptor) {
val fqName: String? = (descriptor.containingDeclaration as LazyJavaClassDescriptor)?.jClass?.fqName?.asString()
if (fqName != null) {
if (fqName.indexOf(".R.") > 0 || fqName.indexOf("R.") == 0) {
return fqName + "." + descriptor.name.asString()
}
}
}
return null
}
}

private val explicitClassesCanonicalPaths = mutableSetOf<String>()
Expand Down Expand Up @@ -143,8 +157,9 @@ class JdepsGenExtension(
)?.let { explicitClassesCanonicalPaths.add(it) }
}
is FunctionDescriptor -> {
resultingDescriptor.returnType?.let { addImplicitDep(it) }
resultingDescriptor.returnType?.let {
collectTypeReferences(it, isExplicit = false, collectTypeArguments = false)
collectTypeReferences(it, isExplicit = false)
}
resultingDescriptor.valueParameters.forEach { valueParameter ->
collectTypeReferences(valueParameter.type, isExplicit = false)
Expand All @@ -162,6 +177,7 @@ class JdepsGenExtension(
}
is JavaPropertyDescriptor -> {
getClassCanonicalPath(resultingDescriptor)?.let { explicitClassesCanonicalPaths.add(it) }
getResourceName(resultingDescriptor)?.let { explicitClassesCanonicalPaths.add(it) }
}
is PropertyDescriptor -> {
when (resultingDescriptor.containingDeclaration) {
Expand All @@ -175,7 +191,7 @@ class JdepsGenExtension(
explicitClassesCanonicalPaths.add(virtualFileClass.file.path)
}
}
collectTypeReferences(resultingDescriptor.type, isExplicit = false)
addImplicitDep(resultingDescriptor.type)
}
else -> return
}
Expand All @@ -191,6 +207,9 @@ class JdepsGenExtension(
descriptor.typeConstructor.supertypes.forEach {
collectTypeReferences(it)
}
descriptor.annotations.forEach { annotation ->
collectTypeReferences(annotation.type)
}
}
is FunctionDescriptor -> {
descriptor.returnType?.let { collectTypeReferences(it) }
Expand Down Expand Up @@ -219,6 +238,14 @@ class JdepsGenExtension(
}
}

private fun addImplicitDep(it: KotlinType) {
getClassCanonicalPath(it.constructor)?.let { implicitClassesCanonicalPaths.add(it) }
}

private fun addExplicitDep(it: KotlinType) {
getClassCanonicalPath(it.constructor)?.let { explicitClassesCanonicalPaths.add(it) }
}

/**
* Records direct and indirect references for a given type. Direct references are explicitly
* used in the code, e.g: a type declaration or a generic type declaration. Indirect references
Expand All @@ -228,41 +255,35 @@ class JdepsGenExtension(
private fun collectTypeReferences(
kotlinType: KotlinType,
isExplicit: Boolean = true,
collectTypeArguments: Boolean = true,
visitedKotlinTypes: MutableSet<Pair<KotlinType, Boolean>> = mutableSetOf(),
) {
val kotlintTypeAndIsExplicit = Pair(kotlinType, isExplicit)
if (!visitedKotlinTypes.contains(kotlintTypeAndIsExplicit)) {
visitedKotlinTypes.add(kotlintTypeAndIsExplicit)
if (isExplicit) {
addExplicitDep(kotlinType)
} else {
addImplicitDep(kotlinType)
}

kotlinType.supertypes().forEach {
addImplicitDep(it)
}

collectTypeArguments(kotlinType, isExplicit)
}

private fun collectTypeArguments(
kotlinType: KotlinType,
isExplicit: Boolean,
visitedKotlinTypes: MutableSet<KotlinType> = mutableSetOf(),
) {
visitedKotlinTypes.add(kotlinType)
kotlinType.arguments.map { it.type }.forEach { typeArgument ->
if (isExplicit) {
getClassCanonicalPath(kotlinType.constructor)?.let {
explicitClassesCanonicalPaths.add(it)
}
addExplicitDep(typeArgument)
} else {
getClassCanonicalPath(kotlinType.constructor)?.let {
implicitClassesCanonicalPaths.add(it)
}
addImplicitDep(typeArgument)
}

kotlinType.supertypes().forEach { supertype ->
collectTypeReferences(
supertype,
isExplicit = false,
collectTypeArguments = collectTypeArguments,
visitedKotlinTypes,
)
}

if (collectTypeArguments) {
kotlinType.arguments.map { it.type }.forEach { typeArgument ->
collectTypeReferences(
typeArgument,
isExplicit = isExplicit,
collectTypeArguments = true,
visitedKotlinTypes = visitedKotlinTypes,
)
}
typeArgument.supertypes().forEach { addImplicitDep(it) }
if (!visitedKotlinTypes.contains(typeArgument)) {
collectTypeArguments(typeArgument, isExplicit, visitedKotlinTypes)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,138 @@ class KotlinBuilderJvmJdepsTest(private val enableK2Compiler: Boolean) {
assertIncomplete(jdeps).isEmpty()
}

@Test
fun `java annotation on class is an explict dep`() {
val dependingTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"AnotherClass.kt",
"""
package something

@JavaAnnotation
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case demonstrates how it fails to track runtime class annotations as a used type.

class AnotherClass {
}
"""
)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}
val jdeps = depsProto(dependingTarget)

assertThat(jdeps.ruleLabel).isEqualTo(dependingTarget.label())

assertExplicit(jdeps).contains(TEST_FIXTURES_DEP.singleCompileJar())
assertImplicit(jdeps).isEmpty()
assertUnused(jdeps).isEmpty()
assertIncomplete(jdeps).isEmpty()
}

@Test
fun `java annotation values on class are an explict dep`() {
val dependentTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"ClassToReference.kt",
"""
package something;

@JavaAnnotation
class ClassToReference
"""
)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}

val anotherDepTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"AnotherClassToReference.kt",
"""
package something;

@JavaAnnotation(modules = [ClassToReference::class])
class AnotherClassToReference
"""
)
c.addDirectDependencies(dependentTarget)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}

val dependingTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"AnotherClass.kt",
"""
package something

@JavaAnnotation(modules = [AnotherClassToReference::class])
class AnotherClass {
}
"""
)
c.addTransitiveDependencies(dependentTarget)
c.addDirectDependencies(anotherDepTarget)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}
val jdeps = depsProto(dependingTarget)

assertThat(jdeps.ruleLabel).isEqualTo(dependingTarget.label())

assertExplicit(jdeps).contains(TEST_FIXTURES_DEP.singleCompileJar())
assertExplicit(jdeps).contains(anotherDepTarget.singleCompileJar())
assertImplicit(jdeps).contains(dependentTarget.singleCompileJar())
assertUnused(jdeps).isEmpty()
assertIncomplete(jdeps).isEmpty()
}

@Test
fun `types referenced inside when statements are explicit deps`() {
val dependingTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"AnotherClass.kt",
"""
package something

import pkg.assertion.ExampleException

fun mapExceptions(exception: Exception) = when (exception) {
is ExampleException -> exception
else -> exception
}
"""
)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}
val jdeps = depsProto(dependingTarget)

assertThat(jdeps.ruleLabel).isEqualTo(dependingTarget.label())

assertExplicit(jdeps).contains(TEST_FIXTURES_DEP.singleCompileJar())
assertImplicit(jdeps).isEmpty()
assertUnused(jdeps).isEmpty()
assertIncomplete(jdeps).isEmpty()
}

@Test
fun `java classes with inheritance`() {
val dependingTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
c.addSource(
"AnotherClass.kt",
"""
package something

class SomeClass : SomeClassWithInheritance.BaseClassCallback {
}
"""
)
c.addDirectDependencies(TEST_FIXTURES_DEP)
}
val jdeps = depsProto(dependingTarget)

assertThat(jdeps.ruleLabel).isEqualTo(dependingTarget.label())

assertExplicit(jdeps).contains(TEST_FIXTURES_DEP.singleCompileJar())
assertImplicit(jdeps).isEmpty()
assertUnused(jdeps).isEmpty()
assertIncomplete(jdeps).isEmpty()
}

@Test
fun `java annotation on property is an explict dep`() {
val dependingTarget = runJdepsCompileTask { c: KotlinJvmTestBuilder.TaskBuilder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package something;

public abstract class BaseClass {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pkg.assertion;

public class ExampleException extends RuntimeException {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;

@Retention(RetentionPolicy.RUNTIME)
public @interface JavaAnnotation {
}

Class<?>[] modules() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package something;

public class SomeClassWithInheritance extends BaseClass {


interface BaseClassCallback {

}
}
Loading