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)));
}
}
}
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());
}
}
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);
}
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));
}
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;
}
Aggregations