Search in sources :

Example 71 with VariableExpression

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

the class EqualsAndHashCodeASTTransformation method createEquals.

public static void createEquals(ClassNode cNode, boolean includeFields, boolean callSuper, boolean useCanEqual, List<String> excludes, List<String> includes, boolean allNames) {
    if (useCanEqual)
        createCanEqual(cNode);
    // make a public method if none exists otherwise try a private method with leading underscore
    boolean hasExistingEquals = hasDeclaredMethod(cNode, "equals", 1);
    if (hasExistingEquals && hasDeclaredMethod(cNode, "_equals", 1))
        return;
    final BlockStatement body = new BlockStatement();
    VariableExpression other = varX("other");
    // some short circuit cases for efficiency
    body.addStatement(ifS(equalsNullX(other), returnS(constX(Boolean.FALSE, true))));
    body.addStatement(ifS(sameX(varX("this"), other), returnS(constX(Boolean.TRUE, true))));
    if (useCanEqual) {
        body.addStatement(ifS(notX(isInstanceOfX(other, GenericsUtils.nonGeneric(cNode))), returnS(constX(Boolean.FALSE, true))));
    } else {
        body.addStatement(ifS(notX(hasClassX(other, GenericsUtils.nonGeneric(cNode))), returnS(constX(Boolean.FALSE, true))));
    }
    VariableExpression otherTyped = varX("otherTyped", GenericsUtils.nonGeneric(cNode));
    CastExpression castExpression = new CastExpression(GenericsUtils.nonGeneric(cNode), other);
    castExpression.setStrict(true);
    body.addStatement(declS(otherTyped, castExpression));
    if (useCanEqual) {
        body.addStatement(ifS(notX(callX(otherTyped, "canEqual", varX("this"))), returnS(constX(Boolean.FALSE, true))));
    }
    List<PropertyNode> pList = getInstanceProperties(cNode);
    for (PropertyNode pNode : pList) {
        if (shouldSkip(pNode.getName(), excludes, includes, allNames))
            continue;
        boolean canBeSelf = StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(pNode.getOriginType(), cNode);
        if (!canBeSelf) {
            body.addStatement(ifS(notX(hasEqualPropertyX(otherTyped.getOriginType(), pNode, otherTyped)), returnS(constX(Boolean.FALSE, true))));
        } else {
            body.addStatement(ifS(notX(hasSamePropertyX(pNode, otherTyped)), ifElseS(differentSelfRecursivePropertyX(pNode, otherTyped), returnS(constX(Boolean.FALSE, true)), ifS(notX(bothSelfRecursivePropertyX(pNode, otherTyped)), ifS(notX(hasEqualPropertyX(otherTyped.getOriginType(), pNode, otherTyped)), returnS(constX(Boolean.FALSE, true)))))));
        }
    }
    List<FieldNode> fList = new ArrayList<FieldNode>();
    if (includeFields) {
        fList.addAll(getInstanceNonPropertyFields(cNode));
    }
    for (FieldNode fNode : fList) {
        if (shouldSkip(fNode.getName(), excludes, includes, allNames))
            continue;
        body.addStatement(ifS(notX(hasSameFieldX(fNode, otherTyped)), ifElseS(differentSelfRecursiveFieldX(fNode, otherTyped), returnS(constX(Boolean.FALSE, true)), ifS(notX(bothSelfRecursiveFieldX(fNode, otherTyped)), ifS(notX(hasEqualFieldX(fNode, otherTyped)), returnS(constX(Boolean.FALSE, true)))))));
    }
    if (callSuper) {
        body.addStatement(ifS(notX(isTrueX(callSuperX("equals", other))), returnS(constX(Boolean.FALSE, true))));
    }
    // default
    body.addStatement(returnS(constX(Boolean.TRUE, true)));
    cNode.addMethod(new MethodNode(hasExistingEquals ? "_equals" : "equals", hasExistingEquals ? ACC_PRIVATE : ACC_PUBLIC, ClassHelper.boolean_TYPE, params(param(OBJECT_TYPE, other.getName())), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 72 with VariableExpression

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

the class GeneralUtils method getterThisX.

/**
     * This method is similar to {@link #propX(Expression, Expression)} but will make sure that if the property
     * being accessed is defined inside the classnode provided as a parameter, then a getter call is generated
     * instead of a field access.
     * @param annotatedNode the class node where the property node is accessed from
     * @param pNode the property being accessed
     * @return a method call expression or a property expression
     */
public static Expression getterThisX(ClassNode annotatedNode, PropertyNode pNode) {
    ClassNode owner = pNode.getDeclaringClass();
    if (annotatedNode.equals(owner)) {
        String getterName = "get" + MetaClassHelper.capitalize(pNode.getName());
        boolean existingExplicitGetter = annotatedNode.getMethod(getterName, Parameter.EMPTY_ARRAY) != null;
        if (ClassHelper.boolean_TYPE.equals(pNode.getOriginType()) && !existingExplicitGetter) {
            getterName = "is" + MetaClassHelper.capitalize(pNode.getName());
        }
        return callThisX(getterName);
    }
    return propX(new VariableExpression("this"), pNode.getName());
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 73 with VariableExpression

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

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 74 with VariableExpression

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

the class BinaryExpressionTransformer method transformDeclarationExpression.

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();
            if (rightExpression instanceof ConstantExpression && ClassHelper.STRING_TYPE.equals(rightExpression.getType())) {
                String text = (String) ((ConstantExpression) rightExpression).getValue();
                if (text.length() == 1) {
                    // optimize char initialization
                    ConstantExpression ce = new ConstantExpression(text.charAt(0), true);
                    ce.setSourcePosition(rightExpression);
                    bin.setRightExpression(ce);
                    return bin;
                }
            }
        }
    }
    return null;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListOfExpressionsExpression(org.codehaus.groovy.transform.sc.ListOfExpressionsExpression) 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) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 75 with VariableExpression

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

the class MethodCallExpressionTransformer method transformToMopSuperCall.

private static MethodCallExpression transformToMopSuperCall(final ClassNode superCallReceiver, final MethodCallExpression expr) {
    MethodNode mn = expr.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
    String mopName = MopWriter.getMopMethodName(mn, false);
    MethodNode direct = new MethodNode(mopName, ACC_PUBLIC | ACC_SYNTHETIC, mn.getReturnType(), mn.getParameters(), mn.getExceptions(), EmptyStatement.INSTANCE);
    direct.setDeclaringClass(superCallReceiver);
    MethodCallExpression result = new MethodCallExpression(new VariableExpression("this"), mopName, expr.getArguments());
    result.setImplicitThis(true);
    result.setSpreadSafe(false);
    result.setSafe(false);
    result.setSourcePosition(expr);
    result.setMethodTarget(direct);
    return result;
}
Also used : ExtensionMethodNode(org.codehaus.groovy.transform.stc.ExtensionMethodNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) 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