/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = getTemplateCtClass(targetClass, template); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
CtBlock<?> block = (CtBlock<?>) pair.element; if (block.getStatements().size() == 1) { CtStatement childStmt = block.getStatement(0); if (childStmt.getLabel() == null) {
public static boolean remove(CtBlock parentBlock, CtStatement fixStatement, int pos) { CtStatement s = parentBlock.getStatement(pos); // To be sure that the position has the element we // want to remove if (fixStatement.equals(s)) { parentBlock.getStatements().remove(pos); return true; } else { System.out.println("\n fx: " + fixStatement + "\n" + (s)); throw new IllegalStateException("Undo: Not valid fix position"); } } }
SourcePosition thenPosition = thenStatement.getPosition(); if (!thenPosition.isValidPosition() && thenStatement instanceof CtBlock) { CtStatement thenExpression = ((CtBlock) thenStatement).getStatement(0); thenPosition = thenExpression.getPosition(); CtStatement elseExpression = ((CtBlock) elseStatement).getStatement(0); elsePosition = elseExpression.getPosition();
@Override public boolean undoChangesInModel(OperatorInstance operation, ProgramVariant p) { StatementOperatorInstance stmtoperator = (StatementOperatorInstance) operation; CtStatement ctst = (CtStatement) operation.getOriginal(); CtStatement fix = (CtStatement) operation.getModified(); CtBlock<?> parentBlock = stmtoperator.getParentBlock(); if (parentBlock != null) { if (fix instanceof CtBlock) { CtBlock movedBlock = (CtBlock) fix; CtStatement firstFromBlock = movedBlock.getStatement(0); firstFromBlock.replace((CtStatement) ctst); for (CtStatement s : movedBlock.getStatements()) { parentBlock.removeStatement(s); } return true; } else { fix.replace((CtStatement) ctst); return true; } } return false; }
public static CtStatement statementOf(CtMethod<?> method, int statementNumber) { if (numberOfStatements(method) < statementNumber) { method.getBody().getStatement(statementNumber); } return null; }
static void addTryCatchFailAssertion(Launcher spoon, CtMethod<?> testCaseToBeFix, Failure failure) { final Factory factory = spoon.getFactory(); final CtTry aTry = factory.createTry(); final CtCatch aCatch = factory.createCatch(); aTry.addCatcher(aCatch); String fullQualifiedNameException = failure.fullQualifiedNameOfException; CtTypeReference reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException); final String[] splittedNameException = fullQualifiedNameException.split("\\."); String exceptionName = splittedNameException[splittedNameException.length - 1]; if (isAnonymous.test(failure.fullQualifiedNameOfException)) { reference = testCaseToBeFix.getFactory().Type().createReference(fullQualifiedNameException).getSuperclass(); } final CtCatchVariable<? extends Throwable> catchVariable = testCaseToBeFix.getFactory().createCatchVariable( reference, PREFIX_NAME_EXPECTED_EXCEPTION + exceptionName ); aCatch.setParameter(catchVariable); aCatch.setBody(factory.createCodeSnippetStatement( "org.junit.Assert.assertTrue(true)" )); aTry.setBody(testCaseToBeFix.getBody().getStatement(0)); for (int i = 1; i < testCaseToBeFix.getBody().getStatements().size(); i++) { aTry.getBody().addStatement(testCaseToBeFix.getBody().getStatement(i)); } aTry.getBody().addStatement(factory .createCodeSnippetStatement("org.junit.Assert." + NAME_FAIL_METHOD + "(\"" + PREFIX_MESSAGE_EXPECTED_EXCEPTION + exceptionName + "\")") ); testCaseToBeFix.setBody(aTry); }
SourcePosition thenPosition = thenStmt.getPosition(); if (!thenPosition.isValidPosition() && thenStmt instanceof CtBlock) { CtStatement thenExpression = ((CtBlock) thenStmt).getStatement(0); thenPosition = thenExpression.getPosition();
/** * Gets a statement from a template executable with all the template * parameters substituted. * * @param targetClass * the target class * @param template * the template that holds the executable * @param statementIndex * the statement index in the executable's body * @param executableName * the source executable template * @param parameterTypes * the parameter types of the source executable * @return the body expression of the source executable template with all * the template parameters substituted */ public static CtStatement substituteStatement(CtClass<?> targetClass, Template<?> template, int statementIndex, String executableName, CtTypeReference<?>... parameterTypes) { CtClass<?> sourceClass = targetClass.getFactory().Class().get(template.getClass()); CtExecutable<?> sourceExecutable = executableName.equals(template.getClass().getSimpleName()) ? sourceClass.getConstructor(parameterTypes) : sourceClass.getMethod(executableName, parameterTypes); return substitute(targetClass, template, sourceExecutable.getBody().getStatement(statementIndex)); }
boolean replaced = false; final CtElement valueToReplace = (CtElement) ((CtInvocation) testCaseToBeFix.getBody() .getStatement(index)).getArguments().get(0); final CtComment comment = factory.createComment("AssertFixer: old assertion " + testCaseToBeFix.getBody().getStatement(index).toString(), CtComment.CommentType.INLINE); if (Logger.observations.containsKey(index)) { String snippet = ((CtInvocation) testCaseToBeFix.getBody().getStatement(index)).getTarget().toString() + ".assert" + Logger.observations.get(index).toString().toUpperCase().substring(0, 1) + Logger.observations.get(index).toString().substring(1) + "(" + valueToReplace + ")"; testCaseToBeFix.getBody().getStatement(index).replace(factory.createCodeSnippetStatement(snippet)); } else if ("assertSame".equals(((CtInvocation) valueToReplace.getParent()).getExecutable().getSimpleName())) { ((CtInvocation) valueToReplace.getParent()).replace(factory.createCodeSnippetStatement( testCaseToBeFix.getBody().getStatement(index).addComment(comment);
private boolean isElseIf(CtStatement original, CtStatement parentLine) { if (parentLine.getParent() instanceof CtIf) { CtStatement elseStatement = ((CtIf) parentLine.getParent()).getElseStatement(); if (elseStatement == original) { return true; } else if (elseStatement instanceof CtBlock) { CtBlock block = (CtBlock) elseStatement; if (block.isImplicit() && block.getStatement(0) == original) { return true; } } } if (parentLine.getParent() instanceof CtBlock) { return isElseIf(original, (CtStatement) parentLine.getParent()); } return false; }