Search in sources :

Example 1 with ImportNode

use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.

the class StaticImportVisitor method transformMapEntryExpression.

// if you have a Bar class with a static foo property, and this:
//   import static Bar.foo as baz
// then this constructor (not normal usage of statics):
//   new Bar(baz:1)
// will become:
//   new Bar(foo:1)
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;
}
Also used : MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 2 with ImportNode

use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.

the class StaticImportVisitor method findStaticMethodImportFromModule.

private Expression findStaticMethodImportFromModule(Expression method, Expression args) {
    ModuleNode module = currentClass.getModule();
    if (module == null || !(method instanceof ConstantExpression))
        return null;
    Map<String, ImportNode> importNodes = module.getStaticImports();
    ConstantExpression ce = (ConstantExpression) method;
    Expression expression;
    Object value = ce.getValue();
    // skip non-Strings, e.g. Integer
    if (!(value instanceof String))
        return null;
    final String name = (String) value;
    // when resolving methodCall() or getProp() or setProp()
    if (importNodes.containsKey(name)) {
        ImportNode importNode = importNodes.get(name);
        expression = findStaticMethod(importNode.getType(), importNode.getFieldName(), args);
        if (expression != null)
            return expression;
        expression = findStaticPropertyAccessorGivenArgs(importNode.getType(), getPropNameForAccessor(importNode.getFieldName()), args);
        if (expression != null) {
            return new StaticMethodCallExpression(importNode.getType(), importNode.getFieldName(), args);
        }
    }
    // when resolving getProp() or setProp()
    if (isValidAccessorName(name)) {
        String propName = getPropNameForAccessor(name);
        if (importNodes.containsKey(propName)) {
            ImportNode importNode = importNodes.get(propName);
            ClassNode importClass = importNode.getType();
            String importMember = importNode.getFieldName();
            expression = findStaticMethod(importClass, prefix(name) + capitalize(importMember), args);
            if (expression != null)
                return expression;
            expression = findStaticPropertyAccessorGivenArgs(importClass, importMember, args);
            if (expression != null) {
                return new StaticMethodCallExpression(importClass, prefix(name) + capitalize(importMember), args);
            }
        }
    }
    Map<String, ImportNode> starImports = module.getStaticStarImports();
    ClassNode starImportType;
    if (currentClass.isEnum() && starImports.containsKey(currentClass.getName())) {
        ImportNode importNode = starImports.get(currentClass.getName());
        starImportType = importNode == null ? null : importNode.getType();
        expression = findStaticMethod(starImportType, name, args);
        if (expression != null)
            return expression;
    } else {
        for (ImportNode importNode : starImports.values()) {
            starImportType = importNode == null ? null : importNode.getType();
            expression = findStaticMethod(starImportType, name, args);
            if (expression != null)
                return expression;
            expression = findStaticPropertyAccessorGivenArgs(starImportType, getPropNameForAccessor(name), args);
            if (expression != null) {
                return new StaticMethodCallExpression(starImportType, name, args);
            }
        }
    }
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 3 with ImportNode

use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.

the class StaticImportVisitor method findStaticProperty.

private Expression findStaticProperty(Map<String, ImportNode> importNodes, String accessorName) {
    Expression result = null;
    ImportNode importNode = importNodes.get(accessorName);
    ClassNode importClass = importNode.getType();
    String importMember = importNode.getFieldName();
    result = findStaticPropertyAccessorByFullName(importClass, importMember);
    if (result == null) {
        result = findStaticPropertyAccessor(importClass, getPropNameForAccessor(importMember));
    }
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) NamedArgumentListExpression(org.codehaus.groovy.ast.expr.NamedArgumentListExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) AnnotationConstantExpression(org.codehaus.groovy.ast.expr.AnnotationConstantExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ImportNode(org.codehaus.groovy.ast.ImportNode)

Example 4 with ImportNode

use of org.codehaus.groovy.ast.ImportNode in project groovy by apache.

the class BaseScriptASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) PackageNode(org.codehaus.groovy.ast.PackageNode)

Example 5 with ImportNode

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

the class BaseScriptASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        changeBaseScriptTypeFromDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof ImportNode || parent instanceof PackageNode) {
        changeBaseScriptTypeFromPackageOrImport(source, parent, node);
    } else if (parent instanceof ClassNode) {
        changeBaseScriptTypeFromClass((ClassNode) parent, node);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ImportNode(org.codehaus.groovy.ast.ImportNode) PackageNode(org.codehaus.groovy.ast.PackageNode)

Aggregations

ImportNode (org.codehaus.groovy.ast.ImportNode)11 ClassNode (org.codehaus.groovy.ast.ClassNode)10 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)6 ModuleNode (org.codehaus.groovy.ast.ModuleNode)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)6 Expression (org.codehaus.groovy.ast.expr.Expression)6 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)6 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)6 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)5 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)5 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)5 ArrayList (java.util.ArrayList)4 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)4 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)4 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)4 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)4 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)4 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)4 MapEntryExpression (org.codehaus.groovy.ast.expr.MapEntryExpression)4 NamedArgumentListExpression (org.codehaus.groovy.ast.expr.NamedArgumentListExpression)4