Skip to content

Commit b1ed716

Browse files
authored
Add support for Guava and FastUtil (#9727)
Consider guava and fastutil collections and maps as safe so they can be treated as regular collections and maps and use in expression and captured as well use a list for safe packages
1 parent 47f955b commit b1ed716

File tree

1 file changed

+25
-14
lines changed
  • dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/util

1 file changed

+25
-14
lines changed

dd-java-agent/agent-debugger/debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/util/WellKnownClasses.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Date;
1515
import java.util.HashMap;
1616
import java.util.HashSet;
17+
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Optional;
1920
import java.util.OptionalDouble;
@@ -187,6 +188,22 @@ public class WellKnownClasses {
187188
THROWABLE_SPECIAL_FIELDS.put("cause", ThrowableFields::cause);
188189
}
189190

191+
private static final List<String> SAFE_COLLECTION_PACKAGES =
192+
Arrays.asList(
193+
"java.", // JDK base module
194+
"com.google.protobuf.", // Google ProtoBuf
195+
"com.google.common.collect.", // Google Guava
196+
"it.unimi.dsi.fastutil." // fastutil
197+
);
198+
199+
private static final List<String> SAFE_MAP_PACKAGES =
200+
Arrays.asList(
201+
"java.", // JDK base module
202+
"com.google.protobuf.", // Google ProtoBuf
203+
"com.google.common.collect.", // Google Guava
204+
"it.unimi.dsi.fastutil." // fastutil
205+
);
206+
190207
/**
191208
* @return true if type is a final class and toString implementation is well known and side effect
192209
* free
@@ -207,27 +224,21 @@ public static boolean isToStringSafe(String concreteType) {
207224
/** @return true if collection implementation is safe to call (only in-memory) */
208225
public static boolean isSafe(Collection<?> collection) {
209226
String className = collection.getClass().getTypeName();
210-
if (className.startsWith("java.")) {
211-
// All Collection implementations from JDK base module are considered as safe
212-
return true;
213-
}
214-
if (className.startsWith("com.google.protobuf.")) {
215-
// All Collection implementations from Google ProtoBuf are considered as safe
216-
return true;
227+
for (String safePackage : SAFE_COLLECTION_PACKAGES) {
228+
if (className.startsWith(safePackage)) {
229+
return true;
230+
}
217231
}
218232
return false;
219233
}
220234

221235
/** @return true if map implementation is safe to call (only in-memory) */
222236
public static boolean isSafe(Map<?, ?> map) {
223237
String className = map.getClass().getTypeName();
224-
if (className.startsWith("java.")) {
225-
// All Map implementations from JDK base module are considered as safe
226-
return true;
227-
}
228-
if (className.startsWith("com.google.protobuf.")) {
229-
// All Map implementations from Google ProtoBuf are considered as safe
230-
return true;
238+
for (String safePackage : SAFE_MAP_PACKAGES) {
239+
if (className.startsWith(safePackage)) {
240+
return true;
241+
}
231242
}
232243
return false;
233244
}

0 commit comments

Comments
 (0)