Java Code Examples for org.codehaus.groovy.ast.expr.Expression#transformExpression()

The following examples show how to use org.codehaus.groovy.ast.expr.Expression#transformExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticImportVisitor.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Expression transformMapEntryExpression(MapEntryExpression me, ClassNode constructorCallType) {
    Expression key = me.getKeyExpression();
    Expression value = me.getValueExpression();
    ModuleNode module = currentClass.getModule();
    if (module != null && key instanceof ConstantExpression) {
        Map<String, ImportNode> importNodes = module.getStaticImports();
        if (importNodes.containsKey(key.getText())) {
            ImportNode importNode = importNodes.get(key.getText());
            if (importNode.getType().equals(constructorCallType)) {
                String newKey = importNode.getFieldName();
                return new MapEntryExpression(new ConstantExpression(newKey), value.transformExpression(this));
            }
        }
    }
    return me;
}
 
Example 2
Source File: MapConstructorASTTransformation.java    From groovy with Apache License 2.0 6 votes vote down vote up
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 ("args".equals(ve.getName()) && ve.getAccessedVariable() instanceof DynamicVariable) {
                    VariableExpression newVe = varX(param(MAP_TYPE, "args"));
                    newVe.setSourcePosition(ve);
                    return newVe;
                }
            }
            return exp.transformExpression(this);
        }

        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }
    };
}
 
Example 3
Source File: ResolveVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(final Expression exp) {
    if (exp == null) return null;
    Expression ret;
    if (exp instanceof VariableExpression) {
        ret = transformVariableExpression((VariableExpression) exp);
    } else if (exp.getClass() == PropertyExpression.class) {
        ret = transformPropertyExpression((PropertyExpression) exp);
    } else if (exp instanceof DeclarationExpression) {
        ret = transformDeclarationExpression((DeclarationExpression) exp);
    } else if (exp instanceof BinaryExpression) {
        ret = transformBinaryExpression((BinaryExpression) exp);
    } else if (exp instanceof MethodCallExpression) {
        ret = transformMethodCallExpression((MethodCallExpression) exp);
    } else if (exp instanceof ClosureExpression) {
        ret = transformClosureExpression((ClosureExpression) exp);
    } else if (exp instanceof ConstructorCallExpression) {
        ret = transformConstructorCallExpression((ConstructorCallExpression) exp);
    } else if (exp instanceof AnnotationConstantExpression) {
        ret = transformAnnotationConstantExpression((AnnotationConstantExpression) exp);
    } else {
        resolveOrFail(exp.getType(), exp);
        ret = exp.transformExpression(this);
    }
    if (ret != null && ret != exp) {
        ret.setSourcePosition(exp);
    }
    return ret;
}
 
Example 4
Source File: OptimizerVisitor.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Expression transform(Expression exp) {
    if (exp == null) return null;
    if (!currentClass.isInterface() && exp.getClass() == ConstantExpression.class) {
        setConstField((ConstantExpression) exp);
    }
    return exp.transformExpression(this);
}
 
Example 5
Source File: FieldASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public Expression transform(Expression expr) {
    if (expr == null) return null;
    if (expr instanceof DeclarationExpression) {
        DeclarationExpression de = (DeclarationExpression) expr;
        if (de.getLeftExpression() == candidate.getLeftExpression()) {
            if (insideScriptBody) {
                // TODO make EmptyExpression work
                // partially works but not if only thing in script
                // return EmptyExpression.INSTANCE;
                return nullX();
            }
            addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script body.", expr);
            return expr;
        }
    } else if (insideScriptBody && expr instanceof VariableExpression && currentClosure != null) {
        VariableExpression ve = (VariableExpression) expr;
        if (ve.getName().equals(variableName)) {
            adjustToClassVar(ve);
            return ve;
        }
    } else if (currentAIC != null && expr instanceof ArgumentListExpression) {
        // if a match is found, the compiler will have already set up aic constructor to hav
        // an argument which isn't needed since we'll be accessing the field; we must undo it
        Expression skip = null;
        List<Expression> origArgList = ((ArgumentListExpression) expr).getExpressions();
        for (int i = 0; i < origArgList.size(); i++) {
            Expression arg = origArgList.get(i);
            if (matchesCandidate(arg)) {
                skip = arg;
                adjustConstructorAndFields(i, currentAIC.getType());
                break;
            }
        }
        if (skip != null) {
            return adjustedArgList(skip, origArgList);
        }
    }
    return expr.transformExpression(this);
}
 
Example 6
Source File: ClassCodeExpressionTransformer.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
public Expression transform(Expression expr) {
    if (expr == null) return null;
    return expr.transformExpression(this);
}