Search in sources :

Example 6 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 7 with ModuleNode

use of org.codehaus.groovy.ast.ModuleNode in project intellij-community by JetBrains.

the class GroovyCompilerWrapper method addErrorMessage.

private void addErrorMessage(GroovyRuntimeException exception) {
    ASTNode astNode = exception.getNode();
    ModuleNode module = exception.getModule();
    if (module == null) {
        module = findModule(astNode);
    }
    String moduleName = module == null ? "<no module>" : module.getDescription();
    int lineNumber = astNode == null ? -1 : astNode.getLineNumber();
    int columnNumber = astNode == null ? -1 : astNode.getColumnNumber();
    collector.add(new CompilerMessage(GroovyCompilerMessageCategories.ERROR, getExceptionMessage(exception), moduleName, lineNumber, columnNumber));
}
Also used : ASTNode(org.codehaus.groovy.ast.ASTNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 8 with ModuleNode

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

the class GradleResolveVisitor method visitClass.

public void visitClass(ClassNode node) {
    ClassNode oldNode = currentClass;
    if (node instanceof InnerClassNode) {
        if (Modifier.isStatic(node.getModifiers())) {
            genericParameterNames = new HashMap<String, GenericsType>();
        }
    } else {
        genericParameterNames = new HashMap<String, GenericsType>();
    }
    currentClass = node;
    resolveGenericsHeader(node.getGenericsTypes());
    ModuleNode module = node.getModule();
    if (!module.hasImportsResolved()) {
        for (ImportNode importNode : module.getImports()) {
            currImportNode = importNode;
            ClassNode type = importNode.getType();
            if (resolve(type, false, false, true)) {
                currImportNode = null;
                continue;
            }
            currImportNode = null;
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticStarImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, false, false, true)) {
                continue;
            }
            // Try with the node's package too. If still not found, revert to original type name.
            if (type.getPackageName() == null && node.getPackageName() != null) {
                String oldTypeName = type.getName();
                type.setName(node.getPackageName() + "." + oldTypeName);
                if (resolve(type, false, false, true)) {
                    continue;
                }
                type.setName(oldTypeName);
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, true, true, true)) {
                continue;
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        for (ImportNode importNode : module.getStaticStarImports().values()) {
            ClassNode type = importNode.getType();
            if (resolve(type, true, true, true)) {
                continue;
            }
            addError("unable to resolve class " + type.getName(), type);
        }
        module.setImportsResolved(true);
    }
    ClassNode sn = node.getUnresolvedSuperClass();
    if (sn != null) {
        resolveOrFail(sn, node, true);
    }
    for (ClassNode anInterface : node.getInterfaces()) {
        resolveOrFail(anInterface, node, true);
    }
    checkCyclicInheritence(node, node.getUnresolvedSuperClass(), node.getInterfaces());
    super.visitClass(node);
    currentClass = oldNode;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType) ImportNode(org.codehaus.groovy.ast.ImportNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 9 with ModuleNode

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

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
        protected 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 10 with ModuleNode

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

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)

Aggregations

ModuleNode (org.codehaus.groovy.ast.ModuleNode)20 ClassNode (org.codehaus.groovy.ast.ClassNode)12 ImportNode (org.codehaus.groovy.ast.ImportNode)9 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 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)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