Skip to content

Commit 08697e5

Browse files
jpbempeldevflow.devflow-routing-intake
andauthored
Fix Spring MethodParameters detection (#11647)
Fix Spring MethodParameters detection Avoid throwing an exception if Spring is not found in loaded classes Introduced an enum for reporting back result state and use UNKNOWN to fallback to another detection method. spotless Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 64fea60 commit 08697e5

1 file changed

Lines changed: 35 additions & 15 deletions

File tree

  • dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/util

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/util/SpringHelper.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010
public class SpringHelper {
1111
private static final Logger LOGGER = LoggerFactory.getLogger(SpringHelper.class);
1212

13+
private enum DetectionResult {
14+
UNKNOWN,
15+
USE_METHOD_PARAMETERS,
16+
USE_LOCAL_VARS
17+
}
18+
1319
public static boolean isSpringUsingOnlyMethodParameters(Instrumentation inst) {
14-
try {
15-
return isSpringUsingOnlyMethodParametersSpringVersion(inst);
16-
} catch (Exception e) {
17-
LOGGER.debug("isSpringUsingOnlyMethodParameters failed for SpringVersion", e);
20+
DetectionResult detectionResult = isSpringUsingOnlyMethodParametersSpringVersion(inst);
21+
if (detectionResult == DetectionResult.UNKNOWN) {
22+
LOGGER.debug(
23+
"isSpringUsingOnlyMethodParameters failed for SpringVersion, trying to detect specific class");
1824
// fallback to lookup for specific class
1925
return isSpringUsingOnlyMethodParametersSpecificClass(inst);
2026
}
27+
return detectionResult == DetectionResult.USE_METHOD_PARAMETERS;
2128
}
2229

23-
private static boolean isSpringUsingOnlyMethodParametersSpringVersion(Instrumentation inst) {
30+
private static DetectionResult isSpringUsingOnlyMethodParametersSpringVersion(
31+
Instrumentation inst) {
2432
try {
2533
// scan for getting an already loaded class and get the classloader
2634
ClassLoader springClassLoader = null;
@@ -30,29 +38,41 @@ private static boolean isSpringUsingOnlyMethodParametersSpringVersion(Instrument
3038
}
3139
}
3240
if (springClassLoader == null) {
33-
throw new IllegalStateException("Cannot find Spring classloader");
41+
return DetectionResult.UNKNOWN;
3442
}
3543
Class<?> springVersionClass =
3644
Class.forName("org.springframework.core.SpringVersion", true, springClassLoader);
3745
Method m = springVersionClass.getDeclaredMethod("getVersion");
3846
String version = (String) m.invoke(null);
3947
ParsedSpringVersion springVersion = new ParsedSpringVersion(version);
4048
// if Spring version is 6.1+ only using MethodParameters
41-
return springVersion.major > 6 || (springVersion.major == 6 && springVersion.minor >= 1);
42-
} catch (Exception e) {
43-
throw new RuntimeException(e);
49+
return springVersion.major > 6 || (springVersion.major == 6 && springVersion.minor >= 1)
50+
? DetectionResult.USE_METHOD_PARAMETERS
51+
: DetectionResult.USE_LOCAL_VARS;
52+
} catch (Exception ex) {
53+
LOGGER.debug("isSpringUsingOnlyMethodParametersSpringVersion failed", ex);
54+
return DetectionResult.UNKNOWN;
4455
}
4556
}
4657

4758
private static boolean isSpringUsingOnlyMethodParametersSpecificClass(Instrumentation inst) {
48-
for (Class<?> clazz : inst.getAllLoadedClasses()) {
49-
if ("org.springframework.web.client.RestClient".equals(clazz.getName())) {
50-
// If this class (coming from Spring web since version 6.1) is found loaded it means Spring
51-
// supports only getting parameter names from the MethodParameter attribute
52-
return true;
59+
try {
60+
for (Class<?> clazz : inst.getAllLoadedClasses()) {
61+
if (clazz == null) {
62+
// class unloaded
63+
continue;
64+
}
65+
if ("org.springframework.web.client.RestClient".equals(clazz.getName())) {
66+
// If this class (coming from Spring web since version 6.1) is found loaded it means
67+
// Spring
68+
// supports only getting parameter names from the MethodParameter attribute
69+
return true;
70+
}
5371
}
72+
} catch (Exception ex) {
73+
LOGGER.debug("isSpringUsingOnlyMethodParametersSpecificClass failed: ", ex);
5474
}
55-
// class not found, probably no Spring
75+
// class not found, probably no Spring or failed
5676
return false;
5777
}
5878

0 commit comments

Comments
 (0)