コード例 #1
0
  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;
  }
コード例 #2
0
  protected double getCoverageForMethod(Coverage coverage, CtType cl, CtMethod mth) {
    if (coverage == null) {
      return 0d;
    }

    String key =
        mth.getDeclaringType().getQualifiedName()
            + "_"
            + mth.getType().getQualifiedName()
            + "_"
            + mth.getSimpleName()
            + "("
            + mth.getParameters()
                .stream()
                .map(param -> ((CtParameter) param).getType().getQualifiedName())
                .collect(Collectors.joining(","))
            + ")";

    if (coverage.getMethodCoverage(key) != null) {
      return coverage.getMethodCoverage(key).coverage();
    } else {
      key =
          cl.getQualifiedName()
              + "_"
              + mth.getType().getQualifiedName()
              + "_"
              + mth.getSimpleName()
              + "("
              + mth.getParameters()
                  .stream()
                  .map(param -> ((CtParameter) param).getType().getQualifiedName())
                  .collect(Collectors.joining(","))
              + ")";
      if (coverage.getMethodCoverage(key) != null) {
        return coverage.getMethodCoverage(key).coverage();
      } else {
        return 0d;
      }
    }
  }