Search in sources :

Example 71 with GenericsType

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

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

the class GenericsUtils method extractSuperClassGenerics.

private static void extractSuperClassGenerics(GenericsType[] usage, GenericsType[] declaration, Map<String, ClassNode> spec) {
    // if declaration does not provide generics, there is no connection to make 
    if (usage == null || declaration == null || declaration.length == 0)
        return;
    if (usage.length != declaration.length)
        return;
    // both have generics
    for (int i = 0; i < usage.length; i++) {
        GenericsType ui = usage[i];
        GenericsType di = declaration[i];
        if (di.isPlaceholder()) {
            spec.put(di.getName(), ui.getType());
        } else if (di.isWildcard()) {
            if (ui.isWildcard()) {
                extractSuperClassGenerics(ui.getLowerBound(), di.getLowerBound(), spec);
                extractSuperClassGenerics(ui.getUpperBounds(), di.getUpperBounds(), spec);
            } else {
                ClassNode cu = ui.getType();
                extractSuperClassGenerics(cu, di.getLowerBound(), spec);
                ClassNode[] upperBounds = di.getUpperBounds();
                if (upperBounds != null) {
                    for (ClassNode cn : upperBounds) {
                        extractSuperClassGenerics(cu, cn, spec);
                    }
                }
            }
        } else {
            extractSuperClassGenerics(ui.getType(), di.getType(), spec);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 73 with GenericsType

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

the class TypeSignatureParser method createWildcard.

private static GenericsType createWildcard(ClassNode[] upper, ClassNode lower) {
    ClassNode base = ClassHelper.makeWithoutCaching("?");
    base.setRedirect(ClassHelper.OBJECT_TYPE);
    GenericsType t = new GenericsType(base, upper, lower);
    t.setWildcard(true);
    return t;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 74 with GenericsType

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

the class SignatureCodecVersion1 method doEncode.

private void doEncode(final ClassNode node, DataOutputStream dos) throws IOException {
    dos.writeUTF(node.getClass().getSimpleName());
    if (node instanceof UnionTypeClassNode) {
        UnionTypeClassNode union = (UnionTypeClassNode) node;
        ClassNode[] delegates = union.getDelegates();
        dos.writeInt(delegates.length);
        for (ClassNode delegate : delegates) {
            doEncode(delegate, dos);
        }
        return;
    } else if (node instanceof WideningCategories.LowestUpperBoundClassNode) {
        WideningCategories.LowestUpperBoundClassNode lub = (WideningCategories.LowestUpperBoundClassNode) node;
        dos.writeUTF(lub.getLubName());
        doEncode(lub.getUnresolvedSuperClass(), dos);
        ClassNode[] interfaces = lub.getInterfaces();
        if (interfaces == null) {
            dos.writeInt(-1);
        } else {
            dos.writeInt(interfaces.length);
            for (ClassNode anInterface : interfaces) {
                doEncode(anInterface, dos);
            }
        }
        return;
    }
    if (node.isArray()) {
        dos.writeBoolean(true);
        doEncode(node.getComponentType(), dos);
    } else {
        dos.writeBoolean(false);
        dos.writeUTF(BytecodeHelper.getTypeDescription(node));
        dos.writeBoolean(node.isUsingGenerics());
        GenericsType[] genericsTypes = node.getGenericsTypes();
        if (genericsTypes == null) {
            dos.writeInt(-1);
        } else {
            dos.writeInt(genericsTypes.length);
            for (GenericsType type : genericsTypes) {
                dos.writeBoolean(type.isPlaceholder());
                dos.writeBoolean(type.isWildcard());
                doEncode(type.getType(), dos);
                ClassNode lb = type.getLowerBound();
                if (lb == null) {
                    dos.writeBoolean(false);
                } else {
                    dos.writeBoolean(true);
                    doEncode(lb, dos);
                }
                ClassNode[] upperBounds = type.getUpperBounds();
                if (upperBounds == null) {
                    dos.writeInt(-1);
                } else {
                    dos.writeInt(upperBounds.length);
                    for (ClassNode bound : upperBounds) {
                        doEncode(bound, dos);
                    }
                }
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) WideningCategories(org.codehaus.groovy.ast.tools.WideningCategories) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 75 with GenericsType

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

the class GradleResolveVisitor method visitConstructorOrMethod.

protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor) {
    VariableScope oldScope = currentScope;
    currentScope = node.getVariableScope();
    Map<String, GenericsType> oldPNames = genericParameterNames;
    genericParameterNames = new HashMap<String, GenericsType>(genericParameterNames);
    resolveGenericsHeader(node.getGenericsTypes());
    Parameter[] paras = node.getParameters();
    for (Parameter p : paras) {
        p.setInitialExpression(transform(p.getInitialExpression()));
        resolveOrFail(p.getType(), p.getType());
        visitAnnotations(p);
    }
    ClassNode[] exceptions = node.getExceptions();
    for (ClassNode t : exceptions) {
        resolveOrFail(t, node);
    }
    resolveOrFail(node.getReturnType(), node);
    MethodNode oldCurrentMethod = currentMethod;
    currentMethod = node;
    super.visitConstructorOrMethod(node, isConstructor);
    currentMethod = oldCurrentMethod;
    genericParameterNames = oldPNames;
    currentScope = oldScope;
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

Aggregations

GenericsType (org.codehaus.groovy.ast.GenericsType)171 ClassNode (org.codehaus.groovy.ast.ClassNode)148 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)76 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)52 Parameter (org.codehaus.groovy.ast.Parameter)21 MethodNode (org.codehaus.groovy.ast.MethodNode)20 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)19 LinkedList (java.util.LinkedList)18 HashMap (java.util.HashMap)17 ArrayList (java.util.ArrayList)15 LinkedHashMap (java.util.LinkedHashMap)9 AST (antlr.collections.AST)8 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)8 ListHashMap (org.codehaus.groovy.util.ListHashMap)8 Map (java.util.Map)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 EnumConstantClassNode (org.codehaus.groovy.ast.EnumConstantClassNode)6 FieldNode (org.codehaus.groovy.ast.FieldNode)6 GroovyBugError (org.codehaus.groovy.GroovyBugError)5 DynamicVariable (org.codehaus.groovy.ast.DynamicVariable)4