Search in sources :

Example 11 with GenericsType

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

the class DelegateASTTransformation method genericPlaceholderNames.

private static List<String> genericPlaceholderNames(MethodNode candidate) {
    GenericsType[] candidateGenericsTypes = candidate.getGenericsTypes();
    List<String> names = new ArrayList<String>();
    if (candidateGenericsTypes != null) {
        for (GenericsType gt : candidateGenericsTypes) {
            names.add(gt.getName());
        }
    }
    return names;
}
Also used : GenericsType(org.codehaus.groovy.ast.GenericsType) ArrayList(java.util.ArrayList)

Example 12 with GenericsType

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

the class AntlrParserPlugin method annotationDef.

protected void annotationDef(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, "Annotation Definition", modifiers, Opcodes.ACC_SYNCHRONIZED, "synchronized");
        node = node.getNextSibling();
    }
    modifiers |= Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE | Opcodes.ACC_ANNOTATION;
    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();
    }
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    classNode = new ClassNode(dot(getPackageName(), name), modifiers, superClass, interfaces, null);
    classNode.setSyntheticPublic(syntheticPublic);
    classNode.addAnnotations(annotations);
    classNode.setGenericsTypes(genericsType);
    classNode.addInterface(ClassHelper.Annotation_TYPE);
    configureAST(classNode, classDef);
    assertNodeType(OBJBLOCK, node);
    objectBlock(node);
    output.addClass(classNode);
    classNode = null;
}
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)

Example 13 with GenericsType

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

the class AntlrParserPlugin method makeGenericsArgumentType.

private GenericsType makeGenericsArgumentType(AST typeArgument) {
    GenericsType gt;
    AST rootNode = typeArgument.getFirstChild();
    if (isType(WILDCARD_TYPE, rootNode)) {
        ClassNode base = ClassHelper.makeWithoutCaching("?");
        if (rootNode.getNextSibling() != null) {
            int boundType = getBoundType(rootNode.getNextSibling());
            ClassNode[] gts = makeGenericsBounds(rootNode, boundType);
            if (boundType == TYPE_UPPER_BOUNDS) {
                gt = new GenericsType(base, gts, null);
            } else {
                gt = new GenericsType(base, null, gts[0]);
            }
        } else {
            gt = new GenericsType(base, null, null);
        }
        gt.setName("?");
        gt.setWildcard(true);
    } else {
        ClassNode argument = makeTypeWithArguments(rootNode);
        gt = new GenericsType(argument);
    }
    configureAST(gt, typeArgument);
    return gt;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 14 with GenericsType

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

the class AntlrParserPlugin method methodDef.

protected void methodDef(AST methodDef) {
    MethodNode oldNode = methodNode;
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = methodDef.getFirstChild();
    GenericsType[] generics = null;
    if (isType(TYPE_PARAMETERS, node)) {
        generics = makeGenericsType(node);
        node = node.getNextSibling();
    }
    int modifiers = Opcodes.ACC_PUBLIC;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        checkNoInvalidModifier(methodDef, "Method", modifiers, Opcodes.ACC_VOLATILE, "volatile");
        node = node.getNextSibling();
    }
    if (isAnInterface()) {
        modifiers |= Opcodes.ACC_ABSTRACT;
    }
    ClassNode returnType = null;
    if (isType(TYPE, node)) {
        returnType = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    String name = identifier(node);
    if (classNode != null && !classNode.isAnnotationDefinition()) {
        if (classNode.getNameWithoutPackage().equals(name)) {
            if (isAnInterface()) {
                throw new ASTRuntimeException(methodDef, "Constructor not permitted within an interface.");
            }
            throw new ASTRuntimeException(methodDef, "Invalid constructor format. Remove '" + returnType.getName() + "' as the return type if you want a constructor, or use a different name if you want a method.");
        }
    }
    node = node.getNextSibling();
    Parameter[] parameters = Parameter.EMPTY_ARRAY;
    ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
    if (classNode == null || !classNode.isAnnotationDefinition()) {
        assertNodeType(PARAMETERS, node);
        parameters = parameters(node);
        if (parameters == null)
            parameters = Parameter.EMPTY_ARRAY;
        node = node.getNextSibling();
        if (isType(LITERAL_throws, node)) {
            AST throwsNode = node.getFirstChild();
            List<ClassNode> exceptionList = new ArrayList<ClassNode>();
            throwsList(throwsNode, exceptionList);
            exceptions = exceptionList.toArray(exceptions);
            node = node.getNextSibling();
        }
    }
    boolean hasAnnotationDefault = false;
    Statement code = null;
    boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
    modifiers &= ~Opcodes.ACC_SYNTHETIC;
    methodNode = new MethodNode(name, modifiers, returnType, parameters, exceptions, code);
    if ((modifiers & Opcodes.ACC_ABSTRACT) == 0) {
        if (node == null) {
            throw new ASTRuntimeException(methodDef, "You defined a method without body. Try adding a body, or declare it abstract.");
        }
        assertNodeType(SLIST, node);
        code = statementList(node);
    } else if (node != null && classNode.isAnnotationDefinition()) {
        code = statement(node);
        hasAnnotationDefault = true;
    } else if ((modifiers & Opcodes.ACC_ABSTRACT) > 0) {
        if (node != null) {
            throw new ASTRuntimeException(methodDef, "Abstract methods do not define a body.");
        }
    }
    methodNode.setCode(code);
    methodNode.addAnnotations(annotations);
    methodNode.setGenericsTypes(generics);
    methodNode.setAnnotationDefault(hasAnnotationDefault);
    methodNode.setSyntheticPublic(syntheticPublic);
    configureAST(methodNode, methodDef);
    if (classNode != null) {
        classNode.addMethod(methodNode);
    } else {
        output.addMethod(methodNode);
    }
    methodNode = oldNode;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ArrayList(java.util.ArrayList) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) GenericsType(org.codehaus.groovy.ast.GenericsType) Parameter(org.codehaus.groovy.ast.Parameter)

Example 15 with GenericsType

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

the class InitializerStrategy method createBuilderMethodForField.

private MethodNode createBuilderMethodForField(ClassNode builder, List<FieldNode> fields, String prefix, int fieldPos) {
    String fieldName = fields.get(fieldPos).getName();
    String setterName = getSetterName(prefix, fieldName);
    GenericsType[] gtypes = new GenericsType[fields.size()];
    List<Expression> argList = new ArrayList<Expression>();
    for (int i = 0; i < fields.size(); i++) {
        gtypes[i] = i == fieldPos ? new GenericsType(ClassHelper.make(SET.class)) : makePlaceholder(i);
        argList.add(i == fieldPos ? propX(varX("this"), constX(fieldName)) : varX(fields.get(i).getName()));
    }
    ClassNode returnType = makeClassSafeWithGenerics(builder, gtypes);
    FieldNode fNode = fields.get(fieldPos);
    Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
    extractSuperClassGenerics(fNode.getType(), builder, genericsSpec);
    ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
    return new MethodNode(setterName, ACC_PUBLIC, returnType, params(param(correctedType, fieldName)), NO_EXCEPTIONS, block(stmt(assignX(propX(varX("this"), constX(fieldName)), varX(fieldName, correctedType))), returnS(ctorX(returnType, args(argList)))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Expression(org.codehaus.groovy.ast.expr.Expression) GenericsType(org.codehaus.groovy.ast.GenericsType) ArrayList(java.util.ArrayList)

Aggregations

GenericsType (org.codehaus.groovy.ast.GenericsType)165 ClassNode (org.codehaus.groovy.ast.ClassNode)143 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)71 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)52 Parameter (org.codehaus.groovy.ast.Parameter)20 ClosureSignatureHint (groovy.transform.stc.ClosureSignatureHint)19 MethodNode (org.codehaus.groovy.ast.MethodNode)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