Ejemplo n.º 1
0
 @Override
 public <C extends CtTargetedExpression<T, CtExpression<?>>> C setTarget(CtExpression<?> target) {
   if (target != null) {
     target.setParent(this);
   }
   this.target = target;
   return null;
 }
Ejemplo n.º 2
0
 private void replace(CtElement e, CtElement op) {
   if (e instanceof CtStatement && op instanceof CtStatement) {
     ((CtStatement) e).replace((CtStatement) op);
     return;
   }
   if (e instanceof CtExpression && op instanceof CtExpression) {
     ((CtExpression) e).replace((CtExpression) op);
     return;
   }
   throw new IllegalArgumentException(e.getClass() + " " + op.getClass());
 }
  @Override
  public CtElement[] getMutatedEntries(CtBinaryOperator<?> element, Factory factory) {
    ArrayList<CtElement> ret = new ArrayList<CtElement>();

    if (element == null) return emptySet();

    for (BinaryOperatorKind kind : operators) {
      if (element.getKind() != kind) {
        CtExpression<?> right_c = factory.Core().clone(element.getRightHandOperand());
        CtExpression<?> left_c = factory.Core().clone(element.getLeftHandOperand());
        CtBinaryOperator<?> binaryOp = factory.Code().createBinaryOperator(left_c, right_c, kind);
        // Set parent
        right_c.setParent(binaryOp);
        left_c.setParent(binaryOp);
        ret.add(binaryOp);
      }
    }

    return ret.toArray(new CtElement[1]);
  }
Ejemplo n.º 4
0
 @Override
 public <T extends CtWhile> T setLoopingExpression(CtExpression<Boolean> expression) {
   expression.setParent(this);
   this.expression = expression;
   return (T) this;
 }
  @Override
  public void process(CtConditional ctConditional) {
    CtStatement parent = ctConditional.getParent(CtStatement.class);
    while (!(parent.getParent() instanceof CtStatementList)) {
      parent = parent.getParent(CtStatement.class);
    }
    CtExpression condition = ctConditional.getCondition();

    CtIf anIf = getFactory().Core().createIf();
    anIf.setPosition(ctConditional.getPosition());

    if (parent instanceof CtReturn) {
      if (!((CtReturn) parent).getReturnedExpression().equals(ctConditional)) {
        return;
      }

      CtReturn returnThen = (CtReturn) parent.clone();
      CtReturn returnElse = (CtReturn) parent.clone();

      returnThen.setReturnedExpression(ctConditional.getThenExpression());
      returnElse.setReturnedExpression(ctConditional.getElseExpression());

      List<CtTypeReference> typeCasts = ctConditional.getTypeCasts();
      for (int i = 0; i < typeCasts.size(); i++) {
        CtTypeReference ctTypeReference = typeCasts.get(i);
        returnThen.getReturnedExpression().addTypeCast(ctTypeReference.clone());
        returnElse.getReturnedExpression().addTypeCast(ctTypeReference.clone());
      }

      anIf.setElseStatement(getFactory().Code().createCtBlock(returnElse));
      anIf.setThenStatement(getFactory().Code().createCtBlock(returnThen));
    } else if (parent instanceof CtAssignment) {
      CtAssignment assignment = (CtAssignment) parent;
      CtExpression ctExpression = assignment.getAssignment();
      if (!ctExpression.equals(ctConditional)) {
        if (ctExpression instanceof CtBinaryOperator) {
          CtBinaryOperator ctBinaryOperator = (CtBinaryOperator) ctExpression;

          createAssignment(ctConditional, anIf, assignment);
          CtBinaryOperator cloneThen = ctBinaryOperator.clone();
          CtBinaryOperator cloneElse = ctBinaryOperator.clone();

          if (ctBinaryOperator.getLeftHandOperand().equals(ctConditional)) {
            cloneThen.setLeftHandOperand(ctConditional.getThenExpression());
            ctConditional.getThenExpression().setParent(cloneThen);
            cloneElse.setLeftHandOperand(ctConditional.getElseExpression());
          } else if (ctBinaryOperator.getRightHandOperand().equals(ctConditional)) {
            cloneThen.setRightHandOperand(ctConditional.getThenExpression());
            cloneElse.setRightHandOperand(ctConditional.getElseExpression());
          }

          cloneThen.getLeftHandOperand().setParent(cloneThen);
          cloneElse.getLeftHandOperand().setParent(cloneElse);
          ((CtAssignment) ((CtBlock) anIf.getThenStatement()).getStatement(0))
              .setAssignment(cloneThen);
          ((CtAssignment) ((CtBlock) anIf.getElseStatement()).getStatement(0))
              .setAssignment(cloneElse);
        } else {
          return;
        }
      } else {
        createAssignment(ctConditional, anIf, assignment);
      }
    } else if (parent instanceof CtLocalVariable) {
      CtLocalVariable localVariable = (CtLocalVariable) parent;
      if (!localVariable.getDefaultExpression().equals(ctConditional)) {
        return;
      }

      CtLocalVariable clone = localVariable.clone();
      clone.setDefaultExpression(null);

      localVariable.insertBefore(clone);

      CtAssignment variableAssignment =
          getFactory()
              .Code()
              .createVariableAssignment(localVariable.getReference(), false, ctConditional);
      variableAssignment.setType(localVariable.getType().clone());
      variableAssignment.setPosition(ctConditional.getPosition());
      createAssignment(ctConditional, anIf, variableAssignment);
    } else if (parent instanceof CtInvocation) {
      CtInvocation invocation = (CtInvocation) parent;
      CtInvocation cloneThen = invocation.clone();
      CtInvocation cloneElse = invocation.clone();

      List arguments = cloneThen.getArguments();
      boolean found = false;
      for (int i = 0; i < arguments.size(); i++) {
        Object o = arguments.get(i);
        if (o.equals(ctConditional)) {
          ctConditional.getThenExpression().setParent(invocation);
          arguments.set(i, ctConditional.getThenExpression());
          ctConditional.getElseExpression().setParent(invocation);
          cloneElse.getArguments().set(i, ctConditional.getElseExpression());
          found = true;
          break;
        }
      }
      if (!found) {
        return;
      }

      cloneThen.setParent(anIf);
      cloneElse.setParent(anIf);

      anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse));
      anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen));
    } else if (parent instanceof CtConstructorCall) {
      CtConstructorCall invocation = (CtConstructorCall) parent;
      CtConstructorCall cloneThen = invocation.clone();
      CtConstructorCall cloneElse = invocation.clone();

      List arguments = cloneThen.getArguments();
      boolean found = false;
      for (int i = 0; i < arguments.size(); i++) {
        Object o = arguments.get(i);
        if (o.equals(ctConditional)) {
          arguments.set(i, ctConditional.getThenExpression());
          cloneElse.getArguments().set(i, ctConditional.getElseExpression());
          found = true;
          break;
        }
      }
      if (!found) {
        return;
      }

      cloneThen.setParent(anIf);
      cloneElse.setParent(anIf);

      anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse));
      anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen));
    } else if (parent instanceof CtIf) {
      CtIf elem = (CtIf) parent;
      if (!elem.getCondition().equals(ctConditional)) {
        return;
      }

      CtIf cloneThen = elem.clone();
      cloneThen.setParent(anIf);
      CtIf cloneElse = elem.clone();
      cloneElse.setParent(anIf);

      cloneThen.setCondition(ctConditional.getThenExpression());
      ctConditional.getThenExpression().setParent(cloneThen);

      cloneElse.setCondition(ctConditional.getElseExpression());
      ctConditional.getElseExpression().setParent(cloneElse);

      anIf.setElseStatement(getFactory().Code().createCtBlock(cloneElse));
      anIf.setThenStatement(getFactory().Code().createCtBlock(cloneThen));
    } else if (parent instanceof CtThrow) {
      return;
    } else if (parent instanceof CtLoop) {
      return;
    } else if (parent instanceof CtUnaryOperator) {
      return;
    } else {
      System.err.println(parent);
      throw new RuntimeException("Other " + parent.getClass());
    }
    /*if(ctConditional.getThenExpression().getTypeCasts() == null ||
            ctConditional.getThenExpression().getTypeCasts().isEmpty()) {
        ((CtExpression)anIf.getThenStatement()).setTypeCasts(ctConditional.getTypeCasts());
    }
    if(ctConditional.getElseExpression().getTypeCasts() == null ||
            ctConditional.getElseExpression().getTypeCasts().isEmpty()) {
        ((CtExpression) anIf.getElseStatement())
                .setTypeCasts(ctConditional.getTypeCasts());
    }*/
    anIf.setCondition(condition);
    condition.setParent(anIf);
    parent.replace(anIf);
  }
Ejemplo n.º 6
0
 @Override
 public <C extends CtRHSReceiver<A>> C setAssignment(CtExpression<A> assignment) {
   assignment.setParent(this);
   this.assignment = assignment;
   return (C) this;
 }
Ejemplo n.º 7
0
 @Override
 public <C extends CtAssignment<T, A>> C setAssigned(CtExpression<T> assigned) {
   assigned.setParent(this);
   this.assigned = assigned;
   return (C) this;
 }
Ejemplo n.º 8
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;
  }