Search in sources :

Example 6 with Method

use of com.github.sevntu.checkstyle.ordering.Method 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 7 with Method

use of com.github.sevntu.checkstyle.ordering.Method 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 8 with Method

use of com.github.sevntu.checkstyle.ordering.Method 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)

Example 9 with Method

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

the class MethodDefinitionTest method testGetterSetterRecognitionsWithCtors.

@Test
public void testGetterSetterRecognitionsWithCtors() throws Exception {
    final MethodOrder dc = withDefaultConfigOrdering("InputMethodDefinition2.java");
    final Method noArgCtor = dc.getMethodByInitialIndex(0);
    assertFalse(noArgCtor.isGetter());
    assertFalse(noArgCtor.isSetter());
    final Method singleArgCtor = dc.getMethodByInitialIndex(1);
    assertFalse(singleArgCtor.isGetter());
    assertFalse(singleArgCtor.isSetter());
}
Also used : MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) Test(org.junit.Test)

Example 10 with Method

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

the class TopologicalMethodReorderer method methodDependenciesRelativeOrderOptimization.

private MethodOrder methodDependenciesRelativeOrderOptimization(MethodOrder methodOrder) {
    MethodOrder currentMethodOrder = methodOrder;
    for (final Method caller : currentMethodOrder.getMethods()) {
        final List<Method> dependenciesInAppearanceOrder = currentMethodOrder.getMethodDependenciesInAppearanceOrder(caller);
        if (dependenciesInAppearanceOrder.size() > 1) {
            final List<Integer> dependenciesIndices = dependenciesInAppearanceOrder.stream().map(currentMethodOrder::getMethodIndex).sorted(Integer::compare).collect(Collectors.toList());
            final List<Method> optimized = new ArrayList<>(currentMethodOrder.getMethods());
            optimized.removeAll(dependenciesInAppearanceOrder);
            for (int i = 0; i < dependenciesIndices.size(); ++i) {
                optimized.add(dependenciesIndices.get(i), dependenciesInAppearanceOrder.get(i));
            }
            final MethodOrder optimizedMethodOrder = currentMethodOrder.reorder(optimized);
            currentMethodOrder = getBestOrdering(currentMethodOrder, optimizedMethodOrder);
        }
    }
    return currentMethodOrder;
}
Also used : ArrayList(java.util.ArrayList) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method)

Aggregations

Method (com.github.sevntu.checkstyle.ordering.Method)10 MethodOrder (com.github.sevntu.checkstyle.ordering.MethodOrder)10 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 List (java.util.List)3 Dependencies (com.github.sevntu.checkstyle.domain.Dependencies)2 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 DependencyInformationConsumerInjector (com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector)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 Element (com.github.sevntu.checkstyle.dot.domain.Element)1 Graph (com.github.sevntu.checkstyle.dot.domain.Graph)1