Search in sources :

Example 76 with VariableExpression

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

the class MethodCallExpressionTransformer method transformMethodCallExpression.

Expression transformMethodCallExpression(final MethodCallExpression expr) {
    Expression trn = tryTransformIsToCompareIdentity(expr);
    if (trn != null) {
        return trn;
    }
    ClassNode superCallReceiver = expr.getNodeMetaData(StaticTypesMarker.SUPER_MOP_METHOD_REQUIRED);
    if (superCallReceiver != null) {
        return transformMethodCallExpression(transformToMopSuperCall(superCallReceiver, expr));
    }
    Expression objectExpression = expr.getObjectExpression();
    ClassNode type = staticCompilationTransformer.getTypeChooser().resolveType(objectExpression, staticCompilationTransformer.getClassNode());
    if (isCallOnClosure(expr)) {
        FieldNode field = staticCompilationTransformer.getClassNode().getField(expr.getMethodAsString());
        if (field != null) {
            VariableExpression vexp = new VariableExpression(field);
            MethodCallExpression result = new MethodCallExpression(vexp, "call", staticCompilationTransformer.transform(expr.getArguments()));
            result.setImplicitThis(false);
            result.setSourcePosition(expr);
            result.setSafe(expr.isSafe());
            result.setSpreadSafe(expr.isSpreadSafe());
            result.setMethodTarget(StaticTypeCheckingVisitor.CLOSURE_CALL_VARGS);
            return result;
        }
    }
    if (type != null && type.isArray()) {
        String method = expr.getMethodAsString();
        ClassNode componentType = type.getComponentType();
        if ("getAt".equals(method)) {
            Expression arguments = expr.getArguments();
            if (arguments instanceof TupleExpression) {
                List<Expression> argList = ((TupleExpression) arguments).getExpressions();
                if (argList.size() == 1) {
                    Expression indexExpr = argList.get(0);
                    ClassNode argType = staticCompilationTransformer.getTypeChooser().resolveType(indexExpr, staticCompilationTransformer.getClassNode());
                    ClassNode indexType = ClassHelper.getWrapper(argType);
                    if (componentType.isEnum() && ClassHelper.Number_TYPE == indexType) {
                        // workaround for generated code in enums which use .next() returning a Number
                        indexType = ClassHelper.Integer_TYPE;
                    }
                    if (argType != null && ClassHelper.Integer_TYPE == indexType) {
                        BinaryExpression binaryExpression = new BinaryExpression(objectExpression, Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()), indexExpr);
                        binaryExpression.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
                        return staticCompilationTransformer.transform(binaryExpression);
                    }
                }
            }
        } else if ("putAt".equals(method)) {
            Expression arguments = expr.getArguments();
            if (arguments instanceof TupleExpression) {
                List<Expression> argList = ((TupleExpression) arguments).getExpressions();
                if (argList.size() == 2) {
                    Expression indexExpr = argList.get(0);
                    Expression objExpr = argList.get(1);
                    ClassNode argType = staticCompilationTransformer.getTypeChooser().resolveType(indexExpr, staticCompilationTransformer.getClassNode());
                    if (argType != null && ClassHelper.Integer_TYPE == ClassHelper.getWrapper(argType)) {
                        BinaryExpression arrayGet = new BinaryExpression(objectExpression, Token.newSymbol("[", indexExpr.getLineNumber(), indexExpr.getColumnNumber()), indexExpr);
                        arrayGet.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, componentType);
                        BinaryExpression assignment = new BinaryExpression(arrayGet, Token.newSymbol("=", objExpr.getLineNumber(), objExpr.getColumnNumber()), objExpr);
                        return staticCompilationTransformer.transform(assignment);
                    }
                }
            }
        }
    }
    return staticCompilationTransformer.superTransform(expr);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) List(java.util.List)

Example 77 with VariableExpression

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

the class FieldASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    sourceUnit = source;
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) parent;
        ClassNode cNode = de.getDeclaringClass();
        if (!cNode.isScript()) {
            addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
            return;
        }
        candidate = de;
        // GROOVY-4548: temp fix to stop CCE until proper support is added
        if (de.isMultipleAssignmentDeclaration()) {
            addError("Annotation " + MY_TYPE_NAME + " not supported with multiple assignment notation.", parent);
            return;
        }
        VariableExpression ve = de.getVariableExpression();
        variableName = ve.getName();
        // set owner null here, it will be updated by addField
        fieldNode = new FieldNode(variableName, ve.getModifiers(), ve.getType(), null, de.getRightExpression());
        fieldNode.setSourcePosition(de);
        cNode.addField(fieldNode);
        String setterName = "set" + MetaClassHelper.capitalize(variableName);
        cNode.addMethod(setterName, ACC_PUBLIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE, params(param(ve.getType(), variableName)), ClassNode.EMPTY_ARRAY, block(stmt(assignX(propX(varX("this"), variableName), varX(variableName)))));
        // GROOVY-4833 : annotations that are not Groovy transforms should be transferred to the generated field
        // GROOVY-6112 : also copy acceptable Groovy transforms
        final List<AnnotationNode> annotations = de.getAnnotations();
        for (AnnotationNode annotation : annotations) {
            // GROOVY-6337 HACK: in case newly created field is @Lazy
            if (annotation.getClassNode().equals(LAZY_TYPE)) {
                LazyASTTransformation.visitField(this, annotation, fieldNode);
            }
            final ClassNode annotationClassNode = annotation.getClassNode();
            if (notTransform(annotationClassNode) || acceptableTransform(annotation)) {
                fieldNode.addAnnotation(annotation);
            }
        }
        super.visitClass(cNode);
        // GROOVY-5207 So that Closures can see newly added fields
        // (not super efficient for a very large class with many @Fields but we chose simplicity
        // and understandability of this solution over more complex but efficient alternatives)
        VariableScopeVisitor scopeVisitor = new VariableScopeVisitor(source);
        scopeVisitor.visitClass(cNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 78 with VariableExpression

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

the class LogASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotatedNode targetClass = (AnnotatedNode) nodes[1];
    AnnotationNode logAnnotation = (AnnotationNode) nodes[0];
    final GroovyClassLoader classLoader = compilationUnit != null ? compilationUnit.getTransformLoader() : source.getClassLoader();
    final LoggingStrategy loggingStrategy = createLoggingStrategy(logAnnotation, classLoader);
    if (loggingStrategy == null)
        return;
    final String logFieldName = lookupLogFieldName(logAnnotation);
    final String categoryName = lookupCategoryName(logAnnotation);
    if (!(targetClass instanceof ClassNode))
        throw new GroovyBugError("Class annotation " + logAnnotation.getClassNode().getName() + " annotated no Class, this must not happen.");
    final ClassNode classNode = (ClassNode) targetClass;
    ClassCodeExpressionTransformer transformer = new ClassCodeExpressionTransformer() {

        private FieldNode logNode;

        @Override
        protected SourceUnit getSourceUnit() {
            return source;
        }

        public Expression transform(Expression exp) {
            if (exp == null)
                return null;
            if (exp instanceof MethodCallExpression) {
                return transformMethodCallExpression(exp);
            }
            if (exp instanceof ClosureExpression) {
                return transformClosureExpression((ClosureExpression) exp);
            }
            return super.transform(exp);
        }

        @Override
        public void visitClass(ClassNode node) {
            FieldNode logField = node.getField(logFieldName);
            if (logField != null && logField.getOwner().equals(node)) {
                addError("Class annotated with Log annotation cannot have log field declared", logField);
            } else if (logField != null && !Modifier.isPrivate(logField.getModifiers())) {
                addError("Class annotated with Log annotation cannot have log field declared because the field exists in the parent class: " + logField.getOwner().getName(), logField);
            } else {
                logNode = loggingStrategy.addLoggerFieldToClass(node, logFieldName, categoryName);
            }
            super.visitClass(node);
        }

        private Expression transformClosureExpression(ClosureExpression exp) {
            if (exp.getCode() instanceof BlockStatement) {
                BlockStatement code = (BlockStatement) exp.getCode();
                super.visitBlockStatement(code);
            }
            return exp;
        }

        private Expression transformMethodCallExpression(Expression exp) {
            Expression modifiedCall = addGuard((MethodCallExpression) exp);
            return modifiedCall == null ? super.transform(exp) : modifiedCall;
        }

        private Expression addGuard(MethodCallExpression mce) {
            // only add guard to methods of the form: logVar.logMethod(params)
            if (!(mce.getObjectExpression() instanceof VariableExpression)) {
                return null;
            }
            VariableExpression variableExpression = (VariableExpression) mce.getObjectExpression();
            if (!variableExpression.getName().equals(logFieldName) || !(variableExpression.getAccessedVariable() instanceof DynamicVariable)) {
                return null;
            }
            String methodName = mce.getMethodAsString();
            if (methodName == null)
                return null;
            if (!loggingStrategy.isLoggingMethod(methodName))
                return null;
            // since there is no saving
            if (usesSimpleMethodArgumentsOnly(mce))
                return null;
            variableExpression.setAccessedVariable(logNode);
            return loggingStrategy.wrapLoggingMethodCall(variableExpression, methodName, mce);
        }

        private boolean usesSimpleMethodArgumentsOnly(MethodCallExpression mce) {
            Expression arguments = mce.getArguments();
            if (arguments instanceof TupleExpression) {
                TupleExpression tuple = (TupleExpression) arguments;
                for (Expression exp : tuple.getExpressions()) {
                    if (!isSimpleExpression(exp))
                        return false;
                }
                return true;
            }
            return !isSimpleExpression(arguments);
        }

        private boolean isSimpleExpression(Expression exp) {
            if (exp instanceof ConstantExpression)
                return true;
            if (exp instanceof VariableExpression)
                return true;
            return false;
        }
    };
    transformer.visitClass(classNode);
    // GROOVY-6373: references to 'log' field are normally already FieldNodes by now, so revisit scoping
    new VariableScopeVisitor(sourceUnit, true).visitClass(classNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) GroovyBugError(org.codehaus.groovy.GroovyBugError) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) GroovyClassLoader(groovy.lang.GroovyClassLoader) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) 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) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Example 79 with VariableExpression

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

the class MapConstructorASTTransformation method makeMapTypedArgsTransformer.

private static ClassCodeExpressionTransformer makeMapTypedArgsTransformer() {
    return new ClassCodeExpressionTransformer() {

        @Override
        public Expression transform(Expression exp) {
            if (exp instanceof ClosureExpression) {
                ClosureExpression ce = (ClosureExpression) exp;
                ce.getCode().visit(this);
            } else if (exp instanceof VariableExpression) {
                VariableExpression ve = (VariableExpression) exp;
                if (ve.getName().equals("args") && ve.getAccessedVariable() instanceof DynamicVariable) {
                    VariableExpression newVe = new VariableExpression(new Parameter(MAP_TYPE, "args"));
                    newVe.setSourcePosition(ve);
                    return newVe;
                }
            }
            return exp.transformExpression(this);
        }

        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }
    };
}
Also used : 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) DynamicVariable(org.codehaus.groovy.ast.DynamicVariable) Parameter(org.codehaus.groovy.ast.Parameter) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassCodeExpressionTransformer(org.codehaus.groovy.ast.ClassCodeExpressionTransformer)

Example 80 with VariableExpression

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

the class VariableExpressionTransformer method tryTransformDelegateToProperty.

private static Expression tryTransformDelegateToProperty(VariableExpression expr) {
    // we need to transform variable expressions that go to a delegate
    // to a property expression, as ACG would loose the information
    // in processClassVariable before it reaches any makeCall, that could
    // handle it
    Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (val == null)
        return null;
    VariableExpression implicitThis = new VariableExpression("this");
    PropertyExpression pexp = new PropertyExpression(implicitThis, expr.getName());
    pexp.copyNodeMetaData(expr);
    pexp.setImplicitThis(true);
    ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
    if (owner != null) {
        implicitThis.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner);
        implicitThis.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val);
    }
    return pexp;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression)

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