protected List<List<Statement>> buildContext( InputContext inputContext, List<Statement> stmts, int targetIndex) { VarCartesianProduct varCartesianProduct = new VarCartesianProduct(); Statement statement = stmts.get(targetIndex); for (CtVariableReference var : statement.getInputContext().getVar()) { varCartesianProduct.addReplaceVar(var, valueCreator.createNull(var.getType())); List<CtVariableReference> candidates = inputContext.allCandidate(var.getType(), true, false); if (!candidates.isEmpty()) { varCartesianProduct.addReplaceVar( var, candidates.get(AmplificationHelper.getRandom().nextInt(candidates.size()))); } Statement cfLocalVar = getLocalVar(var.getType(), inputContext); if (cfLocalVar != null) { varCartesianProduct.addReplaceVar(var, cfLocalVar); } CtLocalVariable localVariable = createLocalVarFromMethodLiterals(currentMethod, var.getType()); if (localVariable != null) { varCartesianProduct.addReplaceVar(var, localVariable); } CtLocalVariable randomVar = valueCreator.createRandomLocalVar(var.getType()); if (randomVar != null) { varCartesianProduct.addReplaceVar(var, randomVar); } } return varCartesianProduct.apply(stmts, targetIndex); }
protected CtLocalVariable createLocalVarFromMethodLiterals( CtMethod method, CtTypeReference type) { List<CtLiteral> literals = getLiterals(method) .stream() .filter(lit -> lit.getType() != null) .filter(lit -> lit.getType().equals(type)) .collect(Collectors.toList()); if (literals.isEmpty()) { return null; } CtLiteral lit = literals.get(AmplificationHelper.getRandom().nextInt(literals.size())); return type.getFactory().Code().createLocalVariable(type, "vc_" + count++, lit); }
@Override public List<CtMethod> apply(CtMethod method) { currentMethod = method; List<CtMethod> newMethods = new ArrayList<>(); if (!coverageBycodeFragments.isEmpty()) { List<InputContext> inputContexts = getInputContexts(method); if (!inputContexts.isEmpty()) { int index = inputContexts.size() - 1; List<List<Statement>> statements = buildStatements(inputContexts.get(index)); for (List<Statement> list : statements) { try { newMethods.add(apply(method, list, index)); } catch (Exception e) { throw new RuntimeException(e); } } } } return AmplificationHelper.updateAmpTestToParent(newMethods, method); }
protected List<CtLocalVariable> getLocalVarInScope(CtStatement stmt) { List<CtLocalVariable> vars = new ArrayList<>(); try { CtBlock parentBlock = stmt.getParent(CtBlock.class); if (parentBlock != null) { boolean beforeCurrentStmt = true; int i = 0; List<CtStatement> stmts = parentBlock.getStatements(); while (beforeCurrentStmt && i < stmts.size()) { CtStatement currentStatement = stmts.get(i); i++; beforeCurrentStmt = beforeCurrentStmt && currentStatement != stmt; if (currentStatement instanceof CtLocalVariable) { vars.add((CtLocalVariable) currentStatement); } } vars.addAll(getLocalVarInScope(parentBlock)); } } catch (Exception e) { throw new RuntimeException(e); } return vars; }