protected Map<Statement, Double> buildCodeFragmentFor(CtType cl, Coverage coverage) { Factory factory = cl.getFactory(); Map<Statement, Double> codeFragments = new LinkedHashMap<>(); for (CtMethod<?> mth : (Set<CtMethod>) cl.getMethods()) { if (!mth.getModifiers().contains(ModifierKind.ABSTRACT) && !mth.getModifiers().contains(ModifierKind.PRIVATE)) { // && getCoverageForMethod(coverage, cl, mth) != 1.0) { CtExecutableReference executableRef = factory.Executable().createReference(mth); executableRef.setStatic(mth.getModifiers().contains(ModifierKind.STATIC)); CtInvocation invocation = factory.Code().createInvocation(buildVarRef(cl.getReference(), factory), executableRef); invocation.setArguments( mth.getParameters() .stream() .map(param -> buildVarRef(param.getType(), factory)) .collect(Collectors.toList())); invocation.setType(mth.getType()); Statement stmt = new Statement(invocation); codeFragments.put(stmt, getCoverageForMethod(coverage, cl, mth)); } } return codeFragments; }
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); }