protected List<InputContext> getInputContexts(CtMethod method) { List<InputContext> inputContexts = new ArrayList<>(); List<CtStatement> statements = getAssertStatement(method); for (CtStatement stmt : statements) { Set<CtVariableReference> varRefs = new HashSet<>(); for (CtLocalVariable var : getLocalVarInScope(stmt)) { varRefs.add(method.getFactory().Code().createLocalVariableReference(var)); } inputContexts.add(new InputContext(varRefs)); } return inputContexts; }
protected List<List<Statement>> buildStatements(InputContext inputContext) { return coverageBycodeFragments .keySet() .stream() .map( cf -> { List<Statement> list = new ArrayList<>(2); list.add(cf.clone()); return list; }) .flatMap( list -> { InputContext cloneInputContext = inputContext.clone(); return buildContext(cloneInputContext, list, list.size() - 1).stream(); }) .collect(Collectors.toList()); }
@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; }