Search in sources :

Example 11 with MethodOrder

use of com.github.sevntu.checkstyle.ordering.MethodOrder in project methods-distance by sevntu-checkstyle.

the class ViolationReporterDependencyInformationConsumer method accept.

@Override
public void accept(String filePath, Dependencies dependencies) {
    final MethodOrder initialMethodOrder = new MethodOrder(dependencies);
    final MethodOrder optimizedMethodOrder = reorderer.reorder(initialMethodOrder);
    logFirstMethodOutOfOrder(module, optimizedMethodOrder);
}
Also used : MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder)

Example 12 with MethodOrder

use of com.github.sevntu.checkstyle.ordering.MethodOrder in project methods-distance by sevntu-checkstyle.

the class TopologicalMethodReorderer method methodDependenciesDistanceOptimization.

private MethodOrder methodDependenciesDistanceOptimization(MethodOrder startingOrder) {
    MethodOrder currentOrder = startingOrder;
    for (final Method caller : startingOrder.getMethods()) {
        final List<Method> dependencies = currentOrder.getMethodDependenciesInAppearanceOrder(caller);
        if (!dependencies.isEmpty()) {
            final int callerIndex = currentOrder.getMethodIndex(caller);
            boolean allDependenciesLocatedAfterCaller = true;
            for (final Method callee : dependencies) {
                allDependenciesLocatedAfterCaller = allDependenciesLocatedAfterCaller && currentOrder.getMethodIndex(callee) > callerIndex;
            }
            if (allDependenciesLocatedAfterCaller) {
                final List<Method> allMethods = new ArrayList<>(currentOrder.getMethods());
                final List<Method> subList = allMethods.subList(callerIndex, allMethods.size());
                subList.removeAll(dependencies);
                subList.addAll(1, dependencies);
                final MethodOrder optimizedMethodOrder = currentOrder.reorder(allMethods);
                currentOrder = getBestOrdering(currentOrder, optimizedMethodOrder);
            }
        }
    }
    return currentOrder;
}
Also used : ArrayList(java.util.ArrayList) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method)

Example 13 with MethodOrder

use of com.github.sevntu.checkstyle.ordering.MethodOrder in project methods-distance by sevntu-checkstyle.

the class TopologicalMethodReorderer method methodGroupsGrouping.

private MethodOrder methodGroupsGrouping(MethodOrder startingMethodOrder, final List<List<Method>> groups) {
    MethodOrder currentMethodOrder = startingMethodOrder;
    for (final List<Method> methodsGroup : groups) {
        final Method lastMethod = methodsGroup.stream().sorted(new MethodIndexComparator(currentMethodOrder).reversed()).findFirst().get();
        final List<Method> nonLastMethodsInGroup = new ArrayList<>(methodsGroup);
        nonLastMethodsInGroup.remove(lastMethod);
        final List<Method> optimizedOrdering = new ArrayList<>(currentMethodOrder.getMethods());
        optimizedOrdering.removeAll(nonLastMethodsInGroup);
        optimizedOrdering.addAll(currentMethodOrder.getMethodIndex(lastMethod), nonLastMethodsInGroup);
        currentMethodOrder = getBestOrdering(currentMethodOrder, currentMethodOrder.reorder(optimizedOrdering));
    }
    return currentMethodOrder;
}
Also used : ArrayList(java.util.ArrayList) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method)

Example 14 with MethodOrder

use of com.github.sevntu.checkstyle.ordering.MethodOrder in project methods-distance by sevntu-checkstyle.

the class TopologicalMethodReorderer method breadthFirstOrder.

private static List<Method> breadthFirstOrder(MethodOrder methodOrder, Method startMethod) {
    final Queue<Method> queue = new LinkedList<>();
    final List<Method> result = new ArrayList<>();
    queue.add(startMethod);
    while (result.size() < methodOrder.getMethods().size()) {
        if (queue.isEmpty()) {
            methodOrder.getMethods().stream().filter(method -> !result.contains(method)).findFirst().ifPresent(queue::add);
        } else {
            final Method head = queue.remove();
            if (!result.contains(head)) {
                result.add(head);
                methodOrder.getMethodDependenciesInAppearanceOrder(head).stream().filter(callee -> !result.contains(callee)).forEach(queue::add);
            }
        }
    }
    return result;
}
Also used : PenaltyCalculator(com.github.sevntu.checkstyle.ordering.PenaltyCalculator) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Queue(java.util.Queue) Comparator(java.util.Comparator) LinkedList(java.util.LinkedList) Collections(java.util.Collections) ArrayList(java.util.ArrayList) Method(com.github.sevntu.checkstyle.ordering.Method) LinkedList(java.util.LinkedList)

Example 15 with MethodOrder

use of com.github.sevntu.checkstyle.ordering.MethodOrder in project methods-distance by sevntu-checkstyle.

the class DependencyInfoGraphSerializer method serializeInfo.

public static String serializeInfo(Dependencies dependencies) {
    final MethodOrder info = new MethodOrder(dependencies);
    final Graph graph = new Graph("dependencies");
    graph.setRankdir(Rankdirs.LR);
    final Cluster simpleMethods = new Cluster("simple");
    final Map<Method, Node> methodToNode = info.getMethods().stream().filter(method -> !info.isInterfaceMethod(method)).collect(Collectors.toMap(Function.identity(), DependencyInfoGraphSerializer::createNode));
    methodToNode.entrySet().stream().forEach(methodAndNode -> {
        if (info.hasMethodDependencies(methodAndNode.getKey())) {
            graph.addComponent(methodAndNode.getValue());
        } else {
            simpleMethods.addComponent(methodAndNode.getValue());
        }
    });
    graph.addComponent(simpleMethods);
    for (final Method caller : methodToNode.keySet()) {
        for (final Method callee : info.getMethodDependenciesInAppearanceOrder(caller)) {
            graph.addComponent(createEdge(caller, callee, methodToNode, info));
        }
    }
    final Comment comment = new Comment(getDescription());
    graph.addComponent(comment);
    return serialize(graph);
}
Also used : PrintWriter(java.io.PrintWriter) Cluster(com.github.sevntu.checkstyle.dot.domain.Cluster) Graph(com.github.sevntu.checkstyle.dot.domain.Graph) Element(com.github.sevntu.checkstyle.dot.domain.Element) FileUtils(com.github.sevntu.checkstyle.utils.FileUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Shapes(com.github.sevntu.checkstyle.dot.domain.Shapes) Method(com.github.sevntu.checkstyle.ordering.Method) List(java.util.List) Colors(com.github.sevntu.checkstyle.dot.domain.Colors) Rankdirs(com.github.sevntu.checkstyle.dot.domain.Rankdirs) Map(java.util.Map) AttributeHolder(com.github.sevntu.checkstyle.dot.domain.AttributeHolder) Comment(com.github.sevntu.checkstyle.dot.domain.Comment) Node(com.github.sevntu.checkstyle.dot.domain.Node) Edge(com.github.sevntu.checkstyle.dot.domain.Edge) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) Comment(com.github.sevntu.checkstyle.dot.domain.Comment) Graph(com.github.sevntu.checkstyle.dot.domain.Graph) Node(com.github.sevntu.checkstyle.dot.domain.Node) Cluster(com.github.sevntu.checkstyle.dot.domain.Cluster) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method)

Aggregations

MethodOrder (com.github.sevntu.checkstyle.ordering.MethodOrder)23 Test (org.junit.Test)15 Method (com.github.sevntu.checkstyle.ordering.Method)10 ArrayList (java.util.ArrayList)4 Dependencies (com.github.sevntu.checkstyle.domain.Dependencies)3 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)3 List (java.util.List)3 Map (java.util.Map)3 DependencyInformationConsumer (com.github.sevntu.checkstyle.module.DependencyInformationConsumer)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)1 MethodCallDependencyCheckInvoker (com.github.sevntu.checkstyle.common.MethodCallDependencyCheckInvoker)1 BaseCheckTestSupport (com.github.sevntu.checkstyle.domain.BaseCheckTestSupport)1 ExpectedDependencies (com.github.sevntu.checkstyle.domain.ExpectedDependencies)1 AttributeHolder (com.github.sevntu.checkstyle.dot.domain.AttributeHolder)1 Cluster (com.github.sevntu.checkstyle.dot.domain.Cluster)1 Colors (com.github.sevntu.checkstyle.dot.domain.Colors)1 Comment (com.github.sevntu.checkstyle.dot.domain.Comment)1 Edge (com.github.sevntu.checkstyle.dot.domain.Edge)1