Source:
private static Graph<ITestNGMethod> topologicalSort(ITestNGMethod[] methods, List<ITestNGMethod> sequentialList, List<ITestNGMethod> parallelList, final Comparator<ITestNGMethod> comparator) { Graph<ITestNGMethod> result = new Graph<>(new Comparator<Node<ITestNGMethod>>() { @Override public int compare(Node<ITestNGMethod> o1, Node<ITestNGMethod> o2) { return comparator.compare(o1.getObject(), o2.getObject()); } }); if (methods.length == 0) { return result; } LinkedHashMap<Object, ArrayList<ITestNGMethod>> testInstances = sortMethodsByInstance(methods); for (ITestNGMethod m : methods) { result.addNode(m); List<ITestNGMethod> predecessors = Lists.newArrayList(); String[] methodsDependedUpon = m.getMethodsDependedUpon(); String[] groupsDependedUpon = m.getGroupsDependedUpon(); if (methodsDependedUpon.length > 0) { ITestNGMethod[] methodsNamed = null; if (m.getInstance() != null) { ArrayList<ITestNGMethod> instanceMethods = testInstances.get(m.getInstance()); ITestNGMethod[] allInstanceMethods = instanceMethods.toArray(new ITestNGMethod[instanceMethods.size()]); methodsNamed = MethodHelper.findDependedUponMethods(m, allInstanceMethods); } else { methodsNamed = MethodHelper.findDependedUponMethods(m, methods); } for (ITestNGMethod pred : methodsNamed) { predecessors.add(pred); } } if (groupsDependedUpon.length > 0) { for (String group : groupsDependedUpon) { ITestNGMethod[] methodsThatBelongToGroup = MethodGroupsHelper.findMethodsThatBelongToGroup(m, methods, group); for (ITestNGMethod pred : methodsThatBelongToGroup) { predecessors.add(pred); } } } for (ITestNGMethod predecessor : predecessors) { result.addPredecessor(m, predecessor); } } result.topologicalSort(); sequentialList.addAll(result.getStrictlySortedNodes()); parallelList.addAll(result.getIndependentNodes()); return result; }

Target:
private static Graph<ITestNGMethod> topologicalSort(ITestNGMethod[] methods, List<ITestNGMethod> sequentialList, List<ITestNGMethod> parallelList, final Comparator<ITestNGMethod> comparator) { Graph<ITestNGMethod> result = new Graph<>(new Comparator<Node<ITestNGMethod>>() { @Override public int compare(Node<ITestNGMethod> o1, Node<ITestNGMethod> o2) { return comparator.compare(o1.getObject(), o2.getObject()); } }); if (methods.length == 0) { return result; } Map<Object, List<ITestNGMethod>> testInstances = sortMethodsByInstance(methods); for (ITestNGMethod m : methods) { result.addNode(m); List<ITestNGMethod> predecessors = Lists.newArrayList(); String[] methodsDependedUpon = m.getMethodsDependedUpon(); String[] groupsDependedUpon = m.getGroupsDependedUpon(); if (methodsDependedUpon.length > 0) { ITestNGMethod[] methodsNamed = null; if (m.getInstance() != null) { List<ITestNGMethod> instanceMethods = testInstances.get(m.getInstance()); methodsNamed = MethodHelper.findDependedUponMethods(m, instanceMethods); } else { methodsNamed = MethodHelper.findDependedUponMethods(m, methods); } for (ITestNGMethod pred : methodsNamed) { predecessors.add(pred); } } if (groupsDependedUpon.length > 0) { for (String group : groupsDependedUpon) { ITestNGMethod[] methodsThatBelongToGroup = MethodGroupsHelper.findMethodsThatBelongToGroup(m, methods, group); for (ITestNGMethod pred : methodsThatBelongToGroup) { predecessors.add(pred); } } } for (ITestNGMethod predecessor : predecessors) { result.addPredecessor(m, predecessor); } } result.topologicalSort(); sequentialList.addAll(result.getStrictlySortedNodes()); parallelList.addAll(result.getIndependentNodes()); return result; }