Skip to content

Commit c3fbbd9

Browse files
committed
Skeleton fluent-API for building type hierarchy predicates
1 parent d038c76 commit c3fbbd9

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ default FieldMatcher type(Class<?> type) {
7474
* @param typeMatcher the field type matcher
7575
* @return matcher of fields with a matching type
7676
*/
77-
default FieldMatcher type(Predicate<String> typeMatcher) {
77+
default FieldMatcher type(TypeMatcher typeMatcher) {
7878
return and(f -> hasFieldType(f, typeMatcher));
7979
}
8080

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static boolean hasParamDescriptor(MethodOutline method, int paramIndex, String p
5555
}
5656

5757
/** Does the method's descriptor contain a matching parameter type at the given index? */
58-
static boolean hasParamType(MethodOutline method, int paramIndex, Predicate<String> typeMatcher) {
58+
static boolean hasParamType(MethodOutline method, int paramIndex, TypeMatcher typeMatcher) {
5959
// boundaries covers start of second parameter, to start of return descriptor
6060
int[] boundaries = method.descriptorBoundaries();
6161
if (paramIndex >= boundaries.length) { // ignore return descriptor boundary
@@ -70,7 +70,7 @@ static boolean hasParamType(MethodOutline method, int paramIndex, Predicate<Stri
7070
}
7171

7272
/** Does the method's descriptor contain a matching return type? */
73-
static boolean hasReturnType(MethodOutline method, Predicate<String> typeMatcher) {
73+
static boolean hasReturnType(MethodOutline method, TypeMatcher typeMatcher) {
7474
// boundaries covers start of second parameter, to start of return descriptor
7575
int[] boundaries = method.descriptorBoundaries();
7676
// return type starts at 2 if no parameters, otherwise check last boundary
@@ -82,7 +82,7 @@ static boolean hasReturnType(MethodOutline method, Predicate<String> typeMatcher
8282
}
8383

8484
/** Does the field's descriptor contain a matching type? */
85-
static boolean hasFieldType(FieldOutline field, Predicate<String> typeMatcher) {
85+
static boolean hasFieldType(FieldOutline field, TypeMatcher typeMatcher) {
8686
String descriptor = field.descriptor;
8787
// extract type from "L...;" descriptor string, ignore primitive/array types
8888
return descriptor.charAt(0) == 'L'
@@ -247,6 +247,40 @@ public boolean test(MethodOutline outline) {
247247
}
248248
}
249249

250+
/** Logical AND of two {@link TypeMatcher}s; nested conjunctions will be collapsed. */
251+
static final class TypeConjunction extends MatcherUnion<TypeMatcher> implements TypeMatcher {
252+
TypeConjunction(TypeMatcher lhs, TypeMatcher rhs) {
253+
super(new TypeMatcher[] {lhs, rhs});
254+
}
255+
256+
@Override
257+
public boolean test(CharSequence typeString) {
258+
for (TypeMatcher matcher : matchers) {
259+
if (!matcher.test(typeString)) {
260+
return false;
261+
}
262+
}
263+
return true;
264+
}
265+
}
266+
267+
/** Logical OR of two {@link TypeMatcher}s; nested disjunctions will be collapsed. */
268+
static final class TypeDisjunction extends MatcherUnion<TypeMatcher> implements TypeMatcher {
269+
TypeDisjunction(TypeMatcher lhs, TypeMatcher rhs) {
270+
super(new TypeMatcher[] {lhs, rhs});
271+
}
272+
273+
@Override
274+
public boolean test(CharSequence typeString) {
275+
for (TypeMatcher matcher : matchers) {
276+
if (matcher.test(typeString)) {
277+
return true;
278+
}
279+
}
280+
return false;
281+
}
282+
}
283+
250284
/** Logical union of two matchers; nested unions of the same type will be collapsed. */
251285
abstract static class MatcherUnion<M> {
252286
protected final M[] matchers;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ default MethodMatcher parameter(int paramIndex, Class<?> paramType) {
172172
* @param typeMatcher the parameter type matcher
173173
* @return matcher of methods with a matching parameter type
174174
*/
175-
default MethodMatcher parameter(int paramIndex, Predicate<String> typeMatcher) {
175+
default MethodMatcher parameter(int paramIndex, TypeMatcher typeMatcher) {
176176
return and(m -> hasParamType(m, paramIndex, typeMatcher));
177177
}
178178

@@ -204,7 +204,7 @@ default MethodMatcher returning(Class<?> returnType) {
204204
* @param typeMatcher the return type matcher
205205
* @return matcher of methods with a matching return type
206206
*/
207-
default MethodMatcher returning(Predicate<String> typeMatcher) {
207+
default MethodMatcher returning(TypeMatcher typeMatcher) {
208208
return and(m -> hasReturnType(m, typeMatcher));
209209
}
210210

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2025-Present Datadog, Inc.
5+
*/
6+
7+
package datadog.instrument.classmatch;
8+
9+
import java.util.function.Predicate;
10+
11+
/** Fluent-API for building type hierarchy predicates. */
12+
public interface TypeMatcher extends Predicate<CharSequence> {
13+
14+
/**
15+
* Conjunction of this matcher AND another.
16+
*
17+
* @param other the other matcher
18+
* @return conjunction of both matchers
19+
*/
20+
default TypeMatcher and(TypeMatcher other) {
21+
return new InternalMatchers.TypeConjunction(this, other);
22+
}
23+
24+
/**
25+
* Disjunction of this matcher OR another.
26+
*
27+
* @param other the other matcher
28+
* @return disjunction of both matchers
29+
*/
30+
default TypeMatcher or(TypeMatcher other) {
31+
return new InternalMatchers.TypeDisjunction(this, other);
32+
}
33+
}

0 commit comments

Comments
 (0)