static List<Integer> replaceByLogStatement(CtMethod<?> clone) { final List<CtInvocation> assertionsToBeReplaced = clone.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) { @Override public boolean matches(CtInvocation element) { return Util.isAssert.test(element) && super.matches(element); } }).stream() .sorted(Comparator.comparingInt(ctInvocation -> ctInvocation.getPosition().getLine())) .collect(Collectors.toList()); final List<Integer> indices = new ArrayList<>(); assertionsToBeReplaced.forEach(assertionToBeReplaced -> { final int index = assertionToBeReplaced.getParent(CtBlock.class).getStatements().indexOf(assertionToBeReplaced); indices.add(index); String snippet = "eu.stamp.project.assertfixer.asserts.log.Logger.log("; if (assertionToBeReplaced.getExecutable().getSimpleName().endsWith("True") || assertionToBeReplaced.getExecutable().getSimpleName().endsWith("False")) { assertionToBeReplaced.replace( clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(0) + ")") ); } else { assertionToBeReplaced.replace( clone.getFactory().createCodeSnippetStatement(snippet + index + "," + assertionToBeReplaced.getArguments().get(1) + ")") ); } }); return indices; }
testCaseToBeFix.getBody().getStatement(index).replace(factory.createCodeSnippetStatement(snippet)); } else if ("assertSame".equals(((CtInvocation) valueToReplace.getParent()).getExecutable().getSimpleName())) { ((CtInvocation) valueToReplace.getParent()).replace(factory.createCodeSnippetStatement( valueToReplace.getParent().toString().replace("assertSame", "assertNotSame") ((CtInvocation) valueToReplace.getParent()).replace(factory.createCodeSnippetStatement( valueToReplace.getParent().toString().replace("assertSame", "assertSame")
private void guardStageMethodCalls() { cxt.allNodes().forEach(node -> { List<CtInvocation<?>> invocations = node.filterBlocksForBuildingDeps((CtInvocation<?> inv) -> true); invocations.forEach(invocation -> { // the case when the declaring type is out of classpath, // to avoid NPE on the next line if (invocation.getExecutable().getDeclaringType() == null) return; CtExecutable<?> declaration = invocation.getExecutable().getDeclaration(); if (declaration == null || !(declaration instanceof CtMethod)) return; CtMethod<?> method = (CtMethod<?>) declaration; StageModel stage = cxt.getStageModelByStageMethod(method); if (stage != null && stage != node) { CtTargetedExpression guardedInvocation = stage.guardedStageMethodCall((CtInvocation) invocation, method); CtExpression<?> target = invocation.getTarget(); guardedInvocation.setTarget(target); if (target != null) target.setParent(guardedInvocation); invocation.replace(guardedInvocation); } }); }); }
ctInvocation.replace(replaceAssertEquals(ctInvocation)); break; case "assertFalse": ctInvocation.replace(replaceAssertTrueFalse(false, ctInvocation)); break; case "assertTrue": ctInvocation.replace(replaceAssertTrueFalse(true, ctInvocation)); break; case "assertNull": ctInvocation.replace(replaceAssertNull(ctInvocation)); break; case "assertNotNull": ctInvocation.replace(replaceAssertNotNull(ctInvocation)); break; default:
invocation.replace((CtExpression) r);