/** * Returns {@code true}, if any of the matchers match the provided string. * * @param matchers the matchers which should be used to match the provided string * @param s the string to match against * @return {@code true}, if any of the matchers match the provided string */ @Nullable public static WildcardMatcher anyMatch(List<WildcardMatcher> matchers, String s) { return anyMatch(matchers, s, null); }
public static MethodMatcher of(String methodMatcher) { final Matcher matcher = METHOD_MATCHER_PATTERN.matcher(methodMatcher); if (!matcher.matches()) { throw new IllegalArgumentException("'" + methodMatcher + "'" + " is not a valid method matcher"); } final String modifier = matcher.group(2); final WildcardMatcher clazz = caseSensitiveMatcher(matcher.group(3)); final WildcardMatcher method = matcher.group(5) != null ? caseSensitiveMatcher(matcher.group(5)) : WildcardMatcher.matchAll(); final List<WildcardMatcher> args = getArgumentMatchers(matcher.group(6)); return new MethodMatcher(methodMatcher, getModifier(modifier), clazz, method, args); }
/** * Returns {@code true}, if any of the matchers match the provided partitioned string. * * @param matchers the matchers which should be used to match the provided string * @param firstPart The first part of the string to match against. * @param secondPart The second part of the string to match against. * @return {@code true}, if any of the matchers match the provided partitioned string * @see #matches(String, String) */ @Nullable public static WildcardMatcher anyMatch(List<WildcardMatcher> matchers, String firstPart, @Nullable String secondPart) { for (int i = 0; i < matchers.size(); i++) { if (matchers.get(i).matches(firstPart, secondPart)) { return matchers.get(i); } } return null; }
@Test void testNeedleLongerThanHaystack() { assertSoftly(softly -> { softly.assertThat(WildcardMatcher.valueOf("*foo").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("*foob").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("*fooba").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("*foobar").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("foo*").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("foob*").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("fooba*").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("foobar*").matches("baz")).isFalse(); softly.assertThat(WildcardMatcher.valueOf("*foobar*").matches("baz")).isFalse(); }); }
@Test void testMethodMatcherThreeArgs() { final MethodMatcher methodMatcher = MethodMatcher.of("* co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest#testMethodMatcher(java.lang.String, foo,bar)"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("testMethodMatcher"); assertThat(methodMatcher.getArgumentMatchers()).hasSize(3); assertThat(methodMatcher.getArgumentMatchers()).containsExactly(caseSensitiveMatcher("java.lang.String"), caseSensitiveMatcher("foo"), caseSensitiveMatcher("bar")); } }
@Test void testMatchAnyStartsWith() { final WildcardMatcher matcher1 = WildcardMatcher.valueOf("foo*"); final WildcardMatcher matcher2 = WildcardMatcher.valueOf("bar*"); assertSoftly(softly -> { softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "foo")).isEqualTo(matcher1); softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "bar")).isEqualTo(matcher2); softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "baz")).isNull(); softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "fo", "o")).isEqualTo(matcher1); softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "ba", "r")).isEqualTo(matcher2); softly.assertThat(WildcardMatcher.anyMatch(Arrays.asList(matcher1, matcher2), "ba", "z")).isNull(); }); }
@Override public WildcardMatcher convert(String s) { return WildcardMatcher.valueOf(s); }
@Nullable private static List<WildcardMatcher> getArgumentMatchers(@Nullable String arguments) { if (arguments == null) { return null; } // remove parenthesis arguments = arguments.substring(1, arguments.length() - 1); final String[] splitArguments = StringUtils.split(arguments, ','); List<WildcardMatcher> matchers = new ArrayList<>(splitArguments.length); for (String argument : splitArguments) { matchers.add(caseSensitiveMatcher(argument.trim())); } return matchers; }
@Test void testMethodMatcherNoArguments() { final MethodMatcher methodMatcher = MethodMatcher.of("public co.elastic.apm.agent.bci.methodmatching.Method*Test#testMethodMatcher()"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.Method*Test"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("testMethodMatcher"); assertThat(methodMatcher.getArgumentMatchers()).isEmpty(); }
int ii = i; while (ii < totalHaystackLength && j < needleLength) { char c = ignoreCase ? Character.toLowerCase(charAt(ii, haystack1, haystack2, haystack1Length)) : charAt(ii, haystack1, haystack2, haystack1Length); char c2 = ignoreCase ? Character.toLowerCase(needle.charAt(j)) : needle.charAt(j); if (c != c2) {
@Test void testIndexOfIgnoreCase() { assertSoftly(softly -> { softly.assertThat(indexOfIgnoreCase( "foo", "foo", "foo", false, 0, 6)).isEqualTo(0); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "foo", false, 0, 6)).isEqualTo(0); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "oob", false, 0, 6)).isEqualTo(1); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "oba", false, 0, 6)).isEqualTo(2); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "bar", false, 0, 6)).isEqualTo(3); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "o", false, 0, 6)).isEqualTo(1); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "ob", false, 0, 6)).isEqualTo(2); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "ooba", false, 0, 6)).isEqualTo(1); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "oobar", false, 0, 6)).isEqualTo(1); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "fooba", false, 0, 6)).isEqualTo(0); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "foobar", false, 0, 6)).isEqualTo(0); softly.assertThat(indexOfIgnoreCase( "afoo", "bar", "oba", false, 0, 7)).isEqualTo(3); softly.assertThat(indexOfIgnoreCase( "afoo", "bara", "oba", false, 0, 8)).isEqualTo(3); softly.assertThat(indexOfIgnoreCase( "aafoo", "baraa", "oba", false, 0, 10)).isEqualTo(4); softly.assertThat(indexOfIgnoreCase( "foo", "bar", "ara", false, 0, 6)).isEqualTo(-1); }); }
@Test void testMethodMatcherTwoArgs() { final MethodMatcher methodMatcher = MethodMatcher.of("protected co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest#testMethodMatcher(*String, foo)"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("testMethodMatcher"); assertThat(methodMatcher.getArgumentMatchers()).hasSize(2); assertThat(methodMatcher.getArgumentMatchers()).containsExactly(caseSensitiveMatcher("*String"), caseSensitiveMatcher("foo")); }
public static WildcardMatcher caseSensitiveMatcher(String matcher) { return valueOf(CASE_SENSITIVE_PREFIX + matcher); }
@Test void testMethodMatcherWithoutMethod() { final MethodMatcher methodMatcher = MethodMatcher.of("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("*"); assertThat(methodMatcher.getArgumentMatchers()).isNull(); }
@Test void testMatchesPartitionedStringEndsWith() { final WildcardMatcher matcher = WildcardMatcher.valueOf("*/bar/baz"); assertSoftly(softly -> { softly.assertThat(matcher.matches("/foo/bar/baz", "")).isTrue(); softly.assertThat(matcher.matches("", "/foo/bar/baz")).isTrue(); softly.assertThat(matcher.matches("/foo/bar", "/baz")).isTrue(); softly.assertThat(matcher.matches("/foo", "/bar/baz")).isTrue(); softly.assertThat(matcher.matches("/foo", "/bar/baz")).isTrue(); softly.assertThat(matcher.matches("/bar", "/foo/baz")).isFalse(); softly.assertThat(matcher.matches("/foo", "/foo/baz")).isFalse(); }); }
@Test void testMethodMatcherOneArg() { final MethodMatcher methodMatcher = MethodMatcher.of("private co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest#test*Matcher(java.lang.String)"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("test*Matcher"); assertThat(methodMatcher.getArgumentMatchers()).hasSize(1); assertThat(methodMatcher.getArgumentMatchers()).contains(caseSensitiveMatcher("java.lang.String")); }
private boolean isDisabled(String name) { return WildcardMatcher.anyMatch(config.getDisableMetrics(), name) != null; }
@Test void testDisabledMetrics() { when(config.getDisableMetrics()).thenReturn(List.of(WildcardMatcher.valueOf("jvm.gc.*"))); final DoubleSupplier problematicMetric = () -> { throw new RuntimeException("Huston, we have a problem"); }; metricRegistry.addUnlessNegative("jvm.gc.count", emptyMap(), problematicMetric); metricRegistry.addUnlessNan("jvm.gc.count", emptyMap(), problematicMetric); metricRegistry.add("jvm.gc.count", emptyMap(), problematicMetric); assertThat(metricRegistry.getMetricSets()).isEmpty(); } }
@Test void testMethodMatcherWithoutArguments() { final MethodMatcher methodMatcher = MethodMatcher.of("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest#testMethodMatcher"); assertThat(methodMatcher).isNotNull(); assertThat(methodMatcher.getClassMatcher().getMatcher()).isEqualTo("co.elastic.apm.agent.bci.methodmatching.MethodMatcherTest"); assertThat(methodMatcher.getMethodMatcher().getMatcher()).isEqualTo("testMethodMatcher"); assertThat(methodMatcher.getArgumentMatchers()).isNull(); }