MethodMatcherFactory { public static MethodMatchers constructorMatcher(String descriptor) { Matcher matcher = CLASS_PATTERN.matcher(descriptor); if (!matcher.find()) { throw new IllegalArgumentException("Illegal constructor specification: " + descriptor); } MethodMatchers.ParametersBuilder constructorMatcher = MethodMatchers.create().ofTypes(matcher.group(1)).constructor(); return collectArguments(descriptor, matcher, 2, constructorMatcher); } 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 constructorFactoryMatching() { MethodMatchers anyArg = MethodMatcherFactory.constructorMatcher("java.lang.String"); MethodMatchers noArg = MethodMatcherFactory.constructorMatcher("java.lang.String()"); MethodMatchers stringBuilder = MethodMatcherFactory.constructorMatcher("java.lang.String(java.lang.StringBuilder)"); MethodMatchers stringBytes = MethodMatcherFactory.constructorMatcher("java.lang.String(byte[],int,int)"); MethodVisitor visitor = new MethodVisitor(); visitor.add(anyArg); visitor.add(noArg); visitor.add(stringBuilder); visitor.add(stringBytes); InputFile testFile = buildTestFile( "package org.sonar.test;", "private class Test {", " private void caller() {", " byte[] bytes = \"Hello world!\".getBytes();", " new String();", " new String(new StringBuilder());", " new String(bytes, 6, 5);", " new String(bytes, 0, 5);", " }", "}"); scanWithVisitor(visitor, testFile); assertThat(visitor.count(anyArg)).isEqualTo(4); assertThat(visitor.count(noArg)).isEqualTo(1); assertThat(visitor.count(stringBuilder)).isEqualTo(1); assertThat(visitor.count(stringBytes)).isEqualTo(2); }