MethodMatcherFactory { public static MethodMatchers methodMatchers(String descriptor) { Matcher matcher = METHOD_PATTERN.matcher(descriptor); if (!matcher.find()) { throw new IllegalArgumentException("Illegal method specification: " + descriptor); } MethodMatchers.ParametersBuilder methodMatcher = MethodMatchers.create().ofTypes(matcher.group(1)).names(matcher.group(2)); return collectArguments(descriptor, matcher, 3, methodMatcher); } private  MethodMatcherFactory(); static MethodMatchers constructorMatcher(String descriptor); static MethodMatchers methodMatchers(String descriptor); static MethodMatchers collectArguments(String descriptor, Matcher initialMatcher, int groupOffset, MethodMatchers.ParametersBuilder methodMatcher);  }
@Test void inner_classes() throws Exception { MethodMatchers anyArg = MethodMatcherFactory.methodMatchers("org.sonar.test.Outer$Inner#foo"); MethodVisitor visitor = new MethodVisitor(); visitor.add(anyArg); scanWithVisitor(visitor, TestUtils.inputFile("src/test/files/matcher/InnerClass.java")); assertThat(visitor.count(anyArg)).isEqualTo(1); }
@Test void methodFactoryMatching() { MethodMatchers anyArg = MethodMatcherFactory.methodMatchers("org.sonar.test.Test#match"); MethodMatchers stringOnly = MethodMatcherFactory.methodMatchers("org.sonar.test.Test#match(java.lang.String)"); MethodMatchers stringInt = MethodMatcherFactory.methodMatchers("org.sonar.test.Test#match(java.lang.String,int)"); MethodMatchers intInt = MethodMatcherFactory.methodMatchers("org.sonar.test.Test#match(int,int)"); MethodMatchers onlyBoolean = MethodMatcherFactory.methodMatchers("org.sonar.test.Test#match(java.lang.Boolean)"); MethodVisitor visitor = new MethodVisitor(); visitor.add(anyArg); visitor.add(stringOnly); visitor.add(stringInt); visitor.add(intInt); visitor.add(onlyBoolean); InputFile testFile = buildTestFile( "package org.sonar.test;", "private class Test {", " private void match(String a) {}", " private void match(String a, int b) {}", " private void match(int a, int b) {}", " private void caller() {", " match(new String());", " match(new String(), 0);", " match(new String(), 1);", " match(0, 1);", " match(1, 2);", " match(3, 5);", " }", "}"); scanWithVisitor(visitor, testFile); assertThat(visitor.count(anyArg)).isEqualTo(6); assertThat(visitor.count(stringOnly)).isEqualTo(1); assertThat(visitor.count(stringInt)).isEqualTo(2); assertThat(visitor.count(intInt)).isEqualTo(3); assertThat(visitor.count(onlyBoolean)).isZero(); }