Search in sources :

Example 16 with ClassNode

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

the class AntlrParserPlugin method innerInterfaceDef.

protected void innerInterfaceDef(AST classDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = classDef.getFirstChild();
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(classDef, "Interface", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE;
    String name = identifier(node);
    node = node.getNextSibling();
    ClassNode superClass = ClassHelper.OBJECT_TYPE;
    GenericsType[] genericsType = null;
    if (isType(TYPE_PARAMETERS, node)) {
        genericsType = makeGenericsType(node);
        node = node.getNextSibling();
    }
    ClassNode[] interfaces = ClassNode.EMPTY_ARRAY;
    if (isType(EXTENDS_CLAUSE, node)) {
        interfaces = interfaces(node);
        node = node.getNextSibling();
    }
    ClassNode outerClass = classNode;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    if (classNode != null) {
        name = classNode.getNameWithoutPackage() + "$" + name;
        String fullName = dot(classNode.getPackageName(), name);
        classNode = new InnerClassNode(classNode, fullName, modifiers, superClass, interfaces, null);
    } else {
        classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    }
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    configureAST(classNode, classDef);
    int oldClassCount = innerClassCounter;
    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);
    classNode = outerClass;
    innerClassCounter = oldClassCount;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList) GenericsType(org.codehaus.groovy.ast.GenericsType) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Example 17 with ClassNode

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

the class AsmReferenceResolver method resolveClassNullable.

public ClassNode resolveClassNullable(String className) {
    ClassNode beingCompiled = unit.getAST().getClass(className);
    if (beingCompiled != null) {
        return beingCompiled;
    }
    ClassNodeResolver.LookupResult lookupResult = resolver.resolveName(className, unit);
    return lookupResult == null ? null : lookupResult.getClassNode();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ClassNodeResolver(org.codehaus.groovy.control.ClassNodeResolver)

Example 18 with ClassNode

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

the class GenericsUtils method correctToGenericsSpecRecurse.

public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions) {
    if (type.isArray()) {
        return correctToGenericsSpecRecurse(genericsSpec, type.getComponentType(), exclusions).makeArray();
    }
    if (type.isGenericsPlaceHolder() && !exclusions.contains(type.getUnresolvedName())) {
        String name = type.getGenericsTypes()[0].getName();
        type = genericsSpec.get(name);
        if (type != null && type.isGenericsPlaceHolder() && type.getGenericsTypes() == null) {
            ClassNode placeholder = ClassHelper.makeWithoutCaching(type.getUnresolvedName());
            placeholder.setGenericsPlaceHolder(true);
            type = makeClassSafeWithGenerics(type, new GenericsType(placeholder));
        }
    }
    if (type == null)
        type = ClassHelper.OBJECT_TYPE;
    GenericsType[] oldgTypes = type.getGenericsTypes();
    GenericsType[] newgTypes = GenericsType.EMPTY_ARRAY;
    if (oldgTypes != null) {
        newgTypes = new GenericsType[oldgTypes.length];
        for (int i = 0; i < newgTypes.length; i++) {
            GenericsType oldgType = oldgTypes[i];
            if (oldgType.isPlaceholder()) {
                if (genericsSpec.get(oldgType.getName()) != null) {
                    newgTypes[i] = new GenericsType(genericsSpec.get(oldgType.getName()));
                } else {
                    newgTypes[i] = new GenericsType(ClassHelper.OBJECT_TYPE);
                }
            } else if (oldgType.isWildcard()) {
                ClassNode oldLower = oldgType.getLowerBound();
                ClassNode lower = oldLower != null ? correctToGenericsSpecRecurse(genericsSpec, oldLower, exclusions) : null;
                ClassNode[] oldUpper = oldgType.getUpperBounds();
                ClassNode[] upper = null;
                if (oldUpper != null) {
                    upper = new ClassNode[oldUpper.length];
                    for (int j = 0; j < oldUpper.length; j++) {
                        upper[j] = correctToGenericsSpecRecurse(genericsSpec, oldUpper[j], exclusions);
                    }
                }
                GenericsType fixed = new GenericsType(oldgType.getType(), upper, lower);
                fixed.setName(oldgType.getName());
                fixed.setWildcard(true);
                newgTypes[i] = fixed;
            } else {
                newgTypes[i] = new GenericsType(correctToGenericsSpecRecurse(genericsSpec, correctToGenericsSpec(genericsSpec, oldgType), exclusions));
            }
        }
    }
    return makeClassSafeWithGenerics(type, newgTypes);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 19 with ClassNode

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

the class GenericsUtils method parseClassNodesFromString.

public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) {
    GroovyLexer lexer = new GroovyLexer(new StringReader("DummyNode<" + option + ">"));
    final GroovyRecognizer rn = GroovyRecognizer.make(lexer);
    try {
        rn.classOrInterfaceType(true);
        final AtomicReference<ClassNode> ref = new AtomicReference<ClassNode>();
        AntlrParserPlugin plugin = new AntlrParserPlugin() {

            @Override
            public ModuleNode buildAST(final SourceUnit sourceUnit, final ClassLoader classLoader, final Reduction cst) throws ParserException {
                ref.set(makeTypeWithArguments(rn.getAST()));
                return null;
            }
        };
        plugin.buildAST(null, null, null);
        ClassNode parsedNode = ref.get();
        // the returned node is DummyNode<Param1, Param2, Param3, ...)
        GenericsType[] parsedNodeGenericsTypes = parsedNode.getGenericsTypes();
        if (parsedNodeGenericsTypes == null) {
            return null;
        }
        ClassNode[] signature = new ClassNode[parsedNodeGenericsTypes.length];
        for (int i = 0; i < parsedNodeGenericsTypes.length; i++) {
            final GenericsType genericsType = parsedNodeGenericsTypes[i];
            signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType());
        }
        return signature;
    } catch (RecognitionException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    } catch (TokenStreamException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    } catch (ParserException e) {
        sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber()));
    }
    return null;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ParserException(org.codehaus.groovy.syntax.ParserException) IncorrectTypeHintException(groovy.transform.stc.IncorrectTypeHintException) AtomicReference(java.util.concurrent.atomic.AtomicReference) SourceUnit(org.codehaus.groovy.control.SourceUnit) TokenStreamException(antlr.TokenStreamException) Reduction(org.codehaus.groovy.syntax.Reduction) AntlrParserPlugin(org.codehaus.groovy.antlr.AntlrParserPlugin) GroovyLexer(org.codehaus.groovy.antlr.parser.GroovyLexer) StringReader(java.io.StringReader) GenericsType(org.codehaus.groovy.ast.GenericsType) GroovyRecognizer(org.codehaus.groovy.antlr.parser.GroovyRecognizer) RecognitionException(antlr.RecognitionException)

Example 20 with ClassNode

use of org.codehaus.groovy.ast.ClassNode 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)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)850 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)329 MethodNode (org.codehaus.groovy.ast.MethodNode)192 GenericsType (org.codehaus.groovy.ast.GenericsType)149 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)140 Parameter (org.codehaus.groovy.ast.Parameter)133 FieldNode (org.codehaus.groovy.ast.FieldNode)125 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)114 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)112 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)107 ArrayList (java.util.ArrayList)95 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)86 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)73 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 LinkedList (java.util.LinkedList)70 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)69 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)59 MethodVisitor (org.objectweb.asm.MethodVisitor)57