Skip to content

Commit 8f62fe5

Browse files
committed
Move isAnonymousType check to the only place that uses it: IastInstrumentation
1 parent 2f91337 commit 8f62fe5

File tree

4 files changed

+23
-23
lines changed

4 files changed

+23
-23
lines changed

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/bytebuddy/outline/TypeFactory.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,6 @@ public TypeList.Generic getInterfaces() {
383383
return outline().getInterfaces();
384384
}
385385

386-
@Override
387-
public boolean isAnonymousType() {
388-
return outline().isAnonymousType();
389-
}
390-
391386
@Override
392387
public boolean isPublic() {
393388
return isPublicFilter.contains(name) || super.isPublic();

dd-java-agent/agent-tooling/src/main/java/datadog/trace/agent/tooling/bytebuddy/outline/TypeOutline.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,6 @@ public MethodList<MethodDescription.InDefinedShape> getDeclaredMethods() {
113113
return declaredMethods.isEmpty() ? NO_METHODS : new MethodList.Explicit<>(declaredMethods);
114114
}
115115

116-
@Override
117-
public boolean isAnonymousType() {
118-
// this method is only used when dd.iast.anonymous-classes.enabled=false
119-
// so take simple approach and look for '$number' at the end of the name
120-
for (int end = name.length() - 1, i = end; i > 0; i--) {
121-
char c = name.charAt(i);
122-
if (c == '$' && i < end) {
123-
return true; // only seen digits so far, assume anonymous
124-
} else if (c < '0' || c > '9') {
125-
break; // non-digit character found, assume not anonymous
126-
}
127-
}
128-
return false;
129-
}
130-
131116
void declare(AnnotationDescription annotation) {
132117
if (null != annotation) {
133118
if (null == declaredAnnotations) {

dd-java-agent/agent-tooling/src/test/groovy/datadog/trace/agent/tooling/bytebuddy/outline/OutlineTypeParserTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import spock.lang.Specification
55

66
class OutlineTypeParserTest extends Specification {
77

8-
void 'test modifiers are correct and anonymous classes are detected'() {
8+
void 'test modifiers are correct'() {
99
setup:
1010
final parser = new OutlineTypeParser()
1111
final locator = ClassFileLocators.classFileLocator(Thread.currentThread().contextClassLoader)
@@ -15,12 +15,13 @@ class OutlineTypeParserTest extends Specification {
1515
final outline = parser.parse(bytes)
1616

1717
then:
18-
outline.anonymousType == anonymous
1918
outline.interface == isinterface
2019
outline.abstract == isabstract
2120
outline.annotation == annotation
2221
outline.enum == isenum
2322

23+
// isAnonymousType is no longer supported in outlines for performance reasons
24+
2425
where:
2526
clazz | anonymous | isinterface | isabstract | annotation | isenum
2627
'datadog.trace.agent.test.EnclosedClasses' | false | false | false | false | false

dd-java-agent/instrumentation/iast-instrumenter/src/main/java/datadog/trace/instrumentation/iastinstrumenter/IastInstrumentation.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,30 @@ protected boolean doMatch(TypeDescription target) {
7676
}
7777
};
7878

79+
// this deliberately only considers anonymous types following the Java naming convention
80+
private static final ElementMatcher.Junction<TypeDescription> ANONYMOUS_TYPE_MATCHER =
81+
new ElementMatcher.Junction.ForNonNullValues<TypeDescription>() {
82+
@Override
83+
protected boolean doMatch(TypeDescription target) {
84+
String name = target.getName();
85+
// search the name in reverse until we find a $ or non-digit
86+
for (int end = name.length() - 1, i = end; i > 0; i--) {
87+
char c = name.charAt(i);
88+
if (c == '$' && i < end) {
89+
return true; // only seen digits so far, assume anonymous
90+
} else if (c < '0' || c > '9') {
91+
break; // non-digit character found, assume not anonymous
92+
}
93+
}
94+
return false;
95+
}
96+
};
97+
7998
static {
8099
if (Config.get().isIastAnonymousClassesEnabled()) {
81100
INSTANCE = TRIE_MATCHER;
82101
} else {
83-
INSTANCE = TRIE_MATCHER.and(not(TypeDescription::isAnonymousType));
102+
INSTANCE = TRIE_MATCHER.and(not(ANONYMOUS_TYPE_MATCHER));
84103
}
85104
}
86105
}

0 commit comments

Comments
 (0)