Search in sources :

Example 41 with ClosureExpression

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

the class StaticImportVisitor method transform.

public Expression transform(Expression exp) {
    if (exp == null)
        return null;
    if (exp.getClass() == VariableExpression.class) {
        return transformVariableExpression((VariableExpression) exp);
    }
    if (exp.getClass() == BinaryExpression.class) {
        return transformBinaryExpression((BinaryExpression) exp);
    }
    if (exp.getClass() == PropertyExpression.class) {
        return transformPropertyExpression((PropertyExpression) exp);
    }
    if (exp.getClass() == MethodCallExpression.class) {
        return transformMethodCallExpression((MethodCallExpression) exp);
    }
    if (exp.getClass() == ClosureExpression.class) {
        return transformClosureExpression((ClosureExpression) exp);
    }
    if (exp.getClass() == ConstructorCallExpression.class) {
        return transformConstructorCallExpression((ConstructorCallExpression) exp);
    }
    if (exp.getClass() == ArgumentListExpression.class) {
        Expression result = exp.transformExpression(this);
        if (inPropertyExpression) {
            foundArgs = result;
        }
        return result;
    }
    if (exp instanceof ConstantExpression) {
        Expression result = exp.transformExpression(this);
        if (inPropertyExpression) {
            foundConstant = result;
        }
        if (inAnnotation && exp instanceof AnnotationConstantExpression) {
            ConstantExpression ce = (ConstantExpression) result;
            if (ce.getValue() instanceof AnnotationNode) {
                // replicate a little bit of AnnotationVisitor here
                // because we can't wait until later to do this
                AnnotationNode an = (AnnotationNode) ce.getValue();
                Map<String, Expression> attributes = an.getMembers();
                for (Map.Entry<String, Expression> entry : attributes.entrySet()) {
                    Expression attrExpr = transform(entry.getValue());
                    entry.setValue(attrExpr);
                }
            }
        }
        return result;
    }
    return exp.transformExpression(this);
}
Also used : 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) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) Map(java.util.Map)

Example 42 with ClosureExpression

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

the class JavaStubGenerator method getAnnotationValue.

private String getAnnotationValue(Object memberValue) {
    String val = "null";
    if (memberValue instanceof ListExpression) {
        StringBuilder sb = new StringBuilder("{");
        boolean first = true;
        ListExpression le = (ListExpression) memberValue;
        for (Expression e : le.getExpressions()) {
            if (first)
                first = false;
            else
                sb.append(",");
            sb.append(getAnnotationValue(e));
        }
        sb.append("}");
        val = sb.toString();
    } else if (memberValue instanceof ConstantExpression) {
        ConstantExpression ce = (ConstantExpression) memberValue;
        Object constValue = ce.getValue();
        if (constValue instanceof AnnotationNode) {
            StringWriter writer = new StringWriter();
            PrintWriter out = new PrintWriter(writer);
            printAnnotation(out, (AnnotationNode) constValue);
            val = writer.toString();
        } else if (constValue instanceof Number || constValue instanceof Boolean)
            val = constValue.toString();
        else
            val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
    } else if (memberValue instanceof PropertyExpression) {
        // assume must be static class field or enum value or class that Java can resolve
        val = ((Expression) memberValue).getText();
    } else if (memberValue instanceof VariableExpression) {
        val = ((Expression) memberValue).getText();
        //check for an alias
        ImportNode alias = currentModule.getStaticImports().get(val);
        if (alias != null)
            val = alias.getClassName() + "." + alias.getFieldName();
    } else if (memberValue instanceof ClosureExpression) {
        // annotation closure; replaced with this specific class literal to cover the
        // case where annotation type uses Class<? extends Closure> for the closure's type
        val = "groovy.lang.Closure.class";
    } else if (memberValue instanceof ClassExpression) {
        val = ((Expression) memberValue).getText() + ".class";
    }
    return val;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) StringWriter(java.io.StringWriter) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) PrintWriter(java.io.PrintWriter)

Example 43 with ClosureExpression

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

the class TraitReceiverTransformer method transform.

@Override
public Expression transform(final Expression exp) {
    ClassNode weavedType = weaved.getOriginType();
    if (exp instanceof BinaryExpression) {
        return transformBinaryExpression((BinaryExpression) exp, weavedType);
    } else if (exp instanceof StaticMethodCallExpression) {
        StaticMethodCallExpression call = (StaticMethodCallExpression) exp;
        ClassNode ownerType = call.getOwnerType();
        if (traitClass.equals(ownerType)) {
            MethodCallExpression result = new MethodCallExpression(new VariableExpression(weaved), call.getMethod(), transform(call.getArguments()));
            result.setSafe(false);
            result.setImplicitThis(false);
            result.setSpreadSafe(false);
            result.setSourcePosition(call);
            return result;
        }
    } else if (exp instanceof MethodCallExpression) {
        MethodCallExpression call = (MethodCallExpression) exp;
        Expression obj = call.getObjectExpression();
        if (call.isImplicitThis() || "this".equals(obj.getText())) {
            return transformMethodCallOnThis(call);
        } else if ("super".equals(obj.getText())) {
            return transformSuperMethodCall(call);
        }
    } else if (exp instanceof FieldExpression) {
        return transformFieldExpression((FieldExpression) exp);
    } else if (exp instanceof VariableExpression) {
        VariableExpression vexp = (VariableExpression) exp;
        Variable accessedVariable = vexp.getAccessedVariable();
        if (accessedVariable instanceof FieldNode) {
            FieldNode fn = (FieldNode) accessedVariable;
            Expression receiver = createFieldHelperReceiver();
            MethodCallExpression mce;
            boolean isStatic = fn.isStatic();
            if (isStatic) {
                receiver = createStaticReceiver(receiver);
            }
            mce = new MethodCallExpression(receiver, Traits.helperGetterName(fn), ArgumentListExpression.EMPTY_ARGUMENTS);
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            markDynamicCall(mce, fn, isStatic);
            return mce;
        } else if (accessedVariable instanceof PropertyNode) {
            String propName = accessedVariable.getName();
            if (knownFields.contains(propName)) {
                return createFieldHelperCall(exp, weavedType, propName);
            } else {
                return new PropertyExpression(new VariableExpression(weaved), accessedVariable.getName());
            }
        } else if (accessedVariable instanceof DynamicVariable) {
            return new PropertyExpression(new VariableExpression(weaved), accessedVariable.getName());
        }
        if (vexp.isThisExpression()) {
            VariableExpression res = new VariableExpression(weaved);
            res.setSourcePosition(exp);
            return res;
        }
        if (vexp.isSuperExpression()) {
            throwSuperError(vexp);
        }
    } else if (exp instanceof PropertyExpression) {
        PropertyExpression pexp = (PropertyExpression) exp;
        Expression object = pexp.getObjectExpression();
        if (pexp.isImplicitThis() || "this".equals(object.getText())) {
            String propName = pexp.getPropertyAsString();
            if (knownFields.contains(propName)) {
                return createFieldHelperCall(exp, weavedType, propName);
            }
        }
    } else if (exp instanceof ClosureExpression) {
        MethodCallExpression mce = new MethodCallExpression(exp, "rehydrate", new ArgumentListExpression(new VariableExpression(weaved), new VariableExpression(weaved), new VariableExpression(weaved)));
        mce.setImplicitThis(false);
        mce.setSourcePosition(exp);
        boolean oldInClosure = inClosure;
        inClosure = true;
        ((ClosureExpression) exp).getCode().visit(this);
        inClosure = oldInClosure;
        // The rewrite we do is causing some troubles with type checking, which will
        // not be able to perform closure parameter type inference
        // so we store the replacement, which will be done *after* type checking.
        exp.putNodeMetaData(TraitASTTransformation.POST_TYPECHECKING_REPLACEMENT, mce);
        return exp;
    }
    // todo: unary expressions (field++, field+=, ...)
    return super.transform(exp);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Variable(org.codehaus.groovy.ast.Variable) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) PropertyNode(org.codehaus.groovy.ast.PropertyNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Example 44 with ClosureExpression

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

the class MapConstructorASTTransformation method createConstructor.

public static void createConstructor(ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperProperties, boolean useSetters, List<String> excludes, List<String> includes, ClosureExpression pre, ClosureExpression post, SourceUnit source, boolean allNames) {
    List<ConstructorNode> constructors = cNode.getDeclaredConstructors();
    boolean foundEmpty = constructors.size() == 1 && constructors.get(0).getFirstStatement() == null;
    // HACK: JavaStubGenerator could have snuck in a constructor we don't want
    if (foundEmpty)
        constructors.remove(0);
    List<FieldNode> superList = new ArrayList<FieldNode>();
    if (includeSuperProperties) {
        superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
    }
    List<FieldNode> list = new ArrayList<FieldNode>();
    if (includeProperties) {
        list.addAll(getInstancePropertyFields(cNode));
    }
    if (includeFields) {
        list.addAll(getInstanceNonPropertyFields(cNode));
    }
    Parameter map = param(MAP_TYPE, "args");
    final BlockStatement body = new BlockStatement();
    ClassCodeExpressionTransformer transformer = makeMapTypedArgsTransformer();
    if (pre != null) {
        ClosureExpression transformed = (ClosureExpression) transformer.transform(pre);
        copyStatementsWithSuperAdjustment(transformed, body);
    }
    final BlockStatement inner = new BlockStatement();
    for (FieldNode fNode : superList) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes, allNames))
            continue;
        assignField(useSetters, map, inner, name);
    }
    for (FieldNode fNode : list) {
        String name = fNode.getName();
        if (shouldSkip(name, excludes, includes, allNames))
            continue;
        assignField(useSetters, map, inner, name);
    }
    body.addStatement(ifS(notNullX(varX("args")), inner));
    if (post != null) {
        ClosureExpression transformed = (ClosureExpression) transformer.transform(post);
        body.addStatement(transformed.getCode());
    }
    cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params(map), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Example 45 with ClosureExpression

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

the class MemoizedASTTransformation method buildMemoizeClosureCallExpression.

private static MethodCallExpression buildMemoizeClosureCallExpression(MethodNode privateMethod, int protectedCacheSize, int maxCacheSize) {
    Parameter[] srcParams = privateMethod.getParameters();
    Parameter[] newParams = cloneParams(srcParams);
    List<Expression> argList = new ArrayList<Expression>(newParams.length);
    for (int i = 0; i < srcParams.length; i++) {
        argList.add(varX(newParams[i]));
    }
    ClosureExpression expression = new ClosureExpression(newParams, stmt(callThisX(privateMethod.getName(), args(argList))));
    MethodCallExpression mce;
    if (protectedCacheSize == 0 && maxCacheSize == 0) {
        mce = callX(expression, MEMOIZE_METHOD_NAME);
    } else if (protectedCacheSize == 0) {
        mce = callX(expression, MEMOIZE_AT_MOST_METHOD_NAME, args(constX(maxCacheSize)));
    } else if (maxCacheSize == 0) {
        mce = callX(expression, MEMOIZE_AT_LEAST_METHOD_NAME, args(constX(protectedCacheSize)));
    } else {
        mce = callX(expression, MEMOIZE_BETWEEN_METHOD_NAME, args(constX(protectedCacheSize), constX(maxCacheSize)));
    }
    mce.setImplicitThis(false);
    return mce;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression)

Aggregations

ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)53 Expression (org.codehaus.groovy.ast.expr.Expression)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)35 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)28 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)23 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)20 ClassNode (org.codehaus.groovy.ast.ClassNode)19 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)18 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)17 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)17 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)17 Parameter (org.codehaus.groovy.ast.Parameter)16 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)14 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)11 FieldNode (org.codehaus.groovy.ast.FieldNode)10 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)10 ArrayList (java.util.ArrayList)9