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; }
/** * Creates a method by copying an existing method. * * @param <T> the type of the method * @param target the target type where the new method has to be inserted to * @param source the source method to be copied * @param redirectReferences tells if all the references to the owning type of the source method * should be redirected to the target type (true is recommended for most uses) * @return the newly created method */ public <T> CtMethod<T> create(CtType<?> target, CtMethod<T> source, boolean redirectReferences) { CtMethod<T> newMethod = factory.Core().clone(source); if (redirectReferences && (source.getDeclaringType() != null)) { Substitution.redirectTypeReferences( newMethod, source.getDeclaringType().getReference(), target.getReference()); } target.getMethods().add(newMethod); return newMethod; }