Search in sources :

Example 66 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.

the class TraitReceiverTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
    Expression leftExpression = exp.getLeftExpression();
    Expression rightExpression = exp.getRightExpression();
    Token operation = exp.getOperation();
    if (operation.getText().equals("=")) {
        String leftFieldName = null;
        // it's an assignment
        if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
            leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
        } else if (leftExpression instanceof FieldExpression) {
            leftFieldName = ((FieldExpression) leftExpression).getFieldName();
        } else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
            leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
            FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
            if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
                return createAssignmentToField(rightExpression, operation, leftFieldName);
            }
        }
        if (leftFieldName != null) {
            FieldNode fn = weavedType.getDeclaredField(leftFieldName);
            FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
            if (fn == null) {
                fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
            }
            Expression receiver = createFieldHelperReceiver();
            boolean isStatic = staticField != null && staticField.isStatic();
            if (fn.isStatic()) {
                // DO NOT USE isStatic variable here!
                receiver = new PropertyExpression(receiver, "class");
            }
            String method = Traits.helperSetterName(fn);
            MethodCallExpression mce = new MethodCallExpression(receiver, method, new ArgumentListExpression(super.transform(rightExpression)));
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            markDynamicCall(mce, staticField, isStatic);
            return mce;
        }
    }
    Expression leftTransform = transform(leftExpression);
    Expression rightTransform = transform(rightExpression);
    Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : new BinaryExpression(leftTransform, operation, rightTransform);
    ret.setSourcePosition(exp);
    ret.copyNodeMetaData(exp);
    return ret;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Token(org.codehaus.groovy.syntax.Token) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 67 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.

the class StaticImportVisitor method transformMethodCallExpression.

protected Expression transformMethodCallExpression(MethodCallExpression mce) {
    Expression args = transform(mce.getArguments());
    Expression method = transform(mce.getMethod());
    Expression object = transform(mce.getObjectExpression());
    boolean isExplicitThisOrSuper = false;
    boolean isExplicitSuper = false;
    if (object instanceof VariableExpression) {
        VariableExpression ve = (VariableExpression) object;
        isExplicitThisOrSuper = !mce.isImplicitThis() && (ve.isThisExpression() || ve.isSuperExpression());
        isExplicitSuper = ve.isSuperExpression();
    }
    if (mce.isImplicitThis() || isExplicitThisOrSuper) {
        if (mce.isImplicitThis()) {
            Expression ret = findStaticMethodImportFromModule(method, args);
            if (ret != null) {
                setSourcePosition(ret, mce);
                return ret;
            }
            if (method instanceof ConstantExpression && !inLeftExpression) {
                // could be a closure field
                String methodName = (String) ((ConstantExpression) method).getValue();
                ret = findStaticFieldOrPropAccessorImportFromModule(methodName);
                if (ret != null) {
                    ret = new MethodCallExpression(ret, "call", args);
                    setSourcePosition(ret, mce);
                    return ret;
                }
            }
        } else if (currentMethod != null && currentMethod.isStatic() && isExplicitSuper) {
            MethodCallExpression ret = new MethodCallExpression(new ClassExpression(currentClass.getSuperClass()), method, args);
            setSourcePosition(ret, mce);
            return ret;
        }
        if (method instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) method;
            Object value = ce.getValue();
            if (value instanceof String) {
                boolean foundInstanceMethod = false;
                String methodName = (String) value;
                boolean inInnerClass = isInnerClass(currentClass);
                if (currentMethod != null && !currentMethod.isStatic()) {
                    if (currentClass.hasPossibleMethod(methodName, args)) {
                        foundInstanceMethod = true;
                    }
                }
                boolean lookForPossibleStaticMethod = !methodName.equals("call");
                lookForPossibleStaticMethod &= !foundInstanceMethod;
                lookForPossibleStaticMethod |= inSpecialConstructorCall;
                lookForPossibleStaticMethod &= !inInnerClass;
                if (!inClosure && lookForPossibleStaticMethod && (hasPossibleStaticMethod(currentClass, methodName, args, true)) || hasPossibleStaticProperty(currentClass, methodName)) {
                    StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass, methodName, args);
                    setSourcePosition(smce, mce);
                    return smce;
                }
                if (!inClosure && inInnerClass && inSpecialConstructorCall && mce.isImplicitThis() && !foundInstanceMethod) {
                    if (currentClass.getOuterClass().hasPossibleMethod(methodName, args)) {
                        object = new PropertyExpression(new ClassExpression(currentClass.getOuterClass()), new ConstantExpression("this"));
                    } else if (hasPossibleStaticMethod(currentClass.getOuterClass(), methodName, args, true) || hasPossibleStaticProperty(currentClass.getOuterClass(), methodName)) {
                        StaticMethodCallExpression smce = new StaticMethodCallExpression(currentClass.getOuterClass(), methodName, args);
                        setSourcePosition(smce, mce);
                        return smce;
                    }
                }
            }
        }
    }
    MethodCallExpression result = new MethodCallExpression(object, method, args);
    result.setSafe(mce.isSafe());
    result.setImplicitThis(mce.isImplicitThis());
    result.setSpreadSafe(mce.isSpreadSafe());
    result.setMethodTarget(mce.getMethodTarget());
    // GROOVY-6757
    result.setGenericsTypes(mce.getGenericsTypes());
    setSourcePosition(result, mce);
    return result;
}
Also used : StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 68 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.

the class BaseScriptASTTransformation method changeBaseScriptTypeFromDeclaration.

private void changeBaseScriptTypeFromDeclaration(final DeclarationExpression de, final AnnotationNode node) {
    if (de.isMultipleAssignmentDeclaration()) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", de);
        return;
    }
    if (!(de.getRightExpression() instanceof EmptyExpression)) {
        addError("Annotation " + MY_TYPE_NAME + " not supported with variable assignment.", de);
        return;
    }
    Expression value = node.getMember("value");
    if (value != null) {
        addError("Annotation " + MY_TYPE_NAME + " cannot have member 'value' if used on a declaration.", value);
        return;
    }
    ClassNode cNode = de.getDeclaringClass();
    ClassNode baseScriptType = de.getVariableExpression().getType().getPlainNodeReference();
    de.setRightExpression(new VariableExpression("this"));
    changeBaseScriptType(de, cNode, baseScriptType);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression)

Example 69 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.

the class CategoryASTTransformation method createThisExpression.

private static VariableExpression createThisExpression() {
    VariableExpression expr = new VariableExpression("$this");
    expr.setClosureSharedVariable(true);
    return expr;
}
Also used : VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 70 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project groovy by apache.

the class EqualsAndHashCodeASTTransformation method createCanEqual.

private static void createCanEqual(ClassNode cNode) {
    boolean hasExistingCanEqual = hasDeclaredMethod(cNode, "canEqual", 1);
    if (hasExistingCanEqual && hasDeclaredMethod(cNode, "_canEqual", 1))
        return;
    final BlockStatement body = new BlockStatement();
    VariableExpression other = varX("other");
    body.addStatement(returnS(isInstanceOfX(other, GenericsUtils.nonGeneric(cNode))));
    cNode.addMethod(new MethodNode(hasExistingCanEqual ? "_canEqual" : "canEqual", hasExistingCanEqual ? ACC_PRIVATE : ACC_PUBLIC, ClassHelper.boolean_TYPE, params(param(OBJECT_TYPE, other.getName())), ClassNode.EMPTY_ARRAY, body));
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Aggregations

VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)176 Expression (org.codehaus.groovy.ast.expr.Expression)86 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)77 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)72 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)67 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)61 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)58 ClassNode (org.codehaus.groovy.ast.ClassNode)52 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)52 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)49 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)47 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)47 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)45 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)41 Parameter (org.codehaus.groovy.ast.Parameter)40 MethodNode (org.codehaus.groovy.ast.MethodNode)37 FieldNode (org.codehaus.groovy.ast.FieldNode)36 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)35 Statement (org.codehaus.groovy.ast.stmt.Statement)33