Search in sources :

Example 16 with Expression

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

the class InnerClassCompletionVisitor method getFirstIfSpecialConstructorCall.

private ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
    if (code == null)
        return null;
    final List<Statement> statementList = code.getStatements();
    if (statementList.isEmpty())
        return null;
    final Statement statement = statementList.get(0);
    if (!(statement instanceof ExpressionStatement))
        return null;
    Expression expression = ((ExpressionStatement) statement).getExpression();
    if (!(expression instanceof ConstructorCallExpression))
        return null;
    ConstructorCallExpression cce = (ConstructorCallExpression) expression;
    if (cce.isSpecialCall())
        return cce;
    return null;
}
Also used : ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) 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) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 17 with Expression

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

the class InnerClassVisitor method passThisReference.

// this is the counterpart of addThisReference(). To non-static inner classes, outer this should be
// passed as the first argument implicitly.
private void passThisReference(ConstructorCallExpression call) {
    ClassNode cn = call.getType().redirect();
    if (!shouldHandleImplicitThisForInnerClass(cn))
        return;
    boolean isInStaticContext = true;
    if (currentMethod != null)
        isInStaticContext = currentMethod.getVariableScope().isInStaticContext();
    else if (currentField != null)
        isInStaticContext = currentField.isStatic();
    else if (processingObjInitStatements)
        isInStaticContext = false;
    // if constructor call is not in static context, return
    if (isInStaticContext) {
        // constructor call is in static context and the inner class is non-static - 1st arg is supposed to be 
        // passed as enclosing "this" instance
        //
        Expression args = call.getArguments();
        if (args instanceof TupleExpression && ((TupleExpression) args).getExpressions().isEmpty()) {
            addError("No enclosing instance passed in constructor call of a non-static inner class", call);
        }
        return;
    }
    insertThis0ToSuperCall(call, cn);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression)

Example 18 with Expression

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

the class DefaultASTDatabindingHelper method getPropertyNamesToIncludeInWhiteList.

private Set<String> getPropertyNamesToIncludeInWhiteList(final SourceUnit sourceUnit, final ClassNode classNode) {
    final Set<String> propertyNamesToIncludeInWhiteList = new HashSet<String>();
    final Set<String> unbindablePropertyNames = new HashSet<String>();
    final Set<String> bindablePropertyNames = new HashSet<String>();
    if (!classNode.getSuperClass().equals(new ClassNode(Object.class))) {
        final Set<String> parentClassPropertyNames = getPropertyNamesToIncludeInWhiteListForParentClass(sourceUnit, classNode.getSuperClass());
        bindablePropertyNames.addAll(parentClassPropertyNames);
    }
    final FieldNode constraintsFieldNode = classNode.getDeclaredField(CONSTRAINTS_FIELD_NAME);
    if (constraintsFieldNode != null && constraintsFieldNode.hasInitialExpression()) {
        final Expression constraintsInitialExpression = constraintsFieldNode.getInitialExpression();
        if (constraintsInitialExpression instanceof ClosureExpression) {
            final Map<String, Map<String, Expression>> constraintsInfo = GrailsASTUtils.getConstraintMetadata((ClosureExpression) constraintsInitialExpression);
            for (Entry<String, Map<String, Expression>> constraintConfig : constraintsInfo.entrySet()) {
                final String propertyName = constraintConfig.getKey();
                final Map<String, Expression> mapEntryExpressions = constraintConfig.getValue();
                for (Entry<String, Expression> entry : mapEntryExpressions.entrySet()) {
                    final String constraintName = entry.getKey();
                    if (BINDABLE_CONSTRAINT_NAME.equals(constraintName)) {
                        final Expression valueExpression = entry.getValue();
                        Boolean bindableValue = null;
                        if (valueExpression instanceof ConstantExpression) {
                            final Object constantValue = ((ConstantExpression) valueExpression).getValue();
                            if (constantValue instanceof Boolean) {
                                bindableValue = (Boolean) constantValue;
                            }
                        }
                        if (bindableValue != null) {
                            if (Boolean.TRUE.equals(bindableValue)) {
                                unbindablePropertyNames.remove(propertyName);
                                bindablePropertyNames.add(propertyName);
                            } else {
                                bindablePropertyNames.remove(propertyName);
                                unbindablePropertyNames.add(propertyName);
                            }
                        } else {
                            GrailsASTUtils.warning(sourceUnit, valueExpression, "The bindable constraint for property [" + propertyName + "] in class [" + classNode.getName() + "] has a value which is not a boolean literal and will be ignored.");
                        }
                    }
                }
            }
        }
    }
    final Set<String> fieldsInTransientsList = getPropertyNamesExpressedInTransientsList(classNode);
    propertyNamesToIncludeInWhiteList.addAll(bindablePropertyNames);
    final List<FieldNode> fields = classNode.getFields();
    for (FieldNode fieldNode : fields) {
        final String fieldName = fieldNode.getName();
        final boolean isDomainClass = GrailsASTUtils.isDomainClass(classNode, sourceUnit);
        if ((!unbindablePropertyNames.contains(fieldName)) && (bindablePropertyNames.contains(fieldName) || shouldFieldBeInWhiteList(fieldNode, fieldsInTransientsList, isDomainClass))) {
            propertyNamesToIncludeInWhiteList.add(fieldName);
        }
    }
    final Map<String, MethodNode> declaredMethodsMap = classNode.getDeclaredMethodsMap();
    for (Entry<String, MethodNode> methodEntry : declaredMethodsMap.entrySet()) {
        final MethodNode value = methodEntry.getValue();
        if (classNode.equals(value.getDeclaringClass())) {
            Parameter[] parameters = value.getParameters();
            if (parameters != null && parameters.length == 1) {
                final String methodName = value.getName();
                if (methodName.startsWith("set")) {
                    final Parameter parameter = parameters[0];
                    final ClassNode paramType = parameter.getType();
                    if (!paramType.equals(new ClassNode(Object.class))) {
                        final String restOfMethodName = methodName.substring(3);
                        final String propertyName = GrailsNameUtils.getPropertyName(restOfMethodName);
                        if (!unbindablePropertyNames.contains(propertyName)) {
                            propertyNamesToIncludeInWhiteList.add(propertyName);
                        }
                    }
                }
            }
        }
    }
    CLASS_NODE_TO_WHITE_LIST_PROPERTY_NAMES.put(classNode, propertyNamesToIncludeInWhiteList);
    Map<String, ClassNode> allAssociationMap = GrailsASTUtils.getAllAssociationMap(classNode);
    for (String associationName : allAssociationMap.keySet()) {
        if (!propertyNamesToIncludeInWhiteList.contains(associationName) && !unbindablePropertyNames.contains(associationName)) {
            propertyNamesToIncludeInWhiteList.add(associationName);
        }
    }
    return propertyNamesToIncludeInWhiteList;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) Expression(org.codehaus.groovy.ast.expr.Expression) Parameter(org.codehaus.groovy.ast.Parameter) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 19 with Expression

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

the class InvokeDynamicWriter method makeIndyCall.

private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean implicitThis, boolean safe, String methodName, Expression arguments) {
    OperandStack operandStack = controller.getOperandStack();
    String sig = prepareIndyCall(receiver, implicitThis);
    // load arguments
    int numberOfArguments = 1;
    ArgumentListExpression ae = makeArgumentList(arguments);
    boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
    if (containsSpreadExpression) {
        controller.getAcg().despreadList(ae.getExpressions(), true);
        sig += getTypeDescription(Object[].class);
    } else {
        for (Expression arg : ae.getExpressions()) {
            arg.visit(controller.getAcg());
            if (arg instanceof CastExpression) {
                operandStack.box();
                controller.getAcg().loadWrapper(arg);
                sig += getTypeDescription(Wrapper.class);
            } else {
                sig += getTypeDescription(operandStack.getTopOperand());
            }
            numberOfArguments++;
        }
    }
    sig += ")Ljava/lang/Object;";
    String callSiteName = METHOD.getCallSiteName();
    if (adapter == null)
        callSiteName = INIT.getCallSiteName();
    int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
    finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
}
Also used : OperandStack(org.codehaus.groovy.classgen.asm.OperandStack) Wrapper(org.codehaus.groovy.runtime.wrappers.Wrapper) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 20 with Expression

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

the class StatementWriter method writeExpressionStatement.

public void writeExpressionStatement(ExpressionStatement statement) {
    controller.getAcg().onLineNumber(statement, "visitExpressionStatement: " + statement.getExpression().getClass().getName());
    writeStatementLabel(statement);
    Expression expression = statement.getExpression();
    int mark = controller.getOperandStack().getStackLength();
    expression.visit(controller.getAcg());
    controller.getOperandStack().popDownTo(mark);
}
Also used : 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) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression)

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