Skip to content

Commit e95e91e

Browse files
committed
feat(agent): Move forced class preload into instrumenter module
1 parent 1ee99fb commit e95e91e

2 files changed

Lines changed: 26 additions & 31 deletions

File tree

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/InstrumenterModule.java

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ public int order() {
9090
}
9191

9292
public List<Instrumenter> typeInstrumentations() {
93+
preloadClasses();
9394
return singletonList(this);
9495
}
9596

@@ -214,6 +215,28 @@ protected final boolean isShortcutMatchingEnabled(boolean defaultToShortcut) {
214215
.isIntegrationShortcutMatchingEnabled(singletonList(name()), defaultToShortcut);
215216
}
216217

218+
/**
219+
* Force loading of classes that need to be instrumented, but are using during instrumentation.
220+
*/
221+
@SuppressForbidden // allow this use of Class.forName()
222+
protected void preloadClasses() {
223+
String[] list = preloadClassNames();
224+
if (list != null) {
225+
for (String clazz : list) {
226+
try {
227+
Class.forName(clazz);
228+
} catch (Throwable t) {
229+
log.debug("Error force loading {} class", clazz);
230+
}
231+
}
232+
}
233+
}
234+
235+
/** Get classes to force load */
236+
public String[] preloadClassNames() {
237+
return null;
238+
}
239+
217240
/** Parent class for all tracing related instrumentations */
218241
public abstract static class Tracing extends InstrumenterModule {
219242
public Tracing(String instrumentationName, String... additionalNames) {
@@ -258,18 +281,11 @@ public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
258281
}
259282

260283
/** Parent class for all IAST related instrumentations */
261-
@SuppressForbidden
262284
public abstract static class Iast extends InstrumenterModule {
263285
public Iast(String instrumentationName, String... additionalNames) {
264286
super(instrumentationName, additionalNames);
265287
}
266288

267-
@Override
268-
public List<Instrumenter> typeInstrumentations() {
269-
preloadClassNames();
270-
return super.typeInstrumentations();
271-
}
272-
273289
@Override
274290
public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
275291
if (enabledSystems.contains(TargetSystem.IAST)) {
@@ -282,27 +298,6 @@ public final boolean isApplicable(Set<TargetSystem> enabledSystems) {
282298
return cfg.getAppSecActivation() == ProductActivation.FULLY_ENABLED;
283299
}
284300

285-
/**
286-
* Force loading of classes that need to be instrumented, but are using during instrumentation.
287-
*/
288-
private void preloadClassNames() {
289-
String[] list = getClassNamesToBePreloaded();
290-
if (list != null) {
291-
for (String clazz : list) {
292-
try {
293-
Class.forName(clazz);
294-
} catch (Throwable t) {
295-
log.debug("Error force loading {} class", clazz);
296-
}
297-
}
298-
}
299-
}
300-
301-
/** Get classes to force load* */
302-
public String[] getClassNamesToBePreloaded() {
303-
return null;
304-
}
305-
306301
@Override
307302
public Advice.PostProcessor.Factory postProcessor() {
308303
return IastPostProcessorFactory.INSTANCE;

dd-java-agent/instrumentation/java/java-io-1.8/src/main/java/datadog/trace/instrumentation/java/lang/InputStreamInstrumentation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
public class InputStreamInstrumentation extends InstrumenterModule.Iast
2121
implements Instrumenter.ForTypeHierarchy, Instrumenter.HasMethodAdvice {
2222

23-
private static final String[] FORCE_LOADING = {"java.io.PushbackInputStream"};
23+
private static final String[] PRELOAD_CLASS_NAMES = {"java.io.PushbackInputStream"};
2424

2525
public InputStreamInstrumentation() {
2626
super("inputStream");
@@ -44,8 +44,8 @@ public void methodAdvice(MethodTransformer transformer) {
4444
}
4545

4646
@Override
47-
public String[] getClassNamesToBePreloaded() {
48-
return FORCE_LOADING;
47+
public String[] preloadClassNames() {
48+
return PRELOAD_CLASS_NAMES;
4949
}
5050

5151
public static class InputStreamAdvice {

0 commit comments

Comments
 (0)