Skip to content

Commit ef060d0

Browse files
committed
De-duplicate a shortlist of very common strings found in class-files
1 parent c3fbbd9 commit ef060d0

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

class-match/src/main/java/datadog/instrument/classmatch/ClassFile.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,17 @@
2323
*/
2424
public final class ClassFile {
2525

26+
static final String JAVA_LANG_OBJECT = "java/lang/Object";
27+
static final String STATIC_INITIALIZER = "<clinit>";
28+
static final String CONSTRUCTOR = "<init>";
29+
static final String SIMPLE_CALL = "()V";
30+
2631
private static final String[] NO_INTERFACES = {};
2732
private static final FieldOutline[] NO_FIELDS = {};
2833
private static final MethodOutline[] NO_METHODS = {};
2934
private static final String[] NO_ANNOTATIONS = {};
35+
36+
private static final int ACC_INTERFACE = 0x0200;
3037
private static final int ACC_MODULE = 0x8000;
3138

3239
// attribute header for annotations that are visible at runtime
@@ -171,7 +178,17 @@ private static ClassHeader parse(byte[] bytecode, boolean onlyHeader) {
171178
String className = utf(bytecode, cp[cp[u2(bytecode, cursor)]]);
172179
cursor += 2;
173180

174-
String superName = access != ACC_MODULE ? utf(bytecode, cp[cp[u2(bytecode, cursor)]]) : null;
181+
String superName;
182+
if ((access & ACC_INTERFACE) != 0) {
183+
superName = JAVA_LANG_OBJECT;
184+
} else if (access != ACC_MODULE) {
185+
superName = utf(bytecode, cp[cp[u2(bytecode, cursor)]]);
186+
if (JAVA_LANG_OBJECT.equals(superName)) {
187+
superName = JAVA_LANG_OBJECT;
188+
}
189+
} else {
190+
superName = null;
191+
}
175192
cursor += 2;
176193

177194
// optional list of implemented/extended interfaces
@@ -231,8 +248,14 @@ private static ClassHeader parse(byte[] bytecode, boolean onlyHeader) {
231248
int methodAccess = u2(bytecode, cursor);
232249
cursor += 2;
233250
String methodName = utf(bytecode, cp[u2(bytecode, cursor)]);
251+
if (CONSTRUCTOR.equals(methodName)) {
252+
methodName = CONSTRUCTOR;
253+
}
234254
cursor += 2;
235255
String descriptor = utf(bytecode, cp[u2(bytecode, cursor)]);
256+
if (SIMPLE_CALL.equals(descriptor)) {
257+
descriptor = SIMPLE_CALL;
258+
}
236259
cursor += 2;
237260
String[] annotations = NO_ANNOTATIONS;
238261
Map<UtfKey, String> ofInterest = annotationsOfInterest;

class-match/src/main/java/datadog/instrument/classmatch/MethodMatcher.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package datadog.instrument.classmatch;
88

9+
import static datadog.instrument.classmatch.ClassFile.CONSTRUCTOR;
10+
import static datadog.instrument.classmatch.ClassFile.STATIC_INITIALIZER;
911
import static datadog.instrument.classmatch.InternalMatchers.ALL_METHODS;
1012
import static datadog.instrument.classmatch.InternalMatchers.declaresAnnotation;
1113
import static datadog.instrument.classmatch.InternalMatchers.declaresAnnotationOneOf;
@@ -58,7 +60,7 @@ static MethodMatcher method(Predicate<String> nameMatcher) {
5860
* @return matcher of constructor methods
5961
*/
6062
static MethodMatcher constructor() {
61-
return m -> "<init>".equals(m.methodName);
63+
return m -> CONSTRUCTOR.equals(m.methodName);
6264
}
6365

6466
/**
@@ -67,7 +69,7 @@ static MethodMatcher constructor() {
6769
* @return matcher of static-initializer methods
6870
*/
6971
static MethodMatcher staticInitializer() {
70-
return m -> "<clinit>".equals(m.methodName);
72+
return m -> STATIC_INITIALIZER.equals(m.methodName);
7173
}
7274

7375
/**

0 commit comments

Comments
 (0)