Ejemplo n.º 1
0
  /**
   * replace assertTrue
   *
   * @param ctInvocation
   * @return
   */
  private CtStatement replaceAssertTrueFalse(boolean type, CtInvocation<?> ctInvocation) {
    List<?> arguments = ctInvocation.getArguments();
    CtIf newIf = ctInvocation.getFactory().Core().createIf();

    Object elem1 = arguments.get(0);
    if (arguments.size() > 1) {
      elem1 = arguments.get(1);
    }
    if (!type) {
      CtExpression<Boolean> condition =
          ctInvocation.getFactory().Code().createCodeSnippetExpression("!(" + elem1 + ")");
      newIf.setCondition(condition);
    } else {
      newIf.setCondition((CtExpression<Boolean>) elem1);
    }
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement(
    // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")"));
    /*
     * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core()
     * .createBlock(); // thenStatement.addStatement((getFactory().Code().
     * createCodeSnippetStatement("System.out.println(\"Then...\")")));
     * thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(" +
     * Debug.class.getCanonicalName() + ".getSolvedPC())")));
     * newIf.setThenStatement(thenStatement);
     */
    newIf.setThenStatement(createThen(ctInvocation));
    return newIf;
  }
Ejemplo n.º 2
0
  /**
   * replace assertNotNull
   *
   * @param ctInvocation
   * @return
   */
  private CtStatement replaceAssertNotNull(CtInvocation<?> ctInvocation) {
    List<?> arguments = ctInvocation.getArguments();
    CtIf newIf = ctInvocation.getFactory().Core().createIf();

    Object elem1 = arguments.get(0);
    CtExpression<Boolean> condition =
        ctInvocation.getFactory().Code().createCodeSnippetExpression("(" + elem1 + ") != null");
    newIf.setCondition(condition);
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement(
    // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")"));
    /*
     * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core()
     * .createBlock(); thenStatement .addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(\"Then...\")")));
     * thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(" +
     * Debug.class.getCanonicalName() + ".getSolvedPC())")));
     * newIf.setThenStatement(thenStatement);
     */
    newIf.setThenStatement(createThen(ctInvocation));
    return newIf;
  }
  @Override
  public void visitCtIf(CtIf ifElement) {
    // super.visitCtIf(ifElement);

    CtStatement ctThen = ifElement.getThenStatement();
    CtStatement ctElse = ifElement.getElseStatement();

    Mutability condMut = mutability(ifElement.getCondition());
    if (ctThen != null) ifElement.getThenStatement().accept(this);
    if (ctElse != null) ifElement.getElseStatement().accept(this);

    if (condMut == Mutability.ERASABLE && isEmpty(ctThen)) {
      // if ( - ) {  } else {  } <- Remove the whole if
      if (ctElse == null || isEmpty(ctElse)) remove(ifElement);
      // else if case: if ( - ) {  } else if { doSomething() } <-- pull the else if element up
      else if (ctElse instanceof CtIf) ctElse.setParent(ifElement.getParent());
    }
  }
Ejemplo n.º 4
0
  /**
   * replace assert Equals
   *
   * @param ctInvocation
   * @return
   */
  private CtStatement replaceAssertEquals(CtInvocation<?> ctInvocation) {
    List<CtExpression<?>> arguments = ctInvocation.getArguments();
    CtIf newIf = ctInvocation.getFactory().Core().createIf();
    CtCodeSnippetExpression<Boolean> condition =
        ctInvocation.getFactory().Core().createCodeSnippetExpression();
    CtExpression<?> elem1 = arguments.get(0);
    CtExpression<?> elem2 = arguments.get(1);
    if (arguments.size() > 2
        && (elem1.getType().equals(ctInvocation.getFactory().Class().STRING)
            || elem1.getType().equals(getFactory().Class().nullType()))) {
      elem1 = arguments.get(1);
      elem2 = arguments.get(2);
    }

    boolean isNull = false;
    Class<?> classArg1 = null;
    if (elem1.toString().equals("null")) {
      isNull = true;
    } else {
      try {
        classArg1 = ((CtTypedElement<?>) elem1).getType().getActualClass();
      } catch (SpoonException e) {
        e.printStackTrace();
      }
    }
    if (isNull
        || (classArg1 != null
            && (classArg1.equals(int.class)
                || classArg1.equals(Integer.class)
                || classArg1.equals(boolean.class)
                || classArg1.equals(Boolean.class)
                || classArg1.equals(byte.class)
                || classArg1.equals(Byte.class)
                || classArg1.equals(long.class)
                || classArg1.equals(Long.class)
                || classArg1.equals(double.class)
                || classArg1.equals(Double.class)
                || classArg1.equals(float.class)
                || classArg1.equals(Float.class)
                || classArg1.equals(short.class)
                || classArg1.equals(Short.class)
                || classArg1.equals(char.class)
                || classArg1.equals(Character.class)))) {
      condition.setValue("" + elem1 + " == " + elem2 + "");
    } else {
      condition.setValue("(" + elem1 + ".equals(" + elem2 + "))");
    }
    newIf.setCondition(condition);
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement(
    // Debug.class.getCanonicalName() + ".printPC(\"Path Condition: \")"));
    // newIf.setThenStatement(getFactory().Code().createCodeSnippetStatement("System.out.println("+Debug.class.getCanonicalName()+".getSolvedPC())"));
    /*
     * CtBlock<Object> thenStatement = ctInvocation.getFactory().Core()
     * .createBlock(); thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(\"Then...\")")));
     * thenStatement.addStatement((getFactory().Code()
     * .createCodeSnippetStatement("System.out.println(" +
     * Debug.class.getCanonicalName() + ".getSolvedPC())")));
     * newIf.setThenStatement(thenStatement);
     */
    newIf.setThenStatement(createThen(ctInvocation));

    return newIf;
  }