Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package net.bytebuddy.agent.builder;

import static java.util.Collections.emptyIterator;
import static java.util.Collections.singleton;
import static java.util.logging.Level.FINE;

import io.opentelemetry.javaagent.extension.matcher.internal.DelegatingMatcher;
Expand Down Expand Up @@ -48,6 +49,8 @@ public class AgentBuilderUtil {
private static final Field erasureMatcherField = getField(ErasureMatcher.class, "matcher");
private static final Field conjunctionMatchersField =
getField(ElementMatcher.Junction.Conjunction.class, "matchers");
private static final Field disjunctionMatchersField =
getField(ElementMatcher.Junction.Disjunction.class, "matchers");
private static final Field stringMatcherValueField = getField(StringMatcher.class, "value");
private static final Field stringMatcherModeField = getField(StringMatcher.class, "mode");
private static final Field stringSetMatcherValuesField =
Expand Down Expand Up @@ -173,11 +176,37 @@ private static Result inspect(ElementMatcher<?> matcher) throws Exception {
List<ElementMatcher<?>> matchers =
getDelegateMatchers((ElementMatcher.Junction.Conjunction<?>) matcher);
for (ElementMatcher<?> elementMatcher : matchers) {
// For conjunction to match all elements need to match, we can return result for any element
// here since we are using it as a negative match - if it does not match the whole matcher
// can't match.
Result result = inspect(elementMatcher);
if (result != null) {
return result;
}
}
} else if (matcher instanceof ElementMatcher.Junction.Disjunction) {
List<ElementMatcher<?>> matchers =
getDelegateMatchers((ElementMatcher.Junction.Disjunction<?>) matcher);
boolean subtype = false;
Set<String> names = new HashSet<>();
boolean failed = false;
for (ElementMatcher<?> elementMatcher : matchers) {
// For disjunction to match at least one element needs to match, we need to inspect all
// elements and combine results to be able to tell whether the whole matcher could match.
Result result = inspect(elementMatcher);
if (result == null) {
failed = true;
break;
}
// Subtype matcher covers named matcher, if we have at least one subtype matcher we can
// treat all named matchers as subtype matchers, if we have only named matchers we can treat
// them as named matchers.
subtype |= result.subtype;
names.addAll(result.names);
}
if (!failed) {
return subtype ? Result.subtype(names) : Result.named(names);
}
}

return null;
Expand Down Expand Up @@ -207,37 +236,38 @@ private Result(boolean subtype) {
this.subtype = subtype;
}

private Result() {
this(false);
}

@Nullable
static Result subtype(@Nullable Result value) {
if (value == null) {
return null;
}
return subtype(value.names);
}

Result result = new Result(true);
result.names.addAll(value.names);
return result;
@Nullable
static Result subtype(@Nullable Set<String> value) {
return result(true, value);
}

@Nullable
static Result named(@Nullable String value) {
if (value == null) {
return null;
}
Result result = new Result();
result.names.add(value);
return result;
return named(singleton(value));
}

@Nullable
static Result named(@Nullable Set<String> value) {
return result(false, value);
}

@Nullable
private static Result result(boolean subtype, @Nullable Set<String> value) {
if (value == null || value.isEmpty()) {
return null;
}
Result result = new Result();
Result result = new Result(subtype);
result.names.addAll(value);
return result;
}
Expand Down Expand Up @@ -283,6 +313,12 @@ private static List<ElementMatcher<?>> getDelegateMatchers(
return (List<ElementMatcher<?>>) conjunctionMatchersField.get(matcher);
}

@SuppressWarnings("unchecked") // casting reflection result
private static List<ElementMatcher<?>> getDelegateMatchers(
ElementMatcher.Junction.Disjunction<?> matcher) throws Exception {
return (List<ElementMatcher<?>>) disjunctionMatchersField.get(matcher);
}

/**
* @return the value given string matcher matches when matcher mode is
* StringMatcher.Mode.EQUALS_FULLY, null otherwise
Expand Down
Loading