Skip to content

Commit d3a2cd2

Browse files
committed
chore: Split single plugin file to separate types
1 parent 210d17c commit d3a2cd2

File tree

9 files changed

+293
-271
lines changed

9 files changed

+293
-271
lines changed

buildSrc/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ gradlePlugin {
1616
plugins {
1717
create("instrument-plugin") {
1818
id = "dd-trace-java.instrument"
19-
implementationClass = "InstrumentPlugin"
19+
implementationClass = "datadog.gradle.plugin.instrument.InstrumentPlugin"
2020
}
2121

2222
create("muzzle-plugin") {

buildSrc/src/main/groovy/InstrumentPlugin.groovy

Lines changed: 0 additions & 268 deletions
This file was deleted.

buildSrc/src/main/groovy/InstrumentingPlugin.groovy renamed to buildSrc/src/main/groovy/datadog/gradle/plugin/instrument/ByteBuddyInstrumenter.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package datadog.gradle.plugin.instrument
2+
13
import net.bytebuddy.ClassFileVersion
24
import net.bytebuddy.build.EntryPoint
35
import net.bytebuddy.build.Plugin
@@ -12,8 +14,8 @@ import org.slf4j.LoggerFactory
1214
* Performs build-time instrumentation of classes, called indirectly from InstrumentPlugin.
1315
* (This is the byte-buddy side of the task; InstrumentPlugin contains the Gradle pieces.)
1416
*/
15-
class InstrumentingPlugin {
16-
static final Logger log = LoggerFactory.getLogger(InstrumentingPlugin.class)
17+
class ByteBuddyInstrumenter {
18+
static final Logger log = LoggerFactory.getLogger(ByteBuddyInstrumenter.class)
1719

1820
static void instrumentClasses(
1921
String[] plugins, ClassLoader instrumentingLoader, File sourceDirectory, File targetDirectory)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package datadog.gradle.plugin.instrument
2+
3+
import java.nio.file.Path
4+
import java.util.concurrent.ConcurrentHashMap
5+
import javax.inject.Inject
6+
import org.gradle.api.file.FileSystemOperations
7+
import org.gradle.api.model.ObjectFactory
8+
import org.gradle.workers.WorkAction
9+
10+
abstract class InstrumentAction implements WorkAction<InstrumentWorkParameters> {
11+
private static final Object lock = new Object()
12+
private static final Map<String, ClassLoader> classLoaderCache = new ConcurrentHashMap<>()
13+
private static volatile long lastBuildStamp
14+
15+
@Inject
16+
public abstract FileSystemOperations getFileSystemOperations();
17+
18+
@Inject
19+
public abstract ObjectFactory getObjects();
20+
21+
@Override
22+
void execute() {
23+
String[] plugins = parameters.getPlugins().get() as String[]
24+
String classLoaderKey = plugins.join(':')
25+
26+
// reset shared class-loaders each time a new build starts
27+
long buildStamp = parameters.buildStartedTime.get()
28+
ClassLoader pluginCL = classLoaderCache.get(classLoaderKey)
29+
if (lastBuildStamp < buildStamp || !pluginCL) {
30+
synchronized (lock) {
31+
pluginCL = classLoaderCache.get(classLoaderKey)
32+
if (lastBuildStamp < buildStamp || !pluginCL) {
33+
pluginCL = createClassLoader(parameters.pluginClassPath)
34+
classLoaderCache.put(classLoaderKey, pluginCL)
35+
lastBuildStamp = buildStamp
36+
}
37+
}
38+
}
39+
Path classesDirectory = parameters.compilerOutputDirectory.get().asFile.toPath()
40+
Path tmpUninstrumentedDir = parameters.tmpDirectory.get().asFile.toPath()
41+
42+
// Original classes will be replaced by post-processed ones
43+
fileSystemOperations.sync {
44+
from(classesDirectory)
45+
into(tmpUninstrumentedDir)
46+
}
47+
fileSystemOperations.delete {
48+
delete(objects.fileTree().from(classesDirectory))
49+
}
50+
51+
ClassLoader instrumentingCL = createClassLoader(parameters.instrumentingClassPath, pluginCL)
52+
InstrumentingPlugin.instrumentClasses(plugins, instrumentingCL, tmpUninstrumentedDir.toFile(), classesDirectory.toFile())
53+
}
54+
55+
static ClassLoader createClassLoader(cp, parent = InstrumentAction.classLoader) {
56+
return new URLClassLoader(cp*.toURI()*.toURL() as URL[], parent as ClassLoader)
57+
}
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package datadog.gradle.plugin.instrument
2+
3+
import org.gradle.api.file.DirectoryProperty
4+
import org.gradle.api.provider.ListProperty
5+
6+
abstract class InstrumentExtension {
7+
abstract ListProperty<String> getPlugins()
8+
abstract ListProperty<DirectoryProperty> getAdditionalClasspath()
9+
}

0 commit comments

Comments
 (0)