import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

/**
 * This is {@link CheckSpringConfigurationBeanProxying}.
 *
 * @author Misagh Moayyed
 * @since 6.3.0
 */
public class CheckSpringConfigurationBeanProxying {
    public static void main(final String[] args) throws Exception {
        checkPattern(args[0]);
    }

    private static void print(final String message, final Object... args) {
        //CHECKSTYLE:OFF
        System.out.printf(message, args);
        //CHECKSTYLE:ON
    }

    private static String readFile(final Path file) {
        try {
            return Files.readString(file);
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
    }

    protected static void checkPattern(final String arg) throws IOException {
        var failBuild = new AtomicBoolean(false);
        final var results = new ArrayList<>();

        var patternBeanMethods = Pattern.compile("public\\s\\w+(<\\w+>)*\\s(\\w+)\\(");

        Files.walk(Paths.get(arg))
            .filter(file -> Files.isRegularFile(file) && file.toFile().getName().endsWith("Configuration.java"))
            .forEach(file -> {
                var text = readFile(file);
                if (text.contains("@Configuration")) {
                    var canProxyBeans = true;
                    var matcher = patternBeanMethods.matcher(text);
                    results.clear();
                    while (matcher.find()) {
                        results.add(matcher.group(2));
                    }
                    for (var r : results) {
                        var patternMethodCall = Pattern.compile(r + "\\(");
                        var matcher2 = patternMethodCall.matcher(text);
                        var count = 0;
                        while (matcher2.find()) {
                            count++;
                        }
                        if (count > 1) {
                            canProxyBeans = false;
                            break;
                        }
                    }
                    if (canProxyBeans) {
                        var proxyPattern = Pattern.compile("@Configuration\\(value\\s*=\\s*\"(\\w+)\",\\s*proxyBeanMethods\\s*=\\s*(false|true)\\)").matcher(text);
                        if (!proxyPattern.find()) {
                            print("Configuration class %s should be marked with proxyBeanMethods = false", file);
                            failBuild.set(true);
                        }
                    }
                }

            });
        if (failBuild.get()) {
            System.exit(1);
        }
    }
}

@@ -1259,16 +1259,44 @@
 dsWith(%22
+SurrogateAuthenticationAudit
 Configur
@@ -1466,527 +1466,174 @@
 var 
-canProxyBeans = true;%0A                    var match
+proxyPatt
 er
+n
  = 
-p
+P
 attern
-BeanMethods.matcher(text);%0A                    results.clear();%0A                    while (matcher.find()) %7B%0A                        results.add(matcher.group(2));%0A                    %7D%0A                    for (var r : results) %7B%0A                        var patternMethodCall = Pattern.compile(r + %22%5C%5C(%22);%0A                        var matcher2 = patternMethodCall.matcher(text);%0A                        var count = 0;%0A                        while (matcher2
+.compile(%22@Configuration%5C%5C(value%5C%5Cs*=%5C%5Cs*%5C%22(%5C%5Cw+)%5C%22,%5C%5Cs*proxyBeanMethods%5C%5Cs*=%5C%5Cs*(true)%5C%5C)%22).matcher(text);%0A                    if (proxyPattern
 .fin
@@ -1667,128 +1667,69 @@
     
-    count++;%0A                        %7D%0A                        if (count %3E 1) %7B%0A                            canP
+print(%22Configuration class %25s should be marked with p
 roxyBean
 s = 
@@ -1724,16 +1724,22 @@
 roxyBean
+Method
 s = fals
@@ -1739,16 +1739,26 @@
  = false
+%25n%22, file)
 ;%0A      
@@ -1779,17 +1779,27 @@
     
-    break
+failBuild.set(true)
 ;%0A  
@@ -1808,36 +1808,32 @@
                 
-    
 %7D%0A              
@@ -1842,79 +1842,8 @@
     
-%7D%0A                    if (canProxyBeans) %7B%0A                        var 
 prox
@@ -1915,50 +1915,8 @@
 +)%5C%22
-,%5C%5Cs*proxyBeanMethods%5C%5Cs*=%5C%5Cs*(false%7Ctrue)
 %5C%5C)%22
@@ -1956,17 +1956,12 @@
     
-    
 if (
-!
 prox
@@ -1971,36 +1971,32 @@
 ttern.find()) %7B%0A
-    
                 
@@ -2035,32 +2035,43 @@
 ss %25s should be 
+explicitly 
 marked with prox
@@ -2090,16 +2090,18 @@
  = false
+%25n
 %22, file)
@@ -2094,36 +2094,32 @@
 alse%25n%22, file);%0A
-    
                 
@@ -2151,34 +2151,8 @@
 e);%0A
-                        %7D%0A
     

check for bean proxying to be off