All Downloads are FREE. Search and download functionalities are using the official Maven repository.

live.document.mavenplugin.rootmethod.RootMethodChecker Maven / Gradle / Ivy

package live.document.mavenplugin.rootmethod;

import live.document.scanner.MethodObject;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class RootMethodChecker {
    private Map patterns = new HashMap<>();
    private Set rootMethodPatterns = new HashSet<>();
    private Set rootMethodAnnotations = new HashSet<>();
    private Set rootMethodExcludePatterns = new HashSet<>();
    private Set rootMethodScopePatterns = new HashSet<>();

    public boolean isRootMethod(MethodObject methodObject) {
        for (String excludePattern : rootMethodExcludePatterns) {
            Pattern pattern = getPattern(excludePattern);
            if (pattern.matcher(methodObject.getFullName()).matches()) {
                return false;
            }
        }

        for (String rootMethodScopePattern : rootMethodScopePatterns) {
            Pattern pattern = getPattern(rootMethodScopePattern);
            if (! pattern.matcher(methodObject.getFullName()).matches()) {
                return false;
            }
        }

        for (String rootMethodPattern : rootMethodPatterns) {
            Pattern pattern = getPattern(rootMethodPattern);
            if (pattern.matcher(methodObject.getFullName()).matches()) {
                return true;
            }
        }

        Set annotations = methodObject.getAnnotations()
                .stream().map(a -> a.getFullClassName()).collect(Collectors.toSet());
        return annotations.removeAll(rootMethodAnnotations);
    }

    protected Pattern getPattern(String regex) {
        if (!patterns.containsKey(regex)) {
            Pattern pattern = Pattern.compile(regex);
            patterns.put(regex, pattern);
        }

        return patterns.get(regex);
    }

    public void setRootMethodPatterns(String[] rootMethodPatterns) {
        if (rootMethodPatterns != null) {
            this.rootMethodPatterns.addAll(Arrays.asList(rootMethodPatterns));
        }
    }

    public void setRootMethodAnnotations(String[] rootMethodAnnotations) {
        if (rootMethodAnnotations != null) {
            this.rootMethodAnnotations.addAll(Arrays.asList(rootMethodAnnotations));
        }
    }

    public void setRootMethodExcludePatterns(String[] rootMethodExcludePatterns) {
        if (rootMethodExcludePatterns != null) {
            this.rootMethodExcludePatterns.addAll(Arrays.asList(rootMethodExcludePatterns));
        }
    }

    public void setRootMethodScopePatterns(String[] rootMethodScopePatterns) {
        if (rootMethodScopePatterns != null) {
            this.rootMethodScopePatterns.addAll(Arrays.asList(rootMethodScopePatterns));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy