Search in sources :

Example 1 with Method

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

the class MethodCallDependenciesModuleTestSupport method mustBeSame.

private static void mustBeSame(final ExpectedDependencies expected, final MethodOrder actual) {
    for (final String expectedMethod : expected.getMethods()) {
        assertTrue("Method " + expectedMethod + " is not present is actual info", actual.getMethods().stream().anyMatch(md -> expectedMethod.equals(md.getSignature())));
    }
    for (final Method actualMethod : actual.getMethods()) {
        assertTrue("Method " + actualMethod.getSignature() + " is not present in expected info", expected.getMethods().stream().anyMatch(mi -> mi.equals(actualMethod.getSignature())));
    }
    for (final String method : expected.getMethods()) {
        final Method caller = actual.getMethods().stream().filter(m -> m.getSignature().equals(method)).findFirst().get();
        final List<Method> dependencies = actual.getMethodDependenciesInAppearanceOrder(caller);
        final List<ExpectedDependencies.MethodInvocation> invocations = expected.getInvocationsFromMethod(method);
        assertEquals("Actual method dependencies count and count of invocations from method " + method + " does not match", invocations.size(), dependencies.size());
        for (int i = 0; i < invocations.size(); ++i) {
            final Method calledMethod = dependencies.get(i);
            final ExpectedDependencies.MethodInvocation invocationOfMethod = invocations.get(i);
            assertTrue("Method " + calledMethod.getSignature() + " is present as actual " + i + " dependency of " + method + " but should not be!", calledMethod.getSignature().equals(expected.getMethodByIndex(invocationOfMethod.callee)));
        }
    }
}
Also used : DependencyInformationConsumerInjector(com.github.sevntu.checkstyle.common.DependencyInformationConsumerInjector) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Assert.assertTrue(org.junit.Assert.assertTrue) HashMap(java.util.HashMap) ExpectedDependencies(com.github.sevntu.checkstyle.domain.ExpectedDependencies) MethodCallDependencyCheckstyleModule(com.github.sevntu.checkstyle.module.MethodCallDependencyCheckstyleModule) BaseCheckTestSupport(com.github.sevntu.checkstyle.domain.BaseCheckTestSupport) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) List(java.util.List) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DependencyInformationConsumer(com.github.sevntu.checkstyle.module.DependencyInformationConsumer) Locale(java.util.Locale) Checker(com.puppycrawl.tools.checkstyle.Checker) Map(java.util.Map) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) Dependencies(com.github.sevntu.checkstyle.domain.Dependencies) Assert.assertEquals(org.junit.Assert.assertEquals) Method(com.github.sevntu.checkstyle.ordering.Method) ExpectedDependencies(com.github.sevntu.checkstyle.domain.ExpectedDependencies)

Example 2 with Method

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

the class MethodDefinitionTest method testGetterSetterRecognition.

@Test
public void testGetterSetterRecognition() throws Exception {
    final MethodOrder dc = withDefaultConfigOrdering("InputMethodDefinition1.java");
    for (final int index : new int[] { 0, 1, 2, 3, 4 }) {
        final Method method = dc.getMethodByInitialIndex(index);
        assertTrue(method.isGetter());
        assertFalse(method.isSetter());
    }
    for (final int index : new int[] { 7 }) {
        final Method method = dc.getMethodByInitialIndex(index);
        assertFalse(method.isGetter());
        assertTrue(method.isSetter());
    }
    for (final int index : new int[] { 5, 6, 8, 9, 10, 11 }) {
        final Method method = dc.getMethodByInitialIndex(index);
        assertFalse(method.isGetter());
        assertFalse(method.isSetter());
    }
}
Also used : MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) Test(org.junit.Test)

Example 3 with Method

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

the class MethodOrderReorderTest method testReordering.

@Test
public void testReordering() throws Exception {
    final int screenLinesCount = 5;
    final MethodOrder first = withDefaultConfigOrdering("InputOrderingReordering1.java");
    final Method b = first.getMethodByInitialIndex(3);
    final Method c = first.getMethodByInitialIndex(6);
    final Method d3 = first.getMethodByInitialIndex(9);
    final Method e = first.getMethodByInitialIndex(13);
    final Method f = first.getMethodByInitialIndex(15);
    final Method h = first.getMethodByInitialIndex(17);
    final MethodOrder firstStep1 = first.moveMethodBy(b, -3);
    final MethodOrder firstStep2 = firstStep1.moveMethodBy(c, -1);
    final MethodOrder firstStep3 = firstStep2.moveMethodBy(d3, 2);
    final MethodOrder firstStep4 = firstStep3.moveMethodBy(e, 1);
    final MethodOrder firstStep5 = firstStep4.moveMethodBy(f, 1);
    final MethodOrder firstLikeSecond = firstStep5.moveMethodBy(h, 1);
    final MethodOrder second = withDefaultConfigOrdering("InputOrderingReordering2.java");
    compare(second, firstLikeSecond, screenLinesCount);
}
Also used : MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) Test(org.junit.Test)

Example 4 with Method

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

the class MethodOrderTest method testDependencies.

@Test
public void testDependencies() throws Exception {
    final Configuration dc = createCheckConfig(MethodCallDependencyCheckstyleModule.class);
    final MethodOrder ord = invokeCheckAndGetOrdering(dc, "InputDependencies.java");
    final Method methodB = ord.getMethodByInitialIndex(1);
    final List<Method> dependencies = ord.getMethodDependenciesInAppearanceOrder(methodB);
    assertTrue(dependencies.size() == 1);
    assertTrue(dependencies.get(0).getSignature().equals("c()"));
    final List<Method> dependants = ord.getMethodDependants(methodB);
    assertTrue(dependants.size() == 1);
    assertTrue(dependants.get(0).getSignature().equals("a()"));
    final Method methodD = ord.getMethodByInitialIndex(3);
    assertFalse(ord.hasMethodDependants(methodD));
    assertFalse(ord.hasMethodDependencies(methodD));
    final Method methodA = ord.getMethodByInitialIndex(0);
    assertTrue(ord.isMethodDependsOn(methodA, methodB));
    assertFalse(ord.isMethodDependsOn(methodA, methodD));
}
Also used : Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) MethodOrder(com.github.sevntu.checkstyle.ordering.MethodOrder) Method(com.github.sevntu.checkstyle.ordering.Method) Test(org.junit.Test)

Example 5 with Method

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

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