protected List<CtStatement> getAssertStatement(CtMethod method) { List<CtStatement> statements = Query.getElements(method, new TypeFilter(CtStatement.class)); return statements .stream() .filter(stmt -> stmt.getParent() instanceof CtBlock) .filter(stmt -> AmplificationChecker.isAssert(stmt)) .collect(Collectors.toList()); }
public void reset(Coverage coverage, CtType testClass) { AmplificationHelper.reset(); literalsByMethod = new HashMap<>(); Set<CtType> codeFragmentsProvide = AmplificationHelper.computeClassProvider(testClass); List<Statement> codeFragmentsByClass = codeFragmentsProvide .stream() .flatMap( cl -> { List<CtStatement> list = Query.getElements(cl, new TypeFilter(CtStatement.class)); return list.stream(); }) .filter( stmt -> { try { return stmt.getParent() != null; } catch (Exception e) { return false; } }) .filter(stmt -> stmt.getParent() instanceof CtBlock) .filter(stmt -> !stmt.toString().startsWith("super")) .filter(stmt -> !stmt.toString().startsWith("this(")) .map(stmt -> new Statement(stmt)) .collect(Collectors.toList()); if (findClassUnderTest(testClass) != null) { coverageBycodeFragments = buildCodeFragmentFor(findClassUnderTest(testClass), coverage); } else { coverageBycodeFragments = new HashMap<>(); } Set<Integer> ids = new HashSet<>(); localVars = codeFragmentsByClass .stream() .filter(cf -> isValidCodeFragment(cf)) .filter(cf -> ids.add(cf.id())) .collect(Collectors.toList()); }
protected CtMethod apply(CtMethod method, List<Statement> statements, int index) { CtMethod cloned_method = AmplificationHelper.cloneMethodTest(method, "_cf", 1000); CtStatement stmt = getAssertStatement(cloned_method).get(index); statements .stream() .forEach( c -> { stmt.insertBefore((CtStatement) c.getCtCodeFragment()); c.getCtCodeFragment().setParent(stmt.getParent()); }); return cloned_method; }
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; } }