Skip to content
Draft
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
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ gradlePlugin {
plugins {
create("instrument-plugin") {
id = "dd-trace-java.instrument"
implementationClass = "InstrumentPlugin"
implementationClass = "datadog.gradle.plugin.instrument.InstrumentPlugin"
}

create("muzzle-plugin") {
Expand Down
209 changes: 0 additions & 209 deletions buildSrc/src/main/groovy/InstrumentPlugin.groovy

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package datadog.gradle.plugin.instrument

import net.bytebuddy.ClassFileVersion
import net.bytebuddy.build.EntryPoint
import net.bytebuddy.build.Plugin
Expand All @@ -12,8 +14,8 @@ import org.slf4j.LoggerFactory
* Performs build-time instrumentation of classes, called indirectly from InstrumentPlugin.
* (This is the byte-buddy side of the task; InstrumentPlugin contains the Gradle pieces.)
*/
class InstrumentingPlugin {
static final Logger log = LoggerFactory.getLogger(InstrumentingPlugin.class)
class ByteBuddyInstrumenter {
static final Logger log = LoggerFactory.getLogger(ByteBuddyInstrumenter.class)

static void instrumentClasses(
String[] plugins, ClassLoader instrumentingLoader, File sourceDirectory, File targetDirectory)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package datadog.gradle.plugin.instrument

import java.nio.file.Path
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import org.gradle.api.file.FileSystemOperations
import org.gradle.api.model.ObjectFactory
import org.gradle.workers.WorkAction

abstract class InstrumentAction implements WorkAction<InstrumentWorkParameters> {
private static final Object lock = new Object()
private static final Map<String, ClassLoader> classLoaderCache = new ConcurrentHashMap<>()
private static volatile long lastBuildStamp

@Inject
public abstract FileSystemOperations getFileSystemOperations();

@Inject
public abstract ObjectFactory getObjects();

@Override
void execute() {
String[] plugins = parameters.getPlugins().get() as String[]
String classLoaderKey = plugins.join(':')

// reset shared class-loaders each time a new build starts
long buildStamp = parameters.buildStartedTime.get()
ClassLoader pluginCL = classLoaderCache.get(classLoaderKey)
if (lastBuildStamp < buildStamp || !pluginCL) {
synchronized (lock) {
pluginCL = classLoaderCache.get(classLoaderKey)
if (lastBuildStamp < buildStamp || !pluginCL) {
pluginCL = createClassLoader(parameters.pluginClassPath)
classLoaderCache.put(classLoaderKey, pluginCL)
lastBuildStamp = buildStamp
}
}
}
Path classesDirectory = parameters.compilerOutputDirectory.get().asFile.toPath()
Path tmpUninstrumentedDir = parameters.tmpDirectory.get().asFile.toPath()

// Original classes will be replaced by post-processed ones
fileSystemOperations.sync {
from(classesDirectory)
into(tmpUninstrumentedDir)
}
fileSystemOperations.delete {
delete(objects.fileTree().from(classesDirectory))
}

ClassLoader instrumentingCL = createClassLoader(parameters.instrumentingClassPath, pluginCL)
ByteBuddyInstrumenter.instrumentClasses(plugins, instrumentingCL, tmpUninstrumentedDir.toFile(), classesDirectory.toFile())
}

static ClassLoader createClassLoader(cp, parent = InstrumentAction.classLoader) {
return new URLClassLoader(cp*.toURI()*.toURL() as URL[], parent as ClassLoader)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package datadog.gradle.plugin.instrument

import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty

abstract class InstrumentExtension {
abstract ListProperty<String> getPlugins()
abstract ListProperty<DirectoryProperty> getAdditionalClasspath()
}
Loading
Loading