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 Statement getLocalVar(CtTypeReference type, InputContext inputContext) { List<Statement> list = localVars .stream() .filter(var -> var.getCtCodeFragment() != null) .filter(var -> type.equals(((CtLocalVariable) var.getCtCodeFragment()).getType())) .filter( var -> inputContext.getVariableOrFieldNamed( ((CtLocalVariable) var.getCtCodeFragment()).getSimpleName()) == null) .collect(Collectors.toList()); if (list.isEmpty()) { return null; } else { boolean localVarFind; while (!list.isEmpty()) { Statement localVar = list.remove(AmplificationHelper.getRandom().nextInt(list.size())); localVarFind = true; for (CtVariableReference var : localVar.getInputContext().getVar()) { CtVariableReference<?> candidate = inputContext.candidate(var.getType(), true); if (candidate == null) { localVarFind = false; break; } } if (localVarFind) { Statement cloneLocalVar = localVar.clone(); for (CtVariableReference var : localVar.getInputContext().getVar()) { try { CtVariableReference variable = cloneLocalVar.getInputContext().getVariableOrFieldNamed(var.getSimpleName()); cloneLocalVar .getInputContext() .getVariableOrFieldNamed(var.getSimpleName()) .replace(variable); } catch (Exception e) { continue; } return cloneLocalVar; } } } return null; } }
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()); }