Java Code Examples for org.codehaus.groovy.ast.expr.BinaryExpression#getLeftExpression()

The following examples show how to use org.codehaus.groovy.ast.expr.BinaryExpression#getLeftExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContractInputProposalsCodeVisitorSupport.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    proposals = completionComputer.computeCompletionProposals(context, monitor);
    final BinaryExpression binaryExpression = (BinaryExpression) contentAssistContext.getPerceivedCompletionNode();
    final Expression leftExpression = binaryExpression.getLeftExpression();
    String multipleInputName = null;
    if (leftExpression instanceof PropertyExpression) {
        final PropertyExpression propertyExpr = (PropertyExpression) leftExpression;
        final Expression objectExpression = propertyExpr.getProperty();
        multipleInputName = objectExpression.getText();
    } else if (leftExpression instanceof VariableExpression) {
        multipleInputName = ((VariableExpression) leftExpression).getName();
    }
    if (multipleInputName != null) {
        final ContractInput multipleInput = getInputWithName(multipleInputName, inputs);
        final ContractInput copy = EcoreUtil.copy(multipleInput);
        copy.setMultiple(false);
        final String fullyQualifiedType = ExpressionHelper.getContractInputReturnType(copy);
        if (fullyQualifiedType != null && prefix.isEmpty()) {
            proposals = getMethodProposals(contentAssistContext, context, inputs, prefix, multipleInputName, fullyQualifiedType);
        }
    }
}
 
Example 2
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected boolean doPrimitiveCompare(final ClassNode leftType, final ClassNode rightType, final BinaryExpression binExp) {
    Expression leftExp = binExp.getLeftExpression();
    Expression rightExp = binExp.getRightExpression();
    int operation = binExp.getOperation().getType();

    int operationType = getOperandConversionType(leftType,rightType);
    BinaryExpressionWriter bew = binExpWriter[operationType];

    if (!bew.write(operation, true)) return false;

    AsmClassGenerator acg = controller.getAcg();
    OperandStack os = controller.getOperandStack();
    leftExp.visit(acg);
    os.doGroovyCast(bew.getNormalOpResultType());
    rightExp.visit(acg);
    os.doGroovyCast(bew.getNormalOpResultType());
    bew.write(operation, false);

    return true;
}
 
Example 3
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected void evaluateBinaryExpressionWithAssignment(final String method, final BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    if (leftExpression instanceof BinaryExpression) {
        BinaryExpression bexp = (BinaryExpression) leftExpression;
        if (bexp.getOperation().getType() == LEFT_SQUARE_BRACKET) {
            evaluateArrayAssignmentWithOperator(method, expression, bexp);
            return;
        }
    }

    evaluateBinaryExpression(method, expression);

    // br to leave a copy of rvalue on the stack; see also isPopRequired()
    controller.getOperandStack().dup();
    controller.getCompileStack().pushLHS(true);
    leftExpression.visit(controller.getAcg());
    controller.getCompileStack().popLHS();
}
 
Example 4
Source File: FinalVariableAnalyzer.java    From groovy with Apache License 2.0 6 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    boolean assignment = StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType());
    boolean isDeclaration = expression instanceof DeclarationExpression;
    Expression leftExpression = expression.getLeftExpression();
    Expression rightExpression = expression.getRightExpression();
    if (isDeclaration) {
        recordFinalVars(leftExpression);
    }
    // visit RHS first for expressions like a = b = 0
    inAssignmentRHS = assignment;
    rightExpression.visit(this);
    inAssignmentRHS = false;
    leftExpression.visit(this);
    if (assignment) {
        recordAssignments(expression, isDeclaration, leftExpression, rightExpression);
    }
}
 
Example 5
Source File: ClassNode.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
    MethodNode constructor = getOrAddStaticConstructorNode();
    Statement statement = constructor.getCode();
    if (statement instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) statement;
        // add given statements for explicitly declared static fields just after enum-special fields
        // are found - the $VALUES binary expression marks the end of such fields.
        List<Statement> blockStatements = block.getStatements();
        ListIterator<Statement> litr = blockStatements.listIterator();
        while (litr.hasNext()) {
            Statement stmt = litr.next();
            if (stmt instanceof ExpressionStatement &&
                    ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
                BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
                if (bExp.getLeftExpression() instanceof FieldExpression) {
                    FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
                    if (fExp.getFieldName().equals("$VALUES")) {
                        for (Statement tmpStmt : staticFieldStatements) {
                            litr.add(tmpStmt);
                        }
                    }
                }
            }
        }
    }
}
 
Example 6
Source File: BinaryExpressionTransformer.java    From groovy with Apache License 2.0 6 votes vote down vote up
private static Expression transformDeclarationExpression(final BinaryExpression bin) {
    Expression leftExpression = bin.getLeftExpression();
    if (leftExpression instanceof VariableExpression) {
        if (ClassHelper.char_TYPE.equals(((VariableExpression) leftExpression).getOriginType())) {
            Expression rightExpression = bin.getRightExpression();
            Character c = tryCharConstant(rightExpression);
            if (c != null) {
                Expression ce = constX(c, true);
                ce.setSourcePosition(rightExpression);
                bin.setRightExpression(ce);
                return bin;
            }
        }
    }
    return null;
}
 
Example 7
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
protected Expression transformBinaryExpression(BinaryExpression be) {
    int type = be.getOperation().getType();
    boolean oldInLeftExpression;
    Expression right = transform(be.getRightExpression());
    be.setRightExpression(right);
    Expression left;
    if (type == Types.EQUAL && be.getLeftExpression() instanceof VariableExpression) {
        oldInLeftExpression = inLeftExpression;
        inLeftExpression = true;
        left = transform(be.getLeftExpression());
        inLeftExpression = oldInLeftExpression;
        if (left instanceof StaticMethodCallExpression) {
            StaticMethodCallExpression smce = (StaticMethodCallExpression) left;
            StaticMethodCallExpression result = new StaticMethodCallExpression(smce.getOwnerType(), smce.getMethod(), right);
            setSourcePosition(result, be);
            return result;
        }
    } else {
        left = transform(be.getLeftExpression());
    }
    be.setLeftExpression(left);
    return be;
}
 
Example 8
Source File: SqlWhereVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void visitBinaryExpression(BinaryExpression expression) {
    Expression left = expression.getLeftExpression();
    Expression right = expression.getRightExpression();
    boolean leaf = (right instanceof ConstantExpression || left instanceof ConstantExpression);

    if (!leaf) buffer.append("(");
    left.visit(this);
    buffer.append(" ");

    Token token = expression.getOperation();
    buffer.append(tokenAsSql(token));

    buffer.append(" ");
    right.visit(this);
    if (!leaf) buffer.append(")");
}
 
Example 9
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 6 votes vote down vote up
private boolean doAssignmentToLocalVariable(final String method, final BinaryExpression binExp) {
    Expression left = binExp.getLeftExpression();
    if (left instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) left;
        Variable v = ve.getAccessedVariable();
        if (v instanceof DynamicVariable) return false;
        if (v instanceof PropertyExpression) return false;
        /* field and declaration we don't return false */
    } else {
        return false;
    }

    evaluateBinaryExpression(method, binExp);
    controller.getOperandStack().dup();
    controller.getCompileStack().pushLHS(true);
    binExp.getLeftExpression().visit(controller.getAcg());
    controller.getCompileStack().popLHS();

    return true;
}
 
Example 10
Source File: ValidationCodeVisitorSupport.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void visitBinaryExpression(final BinaryExpression expression) {
    if (expression.getOperation() != null && expression.getOperation().isA(Types.ASSIGNMENT_OPERATOR)) {
        final org.codehaus.groovy.ast.expr.Expression leftExpression = expression.getLeftExpression();
        if (dependenciesName.contains(leftExpression.getText())) {
            errorStatus.add(context.createFailureStatus(Messages.bind(
                    Messages.invalidDependencyAssignement, leftExpression.getText())));

        }
    }
    super.visitBinaryExpression(expression);
}
 
Example 11
Source File: GroovyClassFilterTransformer.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Expression transformBinaryExpression(String checkedOperation, BinaryExpression be)
{
	Expression leftExpression = be.getLeftExpression();
	Expression rightExpression = be.getRightExpression();
	if (allowed(leftExpression.getType()) && allowed(rightExpression.getType()))
	{
		Expression transformedLeft = transform(leftExpression);
		Expression transformedRight = transform(rightExpression);
		if (transformedLeft.equals(leftExpression) 
				&& transformedRight.equals(rightExpression))
		{
			if (log.isDebugEnabled())
			{
				log.debug("allowed binary expression " + be);
			}
			return be;
		}
		
		BinaryExpression transformedExpression = new BinaryExpression(
				transformedLeft, be.getOperation(), transformedRight);
		if (log.isDebugEnabled())
		{
			log.debug("transformed binary expression " + transformedExpression);
		}
		return transformedExpression;
	}
	
	return super.transformBinaryExpression(checkedOperation, be);
}
 
Example 12
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void evaluateCompareExpression(final MethodCaller compareMethod, final BinaryExpression expression) {
    ClassNode classNode = controller.getClassNode();
    Expression leftExp = expression.getLeftExpression();
    Expression rightExp = expression.getRightExpression();
    ClassNode leftType = controller.getTypeChooser().resolveType(leftExp, classNode);
    ClassNode rightType = controller.getTypeChooser().resolveType(rightExp, classNode);

    boolean done = false;
    if (ClassHelper.isPrimitiveType(leftType) && ClassHelper.isPrimitiveType(rightType)) {
        BinaryExpressionMultiTypeDispatcher helper = new BinaryExpressionMultiTypeDispatcher(controller);
        done = helper.doPrimitiveCompare(leftType, rightType, expression);
    }
    if (!done) {
        AsmClassGenerator acg = controller.getAcg();
        OperandStack operandStack = controller.getOperandStack();

        leftExp.visit(acg);
        operandStack.box();
        rightExp.visit(acg);
        operandStack.box();

        compareMethod.call(controller.getMethodVisitor());
        ClassNode resType = ClassHelper.boolean_TYPE;
        if (compareMethod == findRegexMethod) {
            resType = ClassHelper.OBJECT_TYPE;
        }
        operandStack.replace(resType, 2);
    }
}
 
Example 13
Source File: BinaryExpressionHelper.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void evaluateArrayAssignmentWithOperator(final String method, final BinaryExpression expression, final BinaryExpression leftBinExpr) {
    // e.g. x[a] += b
    // to avoid loading x and a twice we transform the expression to use
    // ExpressionAsVariableSlot
    // -> subscript=a, receiver=x, receiver[subscript]+b, =, receiver[subscript]
    // -> subscript=a, receiver=x, receiver#getAt(subscript)#plus(b), =, receiver#putAt(subscript)
    // -> subscript=a, receiver=x, receiver#putAt(subscript, receiver#getAt(subscript)#plus(b))
    // the result of x[a] += b is x[a]+b, thus:
    // -> subscript=a, receiver=x, receiver#putAt(subscript, ret=receiver#getAt(subscript)#plus(b)), ret
    ExpressionAsVariableSlot subscript = new ExpressionAsVariableSlot(controller, leftBinExpr.getRightExpression(), "subscript");
    ExpressionAsVariableSlot receiver  = new ExpressionAsVariableSlot(controller, leftBinExpr.getLeftExpression(), "receiver");
    MethodCallExpression getAt = callX(receiver, "getAt", args(subscript));
    MethodCallExpression operation = callX(getAt, method, expression.getRightExpression());
    ExpressionAsVariableSlot ret = new ExpressionAsVariableSlot(controller, operation, "ret");
    MethodCallExpression putAt = callX(receiver, "putAt", args(subscript, ret));

    AsmClassGenerator acg = controller.getAcg();
    putAt.visit(acg);
    OperandStack os = controller.getOperandStack();
    os.pop();
    os.load(ret.getType(), ret.getIndex());

    CompileStack compileStack = controller.getCompileStack();
    compileStack.removeVar(ret.getIndex());
    compileStack.removeVar(subscript.getIndex());
    compileStack.removeVar(receiver.getIndex());
}
 
Example 14
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected void evaluateBinaryExpressionWithAssignment(final String method, final BinaryExpression expression) {
    Expression leftExpression = expression.getLeftExpression();
    if (leftExpression instanceof PropertyExpression) {
        PropertyExpression pexp = (PropertyExpression) leftExpression;

        BinaryExpression expressionWithoutAssignment = binX(
                leftExpression,
                Token.newSymbol(
                        TokenUtil.removeAssignment(expression.getOperation().getType()),
                        expression.getOperation().getStartLine(),
                        expression.getOperation().getStartColumn()
                ),
                expression.getRightExpression()
        );
        expressionWithoutAssignment.copyNodeMetaData(expression);
        expressionWithoutAssignment.setSafe(expression.isSafe());
        expressionWithoutAssignment.setSourcePosition(expression);

        if (makeSetProperty(
                pexp.getObjectExpression(),
                pexp.getProperty(),
                expressionWithoutAssignment,
                pexp.isSafe(),
                pexp.isSpreadSafe(),
                pexp.isImplicitThis(),
                pexp instanceof AttributeExpression)) {
            return;
        }
    }
    super.evaluateBinaryExpressionWithAssignment(method, expression);
}
 
Example 15
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void evaluateEqual(final BinaryExpression expression, final boolean defineVariable) {
    Expression leftExpression = expression.getLeftExpression();
    if (!defineVariable) {
        if (leftExpression instanceof PropertyExpression) {
            PropertyExpression pexp = (PropertyExpression) leftExpression;
            if (makeSetProperty(
                    pexp.getObjectExpression(),
                    pexp.getProperty(),
                    expression.getRightExpression(),
                    pexp.isSafe(),
                    pexp.isSpreadSafe(),
                    pexp.isImplicitThis(),
                    pexp instanceof AttributeExpression)) {
                return;
            }
        }
    } else {
        Expression rightExpression = expression.getRightExpression();
        if (rightExpression instanceof LambdaExpression || rightExpression instanceof MethodReferenceExpression) {
            rightExpression.putNodeMetaData(INFERRED_FUNCTIONAL_INTERFACE_TYPE, leftExpression.getNodeMetaData(INFERRED_TYPE));
        }
    }
    // GROOVY-5620: spread-safe operator on LHS is not supported
    if (leftExpression instanceof PropertyExpression
            && ((PropertyExpression) leftExpression).isSpreadSafe()
            && StaticTypeCheckingSupport.isAssignment(expression.getOperation().getType())) {
        // rewrite it so that it can be statically compiled
        transformSpreadOnLHS(expression);
        return;
    }
    super.evaluateEqual(expression, defineVariable);
}
 
Example 16
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 5 votes vote down vote up
/** Handles a groovy BinaryExpression such as foo = true, or bar.baz.foo = true. */
@Override
public void visitBinaryExpression(BinaryExpression binaryExpression) {
  if (!methodCallStack.isEmpty()) {
    MethodCallExpression call = Iterables.getLast(methodCallStack);
    String parent = call.getMethodAsString();
    String parentParent = getParentParent();
    Expression leftExpression = binaryExpression.getLeftExpression();
    Expression rightExpression = binaryExpression.getRightExpression();
    if (rightExpression instanceof ConstantExpression
        && (leftExpression instanceof PropertyExpression
            || leftExpression instanceof VariableExpression)) {
      String value = rightExpression.getText();
      String property = "";
      if (leftExpression instanceof PropertyExpression) {
        Expression leftPropertyExpression = ((PropertyExpression) leftExpression).getProperty();
        if (!(leftPropertyExpression instanceof ConstantExpression)) {
          return;
        }
        property = ((ConstantExpression) leftPropertyExpression).getText();
        Expression leftObjectExpression =
            ((PropertyExpression) leftExpression).getObjectExpression();
        parentParent = parent;
        parent = getValidParentString(leftObjectExpression);
        if (leftObjectExpression instanceof PropertyExpression) {
          parentParent =
              getValidParentString(
                  ((PropertyExpression) leftObjectExpression).getObjectExpression());
        }
      } else {
        property = ((VariableExpression) leftExpression).getName();
      }
      checkDslPropertyAssignment(
          property, value, parent, parentParent, binaryExpression.getLineNumber());
    }
  }
  super.visitBinaryExpression(binaryExpression);
}
 
Example 17
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
protected void evaluateCompareExpression(final MethodCaller compareMethod, final BinaryExpression binExp) {
    ClassNode current =  controller.getClassNode();
    TypeChooser typeChooser = controller.getTypeChooser();

    Expression leftExp = binExp.getLeftExpression();
    ClassNode leftType = typeChooser.resolveType(leftExp, current);
    Expression rightExp = binExp.getRightExpression();
    ClassNode rightType = typeChooser.resolveType(rightExp, current);

    if (!doPrimitiveCompare(leftType, rightType, binExp)) {
        super.evaluateCompareExpression(compareMethod, binExp);
    }
}
 
Example 18
Source File: SuperCallTraitTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
private Expression transformBinaryExpression(final BinaryExpression exp) {
    Expression trn = super.transform(exp);
    if (trn instanceof BinaryExpression) {
        BinaryExpression bin = (BinaryExpression) trn;
        Expression leftExpression = bin.getLeftExpression();
        if (bin.getOperation().getType() == Types.EQUAL && leftExpression instanceof PropertyExpression) {
            ClassNode traitReceiver = null;
            PropertyExpression leftPropertyExpression = (PropertyExpression) leftExpression;
            if (isTraitSuperPropertyExpression(leftPropertyExpression.getObjectExpression())) {
                PropertyExpression pexp = (PropertyExpression) leftPropertyExpression.getObjectExpression();
                traitReceiver = pexp.getObjectExpression().getType();
            }
            if (traitReceiver!=null) {
                // A.super.foo = ...
                TraitHelpersTuple helpers = Traits.findHelpers(traitReceiver);
                ClassNode helper = helpers.getHelper();
                String setterName = MetaProperty.getSetterName(leftPropertyExpression.getPropertyAsString());
                List<MethodNode> methods = helper.getMethods(setterName);
                for (MethodNode method : methods) {
                    Parameter[] parameters = method.getParameters();
                    if (parameters.length==2 && parameters[0].getType().equals(traitReceiver)) {
                        ArgumentListExpression args = new ArgumentListExpression(
                                new VariableExpression("this"),
                                transform(exp.getRightExpression())
                        );
                        MethodCallExpression setterCall = new MethodCallExpression(
                                new ClassExpression(helper),
                                setterName,
                                args
                        );
                        setterCall.setMethodTarget(method);
                        setterCall.setImplicitThis(false);
                        return setterCall;
                    }
                }
                return bin;
            }
        }
    }
    return trn;
}
 
Example 19
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void transformSpreadOnLHS(final BinaryExpression expression) {
    PropertyExpression spreadExpression = (PropertyExpression) expression.getLeftExpression();
    Expression receiver = spreadExpression.getObjectExpression();

    int counter = labelCounter.incrementAndGet();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();

    // create an empty arraylist
    VariableExpression result = varX(this.getClass().getSimpleName() + "$spreadresult" + counter, ARRAYLIST_CLASSNODE);
    ConstructorCallExpression newArrayList = ctorX(ARRAYLIST_CLASSNODE);
    newArrayList.setNodeMetaData(DIRECT_METHOD_CALL_TARGET, ARRAYLIST_CONSTRUCTOR);
    Expression decl = declX(result, newArrayList);
    decl.visit(controller.getAcg());
    // if (receiver != null)
    receiver.visit(controller.getAcg());
    Label ifnull = compileStack.createLocalLabel("ifnull_" + counter);
    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitJumpInsn(IFNULL, ifnull);
    operandStack.remove(1); // receiver consumed by if()
    Label nonull = compileStack.createLocalLabel("nonull_" + counter);
    mv.visitLabel(nonull);
    ClassNode componentType = StaticTypeCheckingVisitor.inferLoopElementType(
            controller.getTypeChooser().resolveType(receiver, controller.getClassNode()));
    Parameter iterator = new Parameter(componentType, "for$it$" + counter);
    VariableExpression iteratorAsVar = varX(iterator);
    PropertyExpression pexp = spreadExpression instanceof AttributeExpression
        ? new AttributeExpression(iteratorAsVar, spreadExpression.getProperty(), true)
        : new PropertyExpression(iteratorAsVar, spreadExpression.getProperty(), true);
    pexp.setImplicitThis(spreadExpression.isImplicitThis());
    pexp.setSourcePosition(spreadExpression);
    BinaryExpression assignment = binX(pexp, expression.getOperation(), expression.getRightExpression());
    MethodCallExpression add = callX(result, "add", assignment);
    add.setMethodTarget(ARRAYLIST_ADD_METHOD);
    // for (e in receiver) { result.add(e?.method(arguments) }
    ForStatement stmt = new ForStatement(
            iterator,
            receiver,
            stmt(add)
    );
    stmt.visit(controller.getAcg());
    // else { empty list }
    mv.visitLabel(ifnull);
    // end of if/else
    // return result list
    result.visit(controller.getAcg());
}
 
Example 20
Source File: BinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 4 votes vote down vote up
private static boolean isAssignmentToArray(final BinaryExpression binExp) {
    Expression leftExpression = binExp.getLeftExpression();
    if (!(leftExpression instanceof BinaryExpression)) return false;
    BinaryExpression leftBinExpr = (BinaryExpression) leftExpression;
    return leftBinExpr.getOperation().getType() == LEFT_SQUARE_BRACKET;
}