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

live.document.mavenplugin.common.CallNodeVisibleFunc Maven / Gradle / Ivy

package live.document.mavenplugin.common;

import live.document.generator.model.CallNode;
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;

public class CallNodeVisibleFunc {
    private Set entitySet = new HashSet<>();
    private Set tableSet = new HashSet<>();
    private Set ignorePatternSet = new HashSet<>();
    private Set parentNodeIgnorePatternSet = new HashSet<>();
    private Map patterns = new HashMap<>();

    public CallNodeVisibleFunc(String[] entities, String[] tables, String[] ignorePatterns, String[] parentNodeIgnorePatterns) {
        if (entities != null) {
            entitySet.addAll(Arrays.asList(entities));
        }

        if (tables != null) {
            tableSet.addAll(Arrays.asList(tables));
        }

        if (ignorePatterns != null) {
            ignorePatternSet.addAll(Arrays.asList(ignorePatterns));
        }

        if (parentNodeIgnorePatterns != null) {
            parentNodeIgnorePatternSet.addAll(Arrays.asList(parentNodeIgnorePatterns));
        }
    }

    /**
     * 当未指定entities时,为了避免mindmap过大,以下节点不会显示:
     * 1. Entity的读方法:getter, isXX...
     * 2. 所有子/孙子节点与自己属于同一个类
     *
     * @param node
     * @return
     */
    public boolean isVisible(CallNode node) {
        return doCheckVisible(node, new HashSet<>());
    }

    private boolean doCheckVisible(CallNode node, Set scannedNodes) {
        if (scannedNodes.contains(node)) {
            return false;
        }
        scannedNodes.add(node);

        if (node.isLink()) {
            if (node.getLinkedNode() != null) {
                return doCheckVisible(node.getLinkedNode(), scannedNodes);
            }
            return false;
        }

        if (selfHasEntityOperation(node)) {
            return true;
        }

        if (matchIgnorePattern(node)) {
            return false;
        }

        if (matchParentNodeIgnorePattern(node)) {
            return false;
        }

        if (node.getMethodObject() != null && node.getMethodObject().getClassName().contains("$") && node.getChildren().isEmpty()) {
            //对 XXX$Result.getOAccidentTime 这样的方法不显示
            // return new EndCaseParameter(caseId, rs.getOAccidentTime(), rs.getOReportDate(), rs.getOAccidentStatus(),
            //                rs.getODeathDate(), rs.getOFinishTime(), rs.getFeeCount());
            return false;
        }

        for (CallNode child : node.getChildren()) {
            if (doCheckVisible(child, scannedNodes)) {
                return true;
            }
        }

        return false;
    }

    private boolean isWriteOperation(MethodObject methodObject) {
        for (String methodName : Constants.READONLY_METHOD_NAME) {
            if (methodObject.getMethodName().startsWith(methodName)) {
                return false;
            }
        }

        return true;
    }

    private boolean selfHasEntityOperation(CallNode node) {
        if (node.getMethodObject() != null) {
            MethodObject methodObject = node.getMethodObject();
            boolean isEntity = isSpecifiedEntityClass(methodObject);
            if (isEntity) {
                return isWriteOperation(methodObject);
            }
        } else if (node.getDbOperation() != null) {
            if (isSpecifiedDbObject(node.getDbOperation().getName())) {
                return true;
            }
        }

        return false;
    }

    private boolean isSpecifiedDbObject(String name) {
        return tableSet.isEmpty() || tableSet.contains(name);
    }

    private boolean isSpecifiedEntityClass(MethodObject methodObject) {
        if (entitySet.isEmpty()) {
            if (tableSet.isEmpty()) {
                return methodObject.getClassName().endsWith("Entity");
            } else {
                return false;
            }
        }

        for (String entity : entitySet) {
            if (entity.indexOf(".") > 0) {
                if (methodObject.getClassName().equalsIgnoreCase(entity)) {
                    return true;
                }
            } else {
                if (methodObject.getClassName().endsWith("." + entity)) {
                    return true;
                }
            }
        }

        return false;
    }

    private boolean matchIgnorePattern(CallNode node) {
        if (node.getMethodObject() == null) {
            return false;
        }

        for (String ignorePattern : ignorePatternSet) {
            Pattern pattern = getPattern(ignorePattern);
            String fullName = node.getMethodObject().getClassName() + "." + node.getMethodObject().getMethodName();
            if (pattern.matcher(fullName).matches()) {
                return true;
            }
        }

        return false;
    }

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

        return patterns.get(regex);
    }

    private boolean matchParentNodeIgnorePattern(CallNode node) {
        if (node.getMethodObject() == null) {
            return false;
        }

        if (node.getParent() != null) {
            for (String ignorePattern : parentNodeIgnorePatternSet) {
                Pattern pattern = getPattern(ignorePattern);
                String fullName = node.getParent().getMethodObject().getClassName() + "." + node.getParent().getMethodObject().getMethodName();
                if (pattern.matcher(fullName).matches()) {
                    return true;
                }
            }
        }

        return false;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy