Skip to content

Commit

Permalink
Use reflection when calling DeprecatableConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
bigdaz committed Apr 6, 2024
1 parent 5e4f027 commit 1a9efb4
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import org.gradle.api.artifacts.Configuration
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.gradle.dependencygraph.extractor.ResolvedConfigurationFilter
import org.gradle.internal.deprecation.DeprecatableConfiguration
import org.gradle.work.DisableCachingByDefault

import java.lang.reflect.Method

@DisableCachingByDefault(because = "Not worth caching")
abstract class LegacyResolveProjectDependenciesTask: DefaultTask() {
private val canSafelyBeResolvedMethod: Method? = getCanSafelyBeResolvedMethod()

@Internal
var configurationFilter: ResolvedConfigurationFilter? = null

Expand All @@ -20,17 +21,31 @@ abstract class LegacyResolveProjectDependenciesTask: DefaultTask() {
}
}

/**
* If `DeprecatableConfiguration.canSafelyBeResolve()` is available, use it.
* Else fall back to `Configuration.canBeResolved`.
*/
private fun canBeResolved(configuration: Configuration): Boolean {
if (canSafelyBeResolvedMethod != null) {
return canSafelyBeResolvedMethod.invoke(configuration) as Boolean
}
return configuration.isCanBeResolved
}

@TaskAction
fun action() {
for (configuration in getReportableConfigurations()) {
println("Resolving ${configuration.name}")
configuration.incoming.resolutionResult.root
}
}

private fun canBeResolved(configuration: Configuration): Boolean {
if (configuration is DeprecatableConfiguration) {
return configuration.canSafelyBeResolved()
private fun getCanSafelyBeResolvedMethod(): Method? {
return try {
val dc = Class.forName("org.gradle.internal.deprecation.DeprecatableConfiguration")
dc.getMethod("canSafelyBeResolved")
} catch (e: ReflectiveOperationException) {
null
}
return configuration.isCanBeResolved
}
}

0 comments on commit 1a9efb4

Please sign in to comment.