Search in sources :

Example 76 with FieldNode

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

the class AntlrParserPlugin method enumConstantDef.

protected void enumConstantDef(AST node) {
    enumConstantBeingDef = true;
    assertNodeType(ENUM_CONSTANT_DEF, node);
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST element = node.getFirstChild();
    if (isType(ANNOTATIONS, element)) {
        processAnnotations(annotations, element);
        element = element.getNextSibling();
    }
    String identifier = identifier(element);
    Expression init = null;
    element = element.getNextSibling();
    if (element != null) {
        init = expression(element);
        ClassNode innerClass;
        if (element.getNextSibling() == null) {
            innerClass = getAnonymousInnerClassNode(init);
            if (innerClass != null) {
                init = null;
            }
        } else {
            element = element.getNextSibling();
            Expression next = expression(element);
            innerClass = getAnonymousInnerClassNode(next);
        }
        if (innerClass != null) {
            // we have to handle an enum constant with a class overriding
            // a method in which case we need to configure the inner class
            innerClass.setSuperClass(classNode.getPlainNodeReference());
            innerClass.setModifiers(classNode.getModifiers() | Opcodes.ACC_FINAL);
            // we use a ClassExpression for transportation to EnumVisitor
            Expression inner = new ClassExpression(innerClass);
            if (init == null) {
                ListExpression le = new ListExpression();
                le.addExpression(inner);
                init = le;
            } else {
                if (init instanceof ListExpression) {
                    ((ListExpression) init).addExpression(inner);
                } else {
                    ListExpression le = new ListExpression();
                    le.addExpression(init);
                    le.addExpression(inner);
                    init = le;
                }
            }
            // and remove the final modifier from classNode to allow the sub class
            classNode.setModifiers(classNode.getModifiers() & ~Opcodes.ACC_FINAL);
        } else if (isType(ELIST, element)) {
            if (init instanceof ListExpression && !((ListExpression) init).isWrapped()) {
                ListExpression le = new ListExpression();
                le.addExpression(init);
                init = le;
            }
        }
    }
    FieldNode enumField = EnumHelper.addEnumConstant(classNode, identifier, init);
    enumField.addAnnotations(annotations);
    configureAST(enumField, node);
    enumConstantBeingDef = false;
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AST(antlr.collections.AST) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ArrayList(java.util.ArrayList)

Example 77 with FieldNode

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

the class InitializerStrategy method initializeFields.

private static void initializeFields(List<FieldNode> fields, BlockStatement body, boolean useSetters) {
    for (FieldNode field : fields) {
        String name = field.getName();
        body.addStatement(stmt(useSetters && !field.isFinal() ? callThisX(getSetterName(name), varX(param(field.getType(), name))) : assignX(propX(varX("this"), field.getName()), varX(param(field.getType(), name)))));
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 78 with FieldNode

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

the class InitializerStrategy method createBuilderForAnnotatedMethod.

private void createBuilderForAnnotatedMethod(BuilderASTTransformation transform, MethodNode mNode, AnnotationNode anno, boolean useSetters) {
    if (transform.getMemberValue(anno, "includes") != null || transform.getMemberValue(anno, "excludes") != null) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: includes/excludes only allowed on classes", anno);
    }
    if (mNode instanceof ConstructorNode) {
        mNode.setModifiers(ACC_PRIVATE | ACC_SYNTHETIC);
    } else {
        if ((mNode.getModifiers() & ACC_STATIC) == 0) {
            transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: method builders only allowed on static methods", anno);
        }
        mNode.setModifiers(ACC_PRIVATE | ACC_SYNTHETIC | ACC_STATIC);
    }
    ClassNode buildee = mNode.getDeclaringClass();
    Parameter[] parameters = mNode.getParameters();
    if (parameters.length == 0) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: at least one parameter is required for this strategy", anno);
    }
    ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee, anno), parameters.length);
    List<FieldNode> convertedFields = convertParamsToFields(builder, parameters);
    buildCommon(buildee, anno, convertedFields, builder);
    if (mNode instanceof ConstructorNode) {
        createBuildeeConstructors(transform, buildee, builder, convertedFields, false, useSetters);
    } else {
        createBuildeeMethods(buildee, mNode, builder, convertedFields);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Example 79 with FieldNode

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

the class InitializerStrategy method createInitializerConstructor.

private static ConstructorNode createInitializerConstructor(ClassNode buildee, ClassNode builder, List<FieldNode> fields) {
    ClassNode paramType = makeClassSafeWithGenerics(builder, setGenTypes(fields.size()));
    List<Expression> argsList = new ArrayList<Expression>();
    Parameter initParam = param(paramType, "initializer");
    for (FieldNode fieldNode : fields) {
        argsList.add(propX(varX(initParam), fieldNode.getName()));
    }
    return buildee.addConstructor(ACC_PUBLIC, params(param(paramType, "initializer")), NO_EXCEPTIONS, block(ctorThisS(args(argsList))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) Expression(org.codehaus.groovy.ast.expr.Expression) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter)

Example 80 with FieldNode

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

the class InitializerStrategy method getParams.

private static Parameter[] getParams(List<FieldNode> fields, ClassNode cNode) {
    Parameter[] parameters = new Parameter[fields.size()];
    for (int i = 0; i < parameters.length; i++) {
        FieldNode fNode = fields.get(i);
        Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
        extractSuperClassGenerics(fNode.getType(), cNode, genericsSpec);
        ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
        parameters[i] = new Parameter(correctedType, fNode.getName());
    }
    return parameters;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

FieldNode (org.codehaus.groovy.ast.FieldNode)204 ClassNode (org.codehaus.groovy.ast.ClassNode)126 Expression (org.codehaus.groovy.ast.expr.Expression)58 MethodNode (org.codehaus.groovy.ast.MethodNode)57 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)54 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Parameter (org.codehaus.groovy.ast.Parameter)49 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)43 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)43 ArrayList (java.util.ArrayList)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)33 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)31 PropertyNode (org.codehaus.groovy.ast.PropertyNode)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)15