Skip to content

Commit 25cbf8e

Browse files
authored
Refactor plugin ids (#299)
_This is PR 1 of 3. The other two are: #300 and #301_ This PR is a segue to the next PR which does the actual job. Unfortunately, we cannot apply Java plugin if the user applies the Android plugin because these two conflict on each other. We trust the user to apply the correct plugin in their Gradle script but we wouldn't force it. To avoid the performance overhead when checking for available plugins, we should reference them using their plugin IDs instead of actual class. Therefore the type of collection was changed from `<Class<out Plugin<Project>>>` to just `<String>`. Because this change only alters the internals but does not alter the behavior, i.e. "everything works as before", I call it a "refactoring PR". To get the idea how this change is used, check out the next PR #300
1 parent dd7e90f commit 25cbf8e

File tree

5 files changed

+9
-6
lines changed

5 files changed

+9
-6
lines changed

plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationDistributionPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class JavaagentApplicationDistributionPlugin :
2323
*/
2424
private val destinationDirectory = "agent-libs"
2525

26-
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(ApplicationPlugin::class.java)
26+
override fun dependentProjectPlugins(): Collection<String> = setOf("application")
2727

2828
override fun applyAfterJavaagentSetup(
2929
project: Project,

plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentApplicationRunPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.gradle.api.tasks.JavaExec
1313
class JavaagentApplicationRunPlugin :
1414
Plugin<Project>,
1515
JavaagentPlugin {
16-
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(ApplicationPlugin::class.java)
16+
override fun dependentProjectPlugins(): Collection<String> = setOf("application")
1717

1818
override fun applyAfterJavaagentSetup(
1919
project: Project,

plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentPlugin.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface JavaagentPlugin : Plugin<Project> {
1313
* Plugins that the implementing class expects to be applied before [applyAfterJavaagentSetup] runs. By default,
1414
* this returns an empty [Collection]
1515
*/
16-
fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = emptySet()
16+
fun dependentProjectPlugins(): Collection<String> = emptySet()
1717

1818
/**
1919
* Initial setup for any plugin that wants to configure a javaagent for a project, followed by delegating to the
@@ -24,8 +24,10 @@ interface JavaagentPlugin : Plugin<Project> {
2424
project.pluginManager.apply(JavaagentBasePlugin::class.java)
2525
// get configuration
2626
val javaagentConfiguration = project.configurations.named(JavaagentBasePlugin.CONFIGURATION_NAME)
27-
dependentProjectPlugins().forEach { pluginClass ->
28-
project.pluginManager.apply(pluginClass)
27+
dependentProjectPlugins().forEach { pluginId ->
28+
if (project.pluginManager.hasPlugin(pluginId)) {
29+
project.pluginManager.apply(pluginId)
30+
}
2931
}
3032
applyAfterJavaagentSetup(project, javaagentConfiguration)
3133
}

plugin/src/main/kotlin/com/ryandens/javaagent/JavaagentTestPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class JavaagentTestPlugin :
1616
const val CONFIGURATION_NAME = "testJavaagent"
1717
}
1818

19-
override fun dependentProjectPlugins(): Collection<Class<out Plugin<Project>>> = setOf(JavaPlugin::class.java)
19+
override fun dependentProjectPlugins(): Collection<String> = setOf("java")
2020

2121
override fun applyAfterJavaagentSetup(
2222
project: Project,

plugin/src/test/kotlin/com/ryandens/javaagent/JavaagentTestPluginTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class JavaagentTestPluginTest {
1717
*/
1818
@BeforeTest fun beforeEach() {
1919
project = ProjectBuilder.builder().build()
20+
project.plugins.apply("java")
2021
project.plugins.apply("com.ryandens.javaagent-test")
2122
project.dependencies.add("javaagent", "io.opentelemetry.javaagent:opentelemetry-javaagent:1.11.1")
2223
}

0 commit comments

Comments
 (0)