-
Notifications
You must be signed in to change notification settings - Fork 850
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate codegen to be a plugin (#3401)
- Loading branch information
Anuraag Agrawal
authored
Jun 24, 2021
1 parent
1f3faca
commit 220ea41
Showing
7 changed files
with
192 additions
and
287 deletions.
There are no files selected for viewing
139 changes: 0 additions & 139 deletions
139
...n/java/io/opentelemetry/instrumentation/gradle/bytebuddy/ByteBuddyPluginConfigurator.java
This file was deleted.
Oops, something went wrong.
90 changes: 0 additions & 90 deletions
90
...main/java/io/opentelemetry/instrumentation/gradle/bytebuddy/ClasspathByteBuddyPlugin.java
This file was deleted.
Oops, something went wrong.
45 changes: 0 additions & 45 deletions
45
.../main/java/io/opentelemetry/instrumentation/gradle/bytebuddy/ClasspathTransformation.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
...c/main/kotlin/io/opentelemetry/instrumentation/gradle/codegen/ClasspathByteBuddyPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.opentelemetry.instrumentation.gradle.codegen | ||
|
||
import net.bytebuddy.ByteBuddy | ||
import net.bytebuddy.build.Plugin | ||
import net.bytebuddy.description.type.TypeDescription | ||
import net.bytebuddy.dynamic.ClassFileLocator | ||
import net.bytebuddy.dynamic.DynamicType | ||
import java.io.File | ||
import java.net.URL | ||
import java.net.URLClassLoader | ||
|
||
/** | ||
* Starting from version 1.10.15, ByteBuddy gradle plugin transformations require that plugin | ||
* classes are given as class instances instead of a class name string. To be able to still use a | ||
* plugin implementation that is not a buildscript dependency, this reimplements the previous logic | ||
* by taking a delegate class name and class path as arguments and loading the plugin class from the | ||
* provided classloader when the plugin is instantiated. | ||
*/ | ||
class ClasspathByteBuddyPlugin( | ||
classPath: Iterable<File>, sourceDirectory: File, className: String | ||
) : Plugin { | ||
private val delegate = pluginFromClassPath(classPath, sourceDirectory, className) | ||
|
||
override fun apply( | ||
builder: DynamicType.Builder<*>, | ||
typeDescription: TypeDescription, | ||
classFileLocator: ClassFileLocator | ||
): DynamicType.Builder<*> { | ||
return delegate.apply(builder, typeDescription, classFileLocator) | ||
} | ||
|
||
override fun close() { | ||
delegate.close() | ||
} | ||
|
||
override fun matches(typeDefinitions: TypeDescription): Boolean { | ||
return delegate.matches(typeDefinitions) | ||
} | ||
|
||
companion object { | ||
private fun pluginFromClassPath( | ||
classPath: Iterable<File>, sourceDirectory: File, className: String | ||
): Plugin { | ||
val classLoader = classLoaderFromClassPath(classPath, sourceDirectory) | ||
try { | ||
val clazz = Class.forName(className, false, classLoader) | ||
return clazz.getDeclaredConstructor().newInstance() as Plugin | ||
} catch (e: Exception) { | ||
throw IllegalStateException("Failed to create ByteBuddy plugin instance", e) | ||
} | ||
} | ||
|
||
private fun classLoaderFromClassPath( | ||
classPath: Iterable<File>, sourceDirectory: File | ||
): ClassLoader { | ||
val urls = mutableListOf<URL>() | ||
urls.add(fileAsUrl(sourceDirectory)) | ||
for (file in classPath) { | ||
urls.add(fileAsUrl(file)) | ||
} | ||
return URLClassLoader(urls.toTypedArray(), ByteBuddy::class.java.classLoader) | ||
} | ||
|
||
private fun fileAsUrl(file: File): URL { | ||
return file.toURI().toURL() | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rc/main/kotlin/io/opentelemetry/instrumentation/gradle/codegen/ClasspathTransformation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package io.opentelemetry.instrumentation.gradle.codegen | ||
|
||
import net.bytebuddy.build.Plugin.Factory.UsingReflection.ArgumentResolver | ||
import net.bytebuddy.build.gradle.Transformation | ||
import org.gradle.api.tasks.Classpath | ||
import org.gradle.api.tasks.Input | ||
import java.io.File | ||
|
||
/** | ||
* Special implementation of [Transformation] is required as classpath argument must be | ||
* exposed to Gradle via [Classpath] annotation, which cannot be done if it is returned by | ||
* [Transformation.getArguments]. | ||
*/ | ||
class ClasspathTransformation( | ||
@get:Classpath val classpath: Iterable<File>, | ||
@get:Input val pluginClassName: String | ||
) : Transformation() { | ||
override fun makeArgumentResolvers(): List<ArgumentResolver> { | ||
return listOf( | ||
ArgumentResolver.ForIndex(0, classpath), | ||
ArgumentResolver.ForIndex(2, pluginClassName) | ||
) | ||
} | ||
} |
Oops, something went wrong.