Search in sources :

Example 1 with ModuleNode

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

the class GenericsUtils method resolveClassNode.

private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode) {
    ClassNode dummyClass = new ClassNode("dummy", 0, ClassHelper.OBJECT_TYPE);
    dummyClass.setModule(new ModuleNode(sourceUnit));
    dummyClass.setGenericsTypes(mn.getDeclaringClass().getGenericsTypes());
    MethodNode dummyMN = new MethodNode("dummy", 0, parsedNode, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
    dummyMN.setGenericsTypes(mn.getGenericsTypes());
    dummyClass.addMethod(dummyMN);
    ResolveVisitor visitor = new ResolveVisitor(compilationUnit) {

        @Override
        public void addError(final String msg, final ASTNode expr) {
            sourceUnit.addError(new IncorrectTypeHintException(mn, msg, usage.getLineNumber(), usage.getColumnNumber()));
        }
    };
    visitor.startResolving(dummyClass, sourceUnit);
    return dummyMN.getReturnType();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) IncorrectTypeHintException(groovy.transform.stc.IncorrectTypeHintException) MethodNode(org.codehaus.groovy.ast.MethodNode) ASTNode(org.codehaus.groovy.ast.ASTNode) ResolveVisitor(org.codehaus.groovy.control.ResolveVisitor) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 2 with ModuleNode

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

the class AsmClassGenerator method addInnerClass.

public boolean addInnerClass(ClassNode innerClass) {
    ModuleNode mn = controller.getClassNode().getModule();
    innerClass.setModule(mn);
    mn.getUnit().addGeneratedInnerClass((InnerClassNode) innerClass);
    return innerClasses.add(innerClass);
}
Also used : ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 3 with ModuleNode

use of org.codehaus.groovy.ast.ModuleNode 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 4 with ModuleNode

use of org.codehaus.groovy.ast.ModuleNode 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 5 with ModuleNode

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

the class AbstractInterruptibleASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        internalError("Expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    this.source = source;
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
    if (!type().equals(node.getClassNode())) {
        internalError("Transformation called from wrong annotation: " + node.getClassNode().getName());
    }
    setupTransform(node);
    // should be limited to the current SourceUnit or propagated to the whole CompilationUnit
    final ModuleNode tree = source.getAST();
    if (applyToAllClasses) {
        // guard every class and method defined in this script
        if (tree != null) {
            final List<ClassNode> classes = tree.getClasses();
            for (ClassNode classNode : classes) {
                visitClass(classNode);
            }
        }
    } else if (annotatedNode instanceof ClassNode) {
        // only guard this particular class
        this.visitClass((ClassNode) annotatedNode);
    } else if (!applyToAllMembers && annotatedNode instanceof MethodNode) {
        this.visitMethod((MethodNode) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else if (!applyToAllMembers && annotatedNode instanceof FieldNode) {
        this.visitField((FieldNode) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else if (!applyToAllMembers && annotatedNode instanceof DeclarationExpression) {
        this.visitDeclarationExpression((DeclarationExpression) annotatedNode);
        this.visitClass(annotatedNode.getDeclaringClass());
    } else {
        // only guard the script class
        if (tree != null) {
            final List<ClassNode> classes = tree.getClasses();
            for (ClassNode classNode : classes) {
                if (classNode.isScript()) {
                    visitClass(classNode);
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) 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) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Aggregations

ModuleNode (org.codehaus.groovy.ast.ModuleNode)17 ClassNode (org.codehaus.groovy.ast.ClassNode)9 ImportNode (org.codehaus.groovy.ast.ImportNode)6 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)5 Expression (org.codehaus.groovy.ast.expr.Expression)5 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)5 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)5 ASTNode (org.codehaus.groovy.ast.ASTNode)4 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)4 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)4 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)4 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)4 File (java.io.File)3 MethodNode (org.codehaus.groovy.ast.MethodNode)3 AnnotationConstantExpression (org.codehaus.groovy.ast.expr.AnnotationConstantExpression)3 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)3 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)3 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)3 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)3 EmptyExpression (org.codehaus.groovy.ast.expr.EmptyExpression)3