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

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

package live.document.mavenplugin.rootmethod;

import live.document.generator.utils.FileUtils;
import live.document.mavenplugin.common.AnalyzeAssistant;
import live.document.scanner.CallGraph;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 查找出所有的root methods
 */
@Mojo(name = "find-all-root-methods")
public class FindAllRootMethodsMojo extends AbstractMojo {
    @Parameter(defaultValue = "${project.basedir}", required = true)
    private File projectRoot;
    /**
     * 排除某些文件夹不扫描class文件
     */
    @Parameter
    private String[] excludeFolders;
    @Parameter
    private String[] rootMethodPatterns;
    @Parameter
    private String[] rootMethodAnnotations;
    @Parameter
    private String[] rootMethodExcludePatterns;
    @Parameter(defaultValue = "", required = true)
    private String analysisResultOutputDir;

    @Override
    public void execute() throws MojoExecutionException {
        try {
            FileUtils fileUtils = new FileUtils();
            getLog().info("scan: " + projectRoot.getAbsolutePath());

            CallGraph callGraph = AnalyzeAssistant.analyzeCode(projectRoot.getAbsolutePath(), excludeFolders, null, analysisResultOutputDir);
            RootMethodChecker rootMethodChecker = new RootMethodChecker();
            rootMethodChecker.setRootMethodPatterns(rootMethodPatterns);
            rootMethodChecker.setRootMethodAnnotations(rootMethodAnnotations);
            rootMethodChecker.setRootMethodExcludePatterns(rootMethodExcludePatterns);

            List results = new ArrayList<>();
            results.add("Module,Class,RootMethodName");
            List rootMethods = callGraph.getClasses()
                    .stream()
                    .flatMap(c -> c.getMethods().stream())
                    .filter(rootMethodChecker::isRootMethod)
                    .map(m -> m.getFullName())
                    .sorted()
                    .collect(Collectors.toList());

            DuplicatedRootMethodRemover remover = new DuplicatedRootMethodRemover(callGraph);
            rootMethods = remover.removeDuplicatedMethods(rootMethods);
            getLog().info("found " + rootMethods.size() + " root methods.");
            rootMethods.forEach(m -> results.add(String.format("%s,%s,%s", getModuleName(callGraph, m), getClassName(m), getMethodName(m))));

            Path rootMethodsFileName =  Paths.get(analysisResultOutputDir,  "all-root-methods.csv");
            fileUtils.writeFile(rootMethodsFileName, results.stream().collect(Collectors.joining(System.lineSeparator())));
            getLog().info("File created (all root methods): file://" + rootMethodsFileName);
        } catch (Exception e) {
            getLog().error(e);
            throw new MojoExecutionException(e.getMessage());
        }
    }

    private String getMethodName(String methodName) {
        int i = methodName.lastIndexOf(".");
        if (i > 0) {
            return methodName.substring(i + 1);
        }

        return methodName;
    }

    private String getClassName(String methodName) {
        int i = methodName.lastIndexOf(".");
        if (i > 0) {
            return methodName.substring(0, i);
        }

        return methodName;
    }

    private String getModuleName(CallGraph callGraph, String methodName) {
        String className = getClassName(methodName);
        return callGraph.getClass(className).getModule();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy