Search in sources :

Example 31 with VariableExpression

use of org.codehaus.groovy.ast.expr.VariableExpression in project grails-core by grails.

the class DefaultASTValidateableHelper method addGetConstraintsMethod.

protected void addGetConstraintsMethod(final ClassNode classNode, boolean defaultNullable) {
    final String getConstraintsMethodName = "getConstraints";
    MethodNode getConstraintsMethod = classNode.getMethod(getConstraintsMethodName, ZERO_PARAMETERS);
    if (getConstraintsMethod == null || !getConstraintsMethod.getDeclaringClass().equals(classNode)) {
        final BooleanExpression isConstraintsPropertyNull = new BooleanExpression(new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.COMPARE_EQUAL, 0, 0), new ConstantExpression(null)));
        final BlockStatement ifConstraintsPropertyIsNullBlockStatement = new BlockStatement();
        final ArgumentListExpression getConstrainedPropertiesForClassArguments = new ArgumentListExpression();
        getConstrainedPropertiesForClassArguments.addExpression(new VariableExpression("this"));
        getConstrainedPropertiesForClassArguments.addExpression(new ConstantExpression(defaultNullable));
        final Expression getConstraintsMethodCall = new StaticMethodCallExpression(ClassHelper.make(ValidationSupport.class), "getConstrainedPropertiesForClass", getConstrainedPropertiesForClassArguments);
        final Expression initializeConstraintsFieldExpression = new BinaryExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), Token.newSymbol(Types.EQUALS, 0, 0), getConstraintsMethodCall);
        final Statement ifConstraintsPropertyIsNullStatement = new IfStatement(isConstraintsPropertyNull, ifConstraintsPropertyIsNullBlockStatement, new ExpressionStatement(new EmptyExpression()));
        ifConstraintsPropertyIsNullBlockStatement.addStatement(new ExpressionStatement(initializeConstraintsFieldExpression));
        if (!defaultNullable) {
            final Map<String, ClassNode> propertiesToConstrain = getPropertiesToEnsureConstraintsFor(classNode);
            for (final Map.Entry<String, ClassNode> entry : propertiesToConstrain.entrySet()) {
                final String propertyName = entry.getKey();
                final ClassNode propertyType = entry.getValue();
                final String cpName = "$" + propertyName + "$constrainedProperty";
                final ArgumentListExpression constrainedPropertyConstructorArgumentList = new ArgumentListExpression();
                constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(classNode));
                constrainedPropertyConstructorArgumentList.addExpression(new ConstantExpression(propertyName));
                constrainedPropertyConstructorArgumentList.addExpression(new ClassExpression(propertyType));
                final ConstructorCallExpression constrainedPropertyCtorCallExpression = new ConstructorCallExpression(new ClassNode(ConstrainedProperty.class), constrainedPropertyConstructorArgumentList);
                final Expression declareConstrainedPropertyExpression = new DeclarationExpression(new VariableExpression(cpName, ClassHelper.OBJECT_TYPE), Token.newSymbol(Types.EQUALS, 0, 0), constrainedPropertyCtorCallExpression);
                final ArgumentListExpression applyConstraintMethodArgumentList = new ArgumentListExpression();
                applyConstraintMethodArgumentList.addExpression(new ConstantExpression(ConstrainedProperty.NULLABLE_CONSTRAINT));
                applyConstraintMethodArgumentList.addExpression(new ConstantExpression(defaultNullable));
                final Expression applyNullableConstraintMethodCallExpression = new MethodCallExpression(new VariableExpression(cpName), "applyConstraint", applyConstraintMethodArgumentList);
                final ArgumentListExpression putMethodArgumentList = new ArgumentListExpression();
                putMethodArgumentList.addExpression(new ConstantExpression(propertyName));
                putMethodArgumentList.addExpression(new VariableExpression(cpName));
                final MethodCallExpression addToConstraintsMapExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME), "put", putMethodArgumentList);
                final BlockStatement addNullableConstraintBlock = new BlockStatement();
                addNullableConstraintBlock.addStatement(new ExpressionStatement(declareConstrainedPropertyExpression));
                addNullableConstraintBlock.addStatement(new ExpressionStatement(applyNullableConstraintMethodCallExpression));
                addNullableConstraintBlock.addStatement(new ExpressionStatement(addToConstraintsMapExpression));
                final Expression constraintsMapContainsKeyExpression = new MethodCallExpression(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME, ClassHelper.make(Map.class)), "containsKey", new ArgumentListExpression(new ConstantExpression(propertyName)));
                final BooleanExpression ifPropertyIsAlreadyConstrainedExpression = new BooleanExpression(constraintsMapContainsKeyExpression);
                final Statement ifPropertyIsAlreadyConstrainedStatement = new IfStatement(ifPropertyIsAlreadyConstrainedExpression, new ExpressionStatement(new EmptyExpression()), addNullableConstraintBlock);
                ifConstraintsPropertyIsNullBlockStatement.addStatement(ifPropertyIsAlreadyConstrainedStatement);
            }
        }
        final BlockStatement methodBlockStatement = new BlockStatement();
        methodBlockStatement.addStatement(ifConstraintsPropertyIsNullStatement);
        final Statement returnStatement = new ReturnStatement(new VariableExpression(CONSTRAINED_PROPERTIES_PROPERTY_NAME));
        methodBlockStatement.addStatement(returnStatement);
        final MethodNode methodNode = new MethodNode(getConstraintsMethodName, Modifier.STATIC | Modifier.PUBLIC, new ClassNode(Map.class), ZERO_PARAMETERS, null, methodBlockStatement);
        if (classNode.redirect() == null) {
            classNode.addMethod(methodNode);
        } else {
            classNode.redirect().addMethod(methodNode);
        }
    }
}
Also used : ValidationSupport(org.grails.web.plugins.support.ValidationSupport) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ClassNode(org.codehaus.groovy.ast.ClassNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) 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) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 32 with VariableExpression

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

the class ModuleNode method createStatementsClass.

protected ClassNode createStatementsClass() {
    ClassNode classNode = getScriptClassDummy();
    if (classNode.getName().endsWith("package-info")) {
        return classNode;
    }
    handleMainMethodIfPresent(methods);
    // return new Foo(new ShellContext(args)).run()
    classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
    MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
    methodNode.setIsScriptBody();
    classNode.addMethod(methodNode);
    classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
    Statement stmt;
    // (like @BaseScript) that could change this.  But this is cautious and anticipates possible compiler changes.
    if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
        stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
    } else {
        // Fallback for non-standard base "script" classes with no context (Binding) constructor.
        stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
    }
    classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
    for (MethodNode node : methods) {
        int modifiers = node.getModifiers();
        if ((modifiers & ACC_ABSTRACT) != 0) {
            throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
        }
        // br: the old logic seems to add static to all def f().... in a script, which makes enclosing
        // inner classes (including closures) in a def function difficult. Comment it out.
        node.setModifiers(modifiers);
        classNode.addMethod(node);
    }
    return classNode;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 33 with VariableExpression

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

the class MethodCallExpressionTransformer method transformMethodCallExpression.

Expression transformMethodCallExpression(final MethodCallExpression expr) {
    Expression trn = tryTransformIsToCompareIdentity(expr);
    if (trn != null) {
        return trn;
    }
    ClassNode superCallReceiver = expr.getNodeMetaData(StaticTypesMarker.SUPER_MOP_METHOD_REQUIRED);
    if (superCallReceiver != null) {
        return transformMethodCallExpression(transformToMopSuperCall(superCallReceiver, expr));
    }
    Expression objectExpression = expr.getObjectExpression();
    ClassNode type = staticCompilationTransformer.getTypeChooser().resolveType(objectExpression, staticCompilationTransformer.getClassNode());
    if (isCallOnClosure(expr)) {
        FieldNode field = staticCompilationTransformer.getClassNode().getField(expr.getMethodAsString());
        if (field != null) {
            VariableExpression vexp = new VariableExpression(field);
            MethodCallExpression result = new MethodCallExpression(vexp, "call", staticCompilationTransformer.transform(expr.getArguments()));
            result.setImplicitThis(false);
            result.setSourcePosition(expr);
            result.setSafe(expr.isSafe());
            result.setSpreadSafe(expr.isSpreadSafe());
            result.setMethodTarget(StaticTypeCheckingVisitor.CLOSURE_CALL_VARGS);
            return result;
        }
    }
    if (type != null && type.isArray()) {
        String method = expr.getMethodAsString();
        ClassNode componentType = type.getComponentType();
        if ("getAt".equals(method)) {
            Expression arguments = expr.getArguments();
            if (arguments instanceof TupleExpression) {
                List<Expression> argList = ((TupleExpression) arguments).getExpressions();
                if (argList.size() == 1) {
                    Expression indexExpr = argList.get(0);
                    ClassNode argType = staticCompilationTransformer.getTypeChooser().resolveType(indexExpr, staticCompilationTransformer.getClassNode());
                    ClassNode indexType = ClassHelper.getWrapper(argType);
                    if (componentType.isEnum() && ClassHelper.Number_TYPE == indexType) {
                        // workaround for generated code in enums which use .next() returning a Number
                        indexType = ClassHelper.Integer_TYPE;
                    }
                    if (argType != null && ClassHelper.Integer_TYPE == indexType) {
                        BinaryExpression binaryExpression = new BinaryExpression(objectExpression, Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()), indexExpr);
                        binaryExpression.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
                        return staticCompilationTransformer.transform(binaryExpression);
                    }
                }
            }
        } else if ("putAt".equals(method)) {
            Expression arguments = expr.getArguments();
            if (arguments instanceof TupleExpression) {
                List<Expression> argList = ((TupleExpression) arguments).getExpressions();
                if (argList.size() == 2) {
                    Expression indexExpr = argList.get(0);
                    Expression objExpr = argList.get(1);
                    ClassNode argType = staticCompilationTransformer.getTypeChooser().resolveType(indexExpr, staticCompilationTransformer.getClassNode());
                    if (argType != null && ClassHelper.Integer_TYPE == ClassHelper.getWrapper(argType)) {
                        BinaryExpression arrayGet = new BinaryExpression(objectExpression, Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()), indexExpr);
                        arrayGet.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
                        BinaryExpression assignment = new BinaryExpression(arrayGet, Token.newSymbol("=", objExpr.getLineNumber(), objExpr.getColumnNumber()), objExpr);
                        return staticCompilationTransformer.transform(assignment);
                    }
                }
            }
        }
    }
    return staticCompilationTransformer.superTransform(expr);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) List(java.util.List)

Example 34 with VariableExpression

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

the class VariableExpressionTransformer method tryTransformDelegateToProperty.

private Expression tryTransformDelegateToProperty(VariableExpression expr) {
    // we need to transform variable expressions that go to a delegate
    // to a property expression, as ACG would loose the information
    // in processClassVariable before it reaches any makeCall, that could
    // handle it
    Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (val == null)
        return null;
    VariableExpression implicitThis = new VariableExpression("this");
    PropertyExpression pexp = new PropertyExpression(implicitThis, expr.getName());
    pexp.copyNodeMetaData(expr);
    pexp.setImplicitThis(true);
    ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
    if (owner != null) {
        implicitThis.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner);
        implicitThis.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val);
    }
    return pexp;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 35 with VariableExpression

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

the class ToStringASTTransformation method appendValue.

private static void appendValue(BlockStatement body, Expression result, VariableExpression first, Expression value, String name, boolean includeNames, boolean ignoreNulls) {
    final BlockStatement thenBlock = new BlockStatement();
    final Statement appendValue = ignoreNulls ? ifS(notNullX(value), thenBlock) : thenBlock;
    appendCommaIfNotFirst(thenBlock, result, first);
    appendPrefix(thenBlock, result, name, includeNames);
    thenBlock.addStatement(ifElseS(sameX(value, new VariableExpression("this")), appendS(result, constX("(this)")), appendS(result, callX(INVOKER_TYPE, "toString", value))));
    body.addStatement(appendValue);
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) 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