A new task to build type usage report (ComputeTypeUsageTask).#1639
A new task to build type usage report (ComputeTypeUsageTask).#1639Laimiux wants to merge 3 commits intoautonomousapps:mainfrom
Conversation
Improves API clarity by making the property name match the excludePackages and excludeTypes pattern. Also removes unnecessary dependsOn from computeAdviceTask - the type usage task should only run when explicitly invoked, not as part of buildHealth. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
| public abstract class AbstractExtension { | ||
| ctor @javax.inject.Inject public AbstractExtension(org.gradle.api.model.ObjectFactory objects, org.gradle.api.invocation.Gradle gradle); | ||
| method public final org.gradle.api.file.RegularFileProperty adviceOutput(); | ||
| method public final org.gradle.api.file.RegularFileProperty typeUsageOutput(); |
There was a problem hiding this comment.
Please provide guidance here - I've added this, but I'm not sure what should actually be included here.
|
|
||
| import static com.autonomousapps.kit.gradle.dependencies.Dependencies.* | ||
|
|
||
| final class TypeUsageProject extends AbstractProject { |
There was a problem hiding this comment.
Added a functional test for this task (tried to follow project conventions)
|
|
||
| import static com.autonomousapps.kit.gradle.dependencies.Dependencies.* | ||
|
|
||
| final class TypeUsageWithFiltersProject extends AbstractProject { |
There was a problem hiding this comment.
Another functional test for the new task (tried to follow project conventions)
| import static com.autonomousapps.utils.Runner.build | ||
| import static com.google.common.truth.Truth.assertThat | ||
|
|
||
| final class TypeUsageSpec extends AbstractJvmSpec { |
There was a problem hiding this comment.
Following project convention to test computeTypeUsageMain task
| // Excluded specific types | ||
| internal val excludedTypes: SetProperty<String> = objects.setProperty(String::class.java) | ||
| .convention(setOf( | ||
| "kotlin.Unit", |
There was a problem hiding this comment.
Curious if we should provide certain defaults here for exclusion out of the report
| public fun getAllLibsVersionsTomlPath(): String = "$ROOT_DIR/allLibs.versions.toml" | ||
| public fun getResolvedDependenciesReport(): String = "$ROOT_DIR/resolved-dependencies-report.txt" | ||
| public fun getResolvedVersionsTomlPath(): String = "$ROOT_DIR/resolvedAllLibs.versions.toml" | ||
| public fun getTypeUsagePath(variantName: String = "main"): String = "$ROOT_DIR/$variantName/intermediates/type-usage.json" |
There was a problem hiding this comment.
TODO: it should probably not live in intermediates
| * enabling coupling analysis and complexity metrics. | ||
| */ | ||
| @JsonClass(generateAdapter = false) | ||
| public data class ProjectTypeUsage( |
There was a problem hiding this comment.
Json serialized models for type usage task.
| } | ||
|
|
||
| // Computes type-level usage statistics for complexity analysis. | ||
| val computeTypeUsageTask = tasks.register("computeTypeUsage$taskNameSuffix", ComputeTypeUsageTask::class.java) { t -> |
| @@ -0,0 +1,94 @@ | |||
| // Copyright (c) 2025. Tony Robalik. | |||
|
|
||
| // Count usage per class per dependency (both non-annotation and annotation classes) | ||
| project.codeSource.forEach { source -> | ||
| val allUsedClasses = source.usedNonAnnotationClasses + source.usedAnnotationClasses |
There was a problem hiding this comment.
Should we include usedAnnotationClasses ?
Summary
A new task to build type usage report (#1637).
Introducing a new
ComputeTypeUsageTasktask. It generatestype-usage.jsonreporting which types (classes) your code uses from each dependency. Designed for analyzing module dependency usage and complexity.{ "projectPath": ":app", "summary": { "totalTypes": 245, "totalFiles": 67, "internalTypes": 12, "projectDependencies": 3, "libraryDependencies": 18 }, "internal": { "com.example.MyClass": 5 }, "projectDependencies": { ":core": { "com.example.core.Utils": 2 } }, "libraryDependencies": { "org.apache.commons:commons-collections4": { "org.apache.commons.collections4.bag.HashBag": 3 } } }Usage:
Output location:
Configuration:
dependencyAnalysis { typeUsage { excludePackages("kotlin.jvm.internal") excludeTypes("kotlin.Unit") excludeRegex(".*_Factory$", ".*Companion$") } }Key design decisions:
buildHealthor other critical paths)synthetic-project.json,exploded-jars.json.gz)