LOCAL_VARIABLE_READ_FILTER(CtLocalVariable localVariable) { super(CtVariableRead.class); this.localVariableReference = localVariable.getReference(); }
LOCAL_VARIABLE_READ_FILTER(CtLocalVariable localVariable) { super(CtVariableRead.class); this.localVariableReference = localVariable.getReference(); }
private void inlineLocalVariable(CtMethod<?> amplifiedTestToBeMinimized) { final List<CtLocalVariable> localVariables = amplifiedTestToBeMinimized.getElements(new TypeFilter<>(CtLocalVariable.class)); final List<CtVariableRead> variableReads = localVariables.stream().map(LOCAL_VARIABLE_READ_FILTER::new) .flatMap(filter -> amplifiedTestToBeMinimized.getElements(filter).stream()) .collect(Collectors.toList()); // we can inline all local variables that are used one time and that have been generated by DSpot final List<CtLocalVariable> oneTimeUsedLocalVariable = localVariables.stream() .filter(localVariable -> variableReads.stream() .map(CtVariableRead::getVariable) .filter(variableRead -> variableRead.equals(localVariable.getReference())) .count() == 1 ).collect(Collectors.toList()); oneTimeUsedLocalVariable.stream().map(localVariable -> { DSpotUtils.printProgress(oneTimeUsedLocalVariable.indexOf(localVariable), oneTimeUsedLocalVariable.size()); variableReads.stream() .filter(variableRead -> variableRead.getVariable().equals(localVariable.getReference()) ).findFirst() .get() .replace(localVariable.getAssignment().clone()); return localVariable; }).forEach(amplifiedTestToBeMinimized.getBody()::removeStatement); //TODO we can inline all local variables that are used only in assertion }
private void inlineLocalVariable(CtMethod<?> amplifiedTestToBeMinimized) { final List<CtLocalVariable> localVariables = amplifiedTestToBeMinimized.getElements(new TypeFilter<>(CtLocalVariable.class)); final List<CtVariableRead> variableReads = localVariables.stream().map(LOCAL_VARIABLE_READ_FILTER::new) .flatMap(filter -> amplifiedTestToBeMinimized.getElements(filter).stream()) .collect(Collectors.toList()); // we can inline all local variables that are used one time and that have been generated by DSpot final List<CtLocalVariable> oneTimeUsedLocalVariable = localVariables.stream() .filter(localVariable -> variableReads.stream() .map(CtVariableRead::getVariable) .filter(variableRead -> variableRead.equals(localVariable.getReference())) .count() == 1 ).collect(Collectors.toList()); oneTimeUsedLocalVariable.stream().map(localVariable -> { DSpotUtils.printProgress(oneTimeUsedLocalVariable.indexOf(localVariable), oneTimeUsedLocalVariable.size()); variableReads.stream() .filter(variableRead -> variableRead.getVariable().equals(localVariable.getReference()) ).findFirst() .get() .replace(localVariable.getAssignment().clone()); return localVariable; }).forEach(amplifiedTestToBeMinimized.getBody()::removeStatement); //TODO we can inline all local variables that are used only in assertion }
private void replaceInvocationByLocalVariable(CtStatement invocationToBeReplaced, CtLocalVariable localVariable) { if (invocationToBeReplaced.getParent() instanceof CtBlock) { invocationToBeReplaced.replace(localVariable); } else { CtElement parent = invocationToBeReplaced.getParent(); while (!(parent.getParent() instanceof CtBlock)) { parent = invocationToBeReplaced.getParent(); } ((CtStatement) parent).insertBefore(localVariable); invocationToBeReplaced.replace(localVariable.getReference()); } }
arguments.add(factory.createVariableRead(localVariable.getReference(), false)); } catch (Exception e) { throw new RuntimeException(e);
wasStageInitRead.setVariable(wasStageInit.getReference());
} else if (stmt instanceof CtLocalVariable) { CtLocalVariable localVar = (CtLocalVariable) stmt; final CtVariableAccess variableRead = stmt.getFactory().createVariableRead(localVar.getReference(), false);// TODO checks static invocationToObjectLog.addArgument(variableRead); invocationToObjectLog.addArgument(stmt.getFactory().createLiteral(localVar.getSimpleName())); throw new RuntimeException(e); invocationToObjectLog.addArgument(stmt.getFactory().createVariableRead(localVariable.getReference(), false)); invocationToObjectLog.addArgument(stmt.getFactory().createLiteral("o_" + id)); insertAfter = localVariable;
@Override public void visitCtForEach(CtForEach foreach) { if (foreach.getExpression() instanceof CtFieldAccess) { CtFieldAccess<?> fa = (CtFieldAccess<?>) foreach.getExpression(); if (Parameters.isParameterSource(fa.getVariable())) { Object[] value = (Object[]) Parameters.getValue(template, fa.getVariable().getSimpleName(), null); CtBlock<?> l = foreach.getFactory().Core().createBlock(); CtStatement body = foreach.getBody(); for (Object element : value) { CtStatement b = foreach.getFactory().Core().clone(body); for (CtVariableAccess<?> va : Query.getElements(b, new VariableAccessFilter<CtVariableAccess<?>>(foreach.getVariable().getReference()))) { va.replace((CtExpression) element); } l.addStatement(b); } foreach.replace(l); throw new SkipException(foreach); } } super.visitCtForEach(foreach); }
@Override public void process(CtStatement statement) { String evaluationAccess = "runtimeAngelicValue"; CtLocalVariable defaultValue = newLocalVariableDeclarationString(statement.getFactory(), cl, "spoonDefaultValue", "false"); insertBeforeUnderSameParent(defaultValue, statement); CtTry aTry = getFactory().Core().createTry(); CtAssignment variableAssignment = getFactory().Code().createVariableAssignment(defaultValue.getReference(), false, getFactory().Code().createCodeSnippetExpression(subprocessor().getDefaultValue())); aTry.setBody(getFactory().Code().createCtBlock(variableAssignment)); CtCatch aCatch = getFactory().Core().createCatch(); CtCatchVariable<Exception> nopolProcessorException = getFactory().Code().createCatchVariable(getFactory().Type().createReference(Exception.class), "__NopolProcessorException"); aCatch.setParameter(nopolProcessorException); aCatch.setBody(getFactory().Core().createBlock()); aTry.addCatcher(aCatch); insertBeforeUnderSameParent(aTry, statement); String evaluationValue = angelicInvocation("spoonDefaultValue"); CtLocalVariable<T> evaluation = newLocalVariableDeclarationString(statement.getFactory(), cl, evaluationAccess, evaluationValue); insertBeforeUnderSameParent(evaluation, statement); ((ConditionalProcessor) subprocessor()).processCondition(statement, evaluationAccess); }
@Override public void process(CtStatement statement) { String evaluationAccess = "runtimeAngelicValue"; CtLocalVariable defaultValue = newLocalVariableDeclarationString(statement.getFactory(), Boolean.class, "spoonDefaultValue", "false"); insertBeforeUnderSameParent(defaultValue, statement); CtTry aTry = getFactory().Core().createTry(); CtAssignment variableAssignment = getFactory().Code().createVariableAssignment(defaultValue.getReference(), false, getFactory().Code().createCodeSnippetExpression(subprocessor().getDefaultValue())); aTry.setBody(getFactory().Code().createCtBlock(variableAssignment)); CtCatch aCatch = getFactory().Core().createCatch(); CtCatchVariable<Exception> nopolProcessorException = getFactory().Code().createCatchVariable(getFactory().Type().createReference(Exception.class), "__NopolProcessorException"); aCatch.setParameter(nopolProcessorException); aCatch.setBody(getFactory().Core().createBlock()); aTry.addCatcher(aCatch); insertBeforeUnderSameParent(aTry, statement); String evaluationValue = angelicInvocation("spoonDefaultValue"); CtLocalVariable<Boolean> evaluation = newLocalVariableDeclaration(statement.getFactory(), Boolean.class, evaluationAccess, evaluationValue); insertBeforeUnderSameParent(evaluation, statement); appendValueCollection(statement, evaluationAccess, "spoonDefaultValue"); ((ConditionalProcessor) subprocessor()).processCondition(statement, evaluationAccess); }