protected CtVariableRead buildVarRef(CtTypeReference type, Factory factory) {
    CtTypeReference<Object> typeRef = factory.Core().clone(type);

    CtLocalVariable<Object> localVar = factory.Core().createLocalVariable();
    localVar.setType(typeRef);
    localVar.setSimpleName("var_" + type.getSimpleName() + "_" + System.currentTimeMillis());

    CtVariableReadImpl varRead = new CtVariableReadImpl();
    varRead.setVariable(factory.Code().createLocalVariableReference(localVar));
    return varRead;
  }
  @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.º 3
0
  public CtStatement apply(CtType<?> targetType) {
    CtClass<?> c;
    Factory factory;

    // we first need a factory
    if (targetType != null) {
      // if it's template with reference replacement
      factory = targetType.getFactory();
    } else {
      // else we have at least one template parameter with a factory
      factory = getFactory();
    }

    c = factory.Class().get(this.getClass());
    if (c == null) {
      c = factory.Class().get(this.getClass());
    }
    // we substitute the first statement of method statement
    CtStatement result =
        factory.Core().clone(c.getMethod("statement").getBody().getStatements().get(0));
    new SubstitutionVisitor(factory, targetType, this).scan(result);
    return result;
  }