Search in sources :

Example 86 with VariableExpression

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

the class AutoNewLineTransformer method createNewLine.

private Statement createNewLine(final ASTNode node) {
    MethodCallExpression mce = new MethodCallExpression(new VariableExpression("this"), "newLine", ArgumentListExpression.EMPTY_ARGUMENTS);
    mce.setImplicitThis(true);
    mce.setSourcePosition(node);
    ExpressionStatement stmt = new ExpressionStatement(mce);
    stmt.setSourcePosition(node);
    return stmt;
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression)

Example 87 with VariableExpression

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

the class GradleResolveVisitor method lookupClassName.

private String lookupClassName(PropertyExpression pe) {
    boolean doInitialClassTest = true;
    String name = "";
    // separated by "."
    for (Expression it = pe; it != null; it = ((PropertyExpression) it).getObjectExpression()) {
        if (it instanceof VariableExpression) {
            VariableExpression ve = (VariableExpression) it;
            // stop at super and this
            if (ve.isSuperExpression() || ve.isThisExpression()) {
                return null;
            }
            String varName = ve.getName();
            if (doInitialClassTest) {
                // field bar.
                if (!testVanillaNameForClass(varName)) {
                    return null;
                }
                doInitialClassTest = false;
                name = varName;
            } else {
                name = varName + "." + name;
            }
            break;
        } else if (it.getClass() != PropertyExpression.class) {
            // VariableExpressions will stop resolving
            return null;
        } else {
            PropertyExpression current = (PropertyExpression) it;
            String propertyPart = current.getPropertyAsString();
            // the class property stops resolving, dynamic property names too
            if (propertyPart == null || propertyPart.equals("class")) {
                return null;
            }
            if (doInitialClassTest) {
                // field bar.
                if (!testVanillaNameForClass(propertyPart)) {
                    return null;
                }
                doInitialClassTest = false;
                name = propertyPart;
            } else {
                name = propertyPart + "." + name;
            }
        }
    }
    if (name.length() == 0) {
        return null;
    }
    return name;
}
Also used : ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

Example 88 with VariableExpression

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

the class RulesVisitor method extractModelPathFromMethodTarget.

// if the target was invalid
@Nullable
private String extractModelPathFromMethodTarget(MethodCallExpression call) {
    Expression target = call.getMethod();
    List<String> names = Lists.newLinkedList();
    while (true) {
        if (target instanceof ConstantExpression) {
            if (target.getType().equals(ClassHelper.STRING_TYPE)) {
                String name = target.getText();
                names.add(0, name);
                if (call.isImplicitThis()) {
                    break;
                } else {
                    target = call.getObjectExpression();
                    continue;
                }
            }
        } else if (target instanceof PropertyExpression) {
            PropertyExpression propertyExpression = (PropertyExpression) target;
            Expression property = propertyExpression.getProperty();
            if (property instanceof ConstantExpression) {
                ConstantExpression constantProperty = (ConstantExpression) property;
                if (constantProperty.getType().equals(ClassHelper.STRING_TYPE)) {
                    String name = constantProperty.getText();
                    names.add(0, name);
                    target = propertyExpression.getObjectExpression();
                    continue;
                }
            }
        } else if (target instanceof VariableExpression) {
            // This will be the left most property
            names.add(0, ((VariableExpression) target).getName());
            break;
        }
        // Invalid paths fall through to here
        restrict(call);
        return null;
    }
    // TODO - validate that it's a valid model path
    return ModelPath.pathString(Iterables.toArray(names, String.class));
}
Also used : 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) 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) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) Nullable(javax.annotation.Nullable)

Example 89 with VariableExpression

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

the class InnerClassVisitor method visitConstructorCallExpression.

@Override
public void visitConstructorCallExpression(ConstructorCallExpression call) {
    super.visitConstructorCallExpression(call);
    if (!call.isUsingAnonymousInnerClass()) {
        passThisReference(call);
        return;
    }
    InnerClassNode innerClass = (InnerClassNode) call.getType();
    ClassNode outerClass = innerClass.getOuterClass();
    ClassNode superClass = innerClass.getSuperClass();
    if (superClass instanceof InnerClassNode && !superClass.isInterface() && !(superClass.isStaticClass() || ((superClass.getModifiers() & ACC_STATIC) == ACC_STATIC))) {
        insertThis0ToSuperCall(call, innerClass);
    }
    if (!innerClass.getDeclaredConstructors().isEmpty())
        return;
    if ((innerClass.getModifiers() & ACC_STATIC) != 0)
        return;
    VariableScope scope = innerClass.getVariableScope();
    if (scope == null)
        return;
    // expressions = constructor call arguments
    List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
    // block = init code for the constructor we produce
    BlockStatement block = new BlockStatement();
    // parameters = parameters of the constructor
    final int additionalParamCount = 1 + scope.getReferencedLocalVariablesCount();
    List<Parameter> parameters = new ArrayList<Parameter>(expressions.size() + additionalParamCount);
    // superCallArguments = arguments for the super call == the constructor call arguments
    List<Expression> superCallArguments = new ArrayList<Expression>(expressions.size());
    // first we add a super() call for all expressions given in the 
    // constructor call expression
    int pCount = additionalParamCount;
    for (Expression expr : expressions) {
        pCount++;
        // add one parameter for each expression in the
        // constructor call
        Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + pCount);
        parameters.add(param);
        // add to super call
        superCallArguments.add(new VariableExpression(param));
    }
    // add the super call
    ConstructorCallExpression cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression(superCallArguments));
    block.addStatement(new ExpressionStatement(cce));
    // we need to add "this" to access unknown methods/properties
    // this is saved in a field named this$0
    pCount = 0;
    expressions.add(pCount, VariableExpression.THIS_EXPRESSION);
    boolean isStatic = isStaticThis(innerClass, scope);
    ClassNode outerClassType = getClassNode(outerClass, isStatic);
    if (!isStatic && inClosure)
        outerClassType = ClassHelper.CLOSURE_TYPE;
    outerClassType = outerClassType.getPlainNodeReference();
    Parameter thisParameter = new Parameter(outerClassType, "p" + pCount);
    parameters.add(pCount, thisParameter);
    thisField = innerClass.addField("this$0", PUBLIC_SYNTHETIC, outerClassType, null);
    addFieldInit(thisParameter, thisField, block);
    // for each shared variable we add a reference and save it as field
    for (Iterator it = scope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
        pCount++;
        org.codehaus.groovy.ast.Variable var = (org.codehaus.groovy.ast.Variable) it.next();
        VariableExpression ve = new VariableExpression(var);
        ve.setClosureSharedVariable(true);
        ve.setUseReferenceDirectly(true);
        expressions.add(pCount, ve);
        ClassNode rawReferenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference();
        Parameter p = new Parameter(rawReferenceType, "p" + pCount);
        parameters.add(pCount, p);
        p.setOriginType(var.getOriginType());
        final VariableExpression initial = new VariableExpression(p);
        initial.setSynthetic(true);
        initial.setUseReferenceDirectly(true);
        final FieldNode pField = innerClass.addFieldFirst(ve.getName(), PUBLIC_SYNTHETIC, rawReferenceType, initial);
        pField.setHolder(true);
        pField.setOriginType(ClassHelper.getWrapper(var.getOriginType()));
    }
    innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(new Parameter[parameters.size()]), ClassNode.EMPTY_ARRAY, block);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) 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) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Iterator(java.util.Iterator) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

Example 90 with VariableExpression

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

the class Verifier method visitConstructor.

public void visitConstructor(ConstructorNode node) {
    CodeVisitorSupport checkSuper = new CodeVisitorSupport() {

        boolean firstMethodCall = true;

        String type = null;

        public void visitMethodCallExpression(MethodCallExpression call) {
            if (!firstMethodCall)
                return;
            firstMethodCall = false;
            String name = call.getMethodAsString();
            // the name might be null if the method name is a GString for example
            if (name == null)
                return;
            if (!name.equals("super") && !name.equals("this"))
                return;
            type = name;
            call.getArguments().visit(this);
            type = null;
        }

        public void visitConstructorCallExpression(ConstructorCallExpression call) {
            if (!call.isSpecialCall())
                return;
            type = call.getText();
            call.getArguments().visit(this);
            type = null;
        }

        public void visitVariableExpression(VariableExpression expression) {
            if (type == null)
                return;
            String name = expression.getName();
            if (!name.equals("this") && !name.equals("super"))
                return;
            throw new RuntimeParserException("cannot reference " + name + " inside of " + type + "(....) before supertype constructor has been called", expression);
        }
    };
    Statement s = node.getCode();
    if (s == null) {
        return;
    } else {
        s.visit(new VerifierCodeVisitor(this));
    }
    s.visit(checkSuper);
}
Also used : MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) RuntimeParserException(org.codehaus.groovy.syntax.RuntimeParserException)

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