Search in sources :

Example 1 with PropertyNode

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

the class AntlrParserPlugin method fieldDef.

protected void fieldDef(AST fieldDef) {
    List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
    AST node = fieldDef.getFirstChild();
    int modifiers = 0;
    if (isType(MODIFIERS, node)) {
        modifiers = modifiers(node, annotations, modifiers);
        node = node.getNextSibling();
    }
    if (classNode.isInterface()) {
        modifiers |= Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
        if ((modifiers & (Opcodes.ACC_PRIVATE | Opcodes.ACC_PROTECTED)) == 0) {
            modifiers |= Opcodes.ACC_PUBLIC;
        }
    }
    ClassNode type = null;
    if (isType(TYPE, node)) {
        type = makeTypeWithArguments(node);
        node = node.getNextSibling();
    }
    String name = identifier(node);
    node = node.getNextSibling();
    Expression initialValue = null;
    if (node != null) {
        assertNodeType(ASSIGN, node);
        initialValue = expression(node.getFirstChild());
    }
    if (classNode.isInterface() && initialValue == null && type != null) {
        initialValue = getDefaultValueForPrimitive(type);
    }
    FieldNode fieldNode = new FieldNode(name, modifiers, type, classNode, initialValue);
    fieldNode.addAnnotations(annotations);
    configureAST(fieldNode, fieldDef);
    if (!hasVisibility(modifiers)) {
        // let's set the modifiers on the field
        int fieldModifiers = 0;
        int flags = Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT | Opcodes.ACC_VOLATILE | Opcodes.ACC_FINAL;
        if (!hasVisibility(modifiers)) {
            modifiers |= Opcodes.ACC_PUBLIC;
            fieldModifiers |= Opcodes.ACC_PRIVATE;
        }
        // let's pass along any other modifiers we need
        fieldModifiers |= (modifiers & flags);
        fieldNode.setModifiers(fieldModifiers);
        fieldNode.setSynthetic(true);
        // in the case that there is already a field, we would
        // like to use that field, instead of the default field
        // for the property
        FieldNode storedNode = classNode.getDeclaredField(fieldNode.getName());
        if (storedNode != null && !classNode.hasProperty(name)) {
            fieldNode = storedNode;
            // we remove it here, because addProperty will add it
            // again and we want to avoid it showing up multiple
            // times in the fields list.
            classNode.getFields().remove(storedNode);
        }
        PropertyNode propertyNode = new PropertyNode(fieldNode, modifiers, null, null);
        configureAST(propertyNode, fieldDef);
        classNode.addProperty(propertyNode);
    } else {
        fieldNode.setModifiers(modifiers);
        // if there is a property of that name, then a field of that
        // name already exists, which means this new field here should
        // be used instead of the field the property originally has.
        PropertyNode pn = classNode.getProperty(name);
        if (pn != null && pn.getField().isSynthetic()) {
            classNode.getFields().remove(pn.getField());
            pn.setField(fieldNode);
        }
        classNode.addField(fieldNode);
    }
}
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) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 2 with PropertyNode

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

the class ClassNodeUtils method hasPossibleStaticProperty.

/**
     * Return true if we have a static accessor
     */
public static boolean hasPossibleStaticProperty(ClassNode candidate, String methodName) {
    // assume explicit static method call checked first so we can assume a simple check here
    if (!methodName.startsWith("get") && !methodName.startsWith("is")) {
        return false;
    }
    String propName = getPropNameForAccessor(methodName);
    PropertyNode pNode = getStaticProperty(candidate, propName);
    return pNode != null && (methodName.startsWith("get") || boolean_TYPE.equals(pNode.getType()));
}
Also used : PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 3 with PropertyNode

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

the class ImmutableASTTransformation method createConstructorOrdered.

private static void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
    final MapExpression argMap = new MapExpression();
    final Parameter[] orderedParams = new Parameter[list.size()];
    int index = 0;
    for (PropertyNode pNode : list) {
        Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
        orderedParams[index++] = param;
        argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
    }
    final BlockStatement orderedBody = new BlockStatement();
    orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 4 with PropertyNode

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

the class ImmutableASTTransformation method createConstructorMap.

private void createConstructorMap(ClassNode cNode, List<PropertyNode> list, List<String> knownImmutableClasses, List<String> knownImmutables) {
    final BlockStatement body = new BlockStatement();
    body.addStatement(ifS(equalsNullX(varX("args")), assignS(varX("args"), new MapExpression())));
    for (PropertyNode pNode : list) {
        body.addStatement(createConstructorStatement(cNode, pNode, knownImmutableClasses, knownImmutables));
    }
    // check for missing properties
    body.addStatement(stmt(callX(SELF_TYPE, "checkPropNames", args("this", "args"))));
    createConstructorMapCommon(cNode, body);
    if (!list.isEmpty()) {
        createNoArgConstructor(cNode);
    }
}
Also used : MapExpression(org.codehaus.groovy.ast.expr.MapExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 5 with PropertyNode

use of org.codehaus.groovy.ast.PropertyNode in project grails-core by grails.

the class AbstractGrailsArtefactTransformer method performInstanceImplementationInjection.

protected void performInstanceImplementationInjection(SourceUnit source, ClassNode classNode, Map<String, ClassNode> genericsPlaceholders, Class instanceImplementation) {
    ClassNode implementationNode;
    final ConstructorCallExpression constructorCallExpression;
    try {
        implementationNode = GrailsASTUtils.replaceGenericsPlaceholders(ClassHelper.make(instanceImplementation), genericsPlaceholders);
        constructorCallExpression = GrailsASTUtils.hasZeroArgsConstructor(implementationNode) ? new ConstructorCallExpression(implementationNode, ZERO_ARGS) : null;
    } catch (Throwable e) {
        // this may well be ok, as we want to be able to compile against, for example, non servlet environments. In this case just bail out.
        return;
    }
    String apiInstanceProperty = INSTANCE_PREFIX + instanceImplementation.getSimpleName();
    Expression apiInstance = new VariableExpression(apiInstanceProperty, implementationNode);
    if (requiresStaticLookupMethod()) {
        final String lookupMethodName = CURRENT_PREFIX + instanceImplementation.getSimpleName();
        MethodNode lookupMethod = createStaticLookupMethod(classNode, implementationNode, apiInstanceProperty, lookupMethodName);
        apiInstance = new MethodCallExpression(new ClassExpression(classNode), lookupMethodName, ZERO_ARGS);
        ((MethodCallExpression) apiInstance).setMethodTarget(lookupMethod);
    } else if (requiresAutowiring()) {
        PropertyNode propertyNode = new PropertyNode(apiInstanceProperty, Modifier.PUBLIC, implementationNode, classNode, constructorCallExpression, null, null);
        propertyNode.addAnnotation(AUTO_WIRED_ANNOTATION);
        if (getMarkerAnnotation() != null) {
            propertyNode.addAnnotation(getMarkerAnnotation());
        }
        classNode.addProperty(propertyNode);
    } else {
        FieldNode fieldNode = classNode.getField(apiInstanceProperty);
        if (fieldNode == null || (Modifier.isPrivate(fieldNode.getModifiers()) && !fieldNode.getDeclaringClass().equals(classNode))) {
            fieldNode = new FieldNode(apiInstanceProperty, PRIVATE_STATIC_MODIFIER, implementationNode, classNode, constructorCallExpression);
            classNode.addField(fieldNode);
        }
    }
    while (!implementationNode.equals(AbstractGrailsArtefactTransformer.OBJECT_CLASS)) {
        List<MethodNode> declaredMethods = implementationNode.getMethods();
        for (MethodNode declaredMethod : declaredMethods) {
            if (GrailsASTUtils.isConstructorMethod(declaredMethod)) {
                GrailsASTUtils.addDelegateConstructor(classNode, declaredMethod, genericsPlaceholders);
            } else if (isCandidateInstanceMethod(classNode, declaredMethod)) {
                addDelegateInstanceMethod(classNode, apiInstance, declaredMethod, getMarkerAnnotation(), genericsPlaceholders);
            }
        }
        implementationNode = implementationNode.getSuperClass();
    }
    performInjectionInternal(apiInstanceProperty, source, classNode);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyNode(org.codehaus.groovy.ast.PropertyNode) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression)

Aggregations

PropertyNode (org.codehaus.groovy.ast.PropertyNode)105 ClassNode (org.codehaus.groovy.ast.ClassNode)50 FieldNode (org.codehaus.groovy.ast.FieldNode)44 ArrayList (java.util.ArrayList)41 MethodNode (org.codehaus.groovy.ast.MethodNode)28 Expression (org.codehaus.groovy.ast.expr.Expression)27 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)24 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)23 Parameter (org.codehaus.groovy.ast.Parameter)22 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)19 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)16 HashSet (java.util.HashSet)14 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)14 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)14 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)14 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)13 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)13 Statement (org.codehaus.groovy.ast.stmt.Statement)12 ConstructorNode (org.codehaus.groovy.ast.ConstructorNode)11