Search in sources :

Example 71 with Expression

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

the class EqualsAndHashCodeASTTransformation method calculateHashStatements.

private static Statement calculateHashStatements(ClassNode cNode, Expression hash, boolean includeFields, boolean callSuper, List<String> excludes, List<String> includes) {
    final List<PropertyNode> pList = getInstanceProperties(cNode);
    final List<FieldNode> fList = new ArrayList<FieldNode>();
    if (includeFields) {
        fList.addAll(getInstanceNonPropertyFields(cNode));
    }
    final BlockStatement body = new BlockStatement();
    // def _result = HashCodeHelper.initHash()
    final Expression result = varX("_result");
    body.addStatement(declS(result, callX(HASHUTIL_TYPE, "initHash")));
    for (PropertyNode pNode : pList) {
        if (shouldSkip(pNode.getName(), excludes, includes))
            continue;
        // _result = HashCodeHelper.updateHash(_result, getProperty()) // plus self-reference checking
        Expression getter = getterX(cNode, pNode);
        final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, getter));
        body.addStatement(ifS(notX(sameX(getter, varX("this"))), assignS(result, current)));
    }
    for (FieldNode fNode : fList) {
        if (shouldSkip(fNode.getName(), excludes, includes))
            continue;
        // _result = HashCodeHelper.updateHash(_result, field) // plus self-reference checking
        final Expression fieldExpr = varX(fNode);
        final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, fieldExpr));
        body.addStatement(ifS(notX(sameX(fieldExpr, varX("this"))), assignS(result, current)));
    }
    if (callSuper) {
        // _result = HashCodeHelper.updateHash(_result, super.hashCode())
        final Expression current = callX(HASHUTIL_TYPE, "updateHash", args(result, callSuperX("hashCode")));
        body.addStatement(assignS(result, current));
    }
    // $hash$code = _result
    if (hash != null) {
        body.addStatement(assignS(hash, result));
    } else {
        body.addStatement(returnS(result));
    }
    return body;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 72 with Expression

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

the class EqualsAndHashCodeASTTransformation method differentSelfRecursivePropertyX.

private static BinaryExpression differentSelfRecursivePropertyX(PropertyNode pNode, Expression other) {
    String getterName = getGetterName(pNode);
    Expression selfGetter = callThisX(getterName);
    Expression otherGetter = callX(other, getterName);
    return orX(andX(sameX(selfGetter, varX("this")), notX(sameX(otherGetter, other))), andX(notX(sameX(selfGetter, varX("this"))), sameX(otherGetter, other)));
}
Also used : Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 73 with Expression

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

the class LazyASTTransformation method visitField.

static void visitField(AnnotationNode node, FieldNode fieldNode) {
    final Expression soft = node.getMember("soft");
    final Expression init = getInitExpr(fieldNode);
    fieldNode.rename("$" + fieldNode.getName());
    fieldNode.setModifiers(ACC_PRIVATE | (fieldNode.getModifiers() & (~(ACC_PUBLIC | ACC_PROTECTED))));
    if (soft instanceof ConstantExpression && ((ConstantExpression) soft).getValue().equals(true)) {
        createSoft(fieldNode, init);
    } else {
        create(fieldNode, init);
        // @Lazy not meaningful with primitive so convert to wrapper if needed
        if (ClassHelper.isPrimitiveType(fieldNode.getType())) {
            fieldNode.setType(ClassHelper.getWrapper(fieldNode.getType()));
        }
    }
}
Also used : 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression)

Example 74 with Expression

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

the class LazyASTTransformation method createSoftSetter.

private static void createSoftSetter(FieldNode fieldNode, ClassNode type) {
    final BlockStatement body = new BlockStatement();
    final Expression fieldExpr = varX(fieldNode);
    final String name = "set" + MetaClassHelper.capitalize(fieldNode.getName().substring(1));
    final Parameter parameter = param(type, "value");
    final Expression paramExpr = varX(parameter);
    body.addStatement(ifElseS(notNullX(paramExpr), assignS(fieldExpr, ctorX(SOFT_REF, paramExpr)), assignS(fieldExpr, NULL_EXPR)));
    int visibility = ACC_PUBLIC;
    if (fieldNode.isStatic())
        visibility |= ACC_STATIC;
    fieldNode.getDeclaringClass().addMethod(name, visibility, ClassHelper.VOID_TYPE, params(parameter), ClassNode.EMPTY_ARRAY, body);
}
Also used : 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) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 75 with Expression

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

the class MemoizedASTTransformation method buildMemoizeClosureCallExpression.

private 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

Expression (org.codehaus.groovy.ast.expr.Expression)369 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)257 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)244 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)187 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)187 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)152 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)151 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)149 ClassNode (org.codehaus.groovy.ast.ClassNode)146 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)134 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)130 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)113 TupleExpression (org.codehaus.groovy.ast.expr.TupleExpression)103 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)95 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)84 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)78 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)63 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)63 Statement (org.codehaus.groovy.ast.stmt.Statement)63 MapExpression (org.codehaus.groovy.ast.expr.MapExpression)61