Search in sources :

Example 81 with FieldNode

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

the class InitializerStrategy method createBuilderForAnnotatedClass.

private void createBuilderForAnnotatedClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode anno, boolean useSetters, boolean allNames) {
    List<String> excludes = new ArrayList<String>();
    List<String> includes = new ArrayList<String>();
    includes.add(Undefined.STRING);
    if (!getIncludeExclude(transform, anno, buildee, excludes, includes))
        return;
    if (includes.size() == 1 && Undefined.isUndefined(includes.get(0)))
        includes = null;
    List<FieldNode> fields = getFields(transform, anno, buildee);
    List<FieldNode> filteredFields = filterFields(fields, includes, excludes, allNames);
    if (filteredFields.isEmpty()) {
        transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: at least one property is required for this strategy", anno);
    }
    ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee, anno), filteredFields.size());
    addFields(buildee, filteredFields, builder);
    buildCommon(buildee, anno, filteredFields, builder);
    createBuildeeConstructors(transform, buildee, builder, filteredFields, true, useSetters);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ArrayList(java.util.ArrayList)

Example 82 with FieldNode

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

the class InitializerStrategy method createFieldCopy.

private static FieldNode createFieldCopy(ClassNode buildee, FieldNode fNode) {
    Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
    extractSuperClassGenerics(fNode.getType(), buildee, genericsSpec);
    ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
    return new FieldNode(fNode.getName(), fNode.getModifiers(), correctedType, buildee, DEFAULT_INITIAL_VALUE);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 83 with FieldNode

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

Example 84 with FieldNode

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

the class AnnotationVisitor method validateEnumConstant.

private boolean validateEnumConstant(Expression exp) {
    if (exp instanceof PropertyExpression) {
        PropertyExpression pe = (PropertyExpression) exp;
        String name = pe.getPropertyAsString();
        if (pe.getObjectExpression() instanceof ClassExpression && name != null) {
            ClassExpression ce = (ClassExpression) pe.getObjectExpression();
            ClassNode type = ce.getType();
            if (type.isEnum()) {
                boolean ok = false;
                try {
                    FieldNode enumField = type.getDeclaredField(name);
                    ok = enumField != null && enumField.getType().equals(type);
                } catch (Exception ex) {
                // ignore
                }
                if (!ok) {
                    addError("No enum const " + type.getName() + "." + name, pe);
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) SyntaxException(org.codehaus.groovy.syntax.SyntaxException)

Example 85 with FieldNode

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

the class GeneralUtils method getInstanceNonPropertyFieldNames.

public static List<String> getInstanceNonPropertyFieldNames(ClassNode cNode) {
    List<FieldNode> fList = getInstanceNonPropertyFields(cNode);
    List<String> result = new ArrayList<String>(fList.size());
    for (FieldNode fNode : fList) {
        result.add(fNode.getName());
    }
    return result;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ArrayList(java.util.ArrayList)

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