Search in sources :

Example 11 with FieldNode

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

the class MemoizedASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    init(nodes, source);
    AnnotationNode annotationNode = (AnnotationNode) nodes[0];
    AnnotatedNode annotatedNode = (AnnotatedNode) nodes[1];
    if (MY_TYPE.equals(annotationNode.getClassNode()) && annotatedNode instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) annotatedNode;
        if (methodNode.isAbstract()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for abstract methods.", methodNode);
            return;
        }
        if (methodNode.isVoidMethod()) {
            addError("Annotation " + MY_TYPE_NAME + " cannot be used for void methods.", methodNode);
            return;
        }
        ClassNode ownerClassNode = methodNode.getDeclaringClass();
        MethodNode delegatingMethod = buildDelegatingMethod(methodNode, ownerClassNode);
        ownerClassNode.addMethod(delegatingMethod);
        int modifiers = FieldNode.ACC_PRIVATE | FieldNode.ACC_FINAL;
        if (methodNode.isStatic()) {
            modifiers = modifiers | FieldNode.ACC_STATIC;
        }
        int protectedCacheSize = getMemberIntValue(annotationNode, PROTECTED_CACHE_SIZE_NAME);
        int maxCacheSize = getMemberIntValue(annotationNode, MAX_CACHE_SIZE_NAME);
        MethodCallExpression memoizeClosureCallExpression = buildMemoizeClosureCallExpression(delegatingMethod, protectedCacheSize, maxCacheSize);
        String memoizedClosureFieldName = buildUniqueName(ownerClassNode, CLOSURE_LABEL, methodNode);
        FieldNode memoizedClosureField = new FieldNode(memoizedClosureFieldName, modifiers, newClass(ClassHelper.CLOSURE_TYPE), null, memoizeClosureCallExpression);
        ownerClassNode.addField(memoizedClosureField);
        BlockStatement newCode = new BlockStatement();
        MethodCallExpression closureCallExpression = callX(fieldX(memoizedClosureField), CLOSURE_CALL_METHOD_NAME, args(methodNode.getParameters()));
        closureCallExpression.setImplicitThis(false);
        newCode.addStatement(returnS(closureCallExpression));
        methodNode.setCode(newCode);
        VariableScopeVisitor visitor = new VariableScopeVisitor(source);
        visitor.visitClass(ownerClassNode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableScopeVisitor(org.codehaus.groovy.classgen.VariableScopeVisitor)

Example 12 with FieldNode

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

the class SingletonASTTransformation method createField.

private void createField(ClassNode classNode, String propertyName, boolean isLazy, boolean isStrict) {
    int modifiers = isLazy ? ACC_PRIVATE | ACC_STATIC | ACC_VOLATILE : ACC_PUBLIC | ACC_FINAL | ACC_STATIC;
    Expression initialValue = isLazy ? null : ctorX(classNode);
    final FieldNode fieldNode = classNode.addField(propertyName, modifiers, newClass(classNode), initialValue);
    createConstructor(classNode, fieldNode, propertyName, isStrict);
    final BlockStatement body = new BlockStatement();
    body.addStatement(isLazy ? lazyBody(classNode, fieldNode) : nonLazyBody(fieldNode));
    classNode.addMethod(getGetterName(propertyName), ACC_STATIC | ACC_PUBLIC, newClass(classNode), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) Expression(org.codehaus.groovy.ast.expr.Expression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 13 with FieldNode

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

the class SourceURIASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(node.getClassNode()))
        return;
    if (parent instanceof DeclarationExpression) {
        setScriptURIOnDeclaration((DeclarationExpression) parent, node);
    } else if (parent instanceof FieldNode) {
        setScriptURIOnField((FieldNode) parent, node);
    } else {
        addError("Expected to find the annotation " + MY_TYPE_NAME + " on an declaration statement.", parent);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression)

Example 14 with FieldNode

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

the class ImmutableASTTransformation method createConstructorStatement.

private Statement createConstructorStatement(ClassNode cNode, PropertyNode pNode, List<String> knownImmutableClasses, List<String> knownImmutables) {
    FieldNode fNode = pNode.getField();
    final ClassNode fieldType = fNode.getType();
    Statement statement = null;
    if (fieldType.isArray() || isOrImplements(fieldType, CLONEABLE_TYPE)) {
        statement = createConstructorStatementArrayOrCloneable(fNode);
    } else if (isKnownImmutableClass(fieldType, knownImmutableClasses) || isKnownImmutable(pNode.getName(), knownImmutables)) {
        statement = createConstructorStatementDefault(fNode);
    } else if (fieldType.isDerivedFrom(DATE_TYPE)) {
        statement = createConstructorStatementDate(fNode);
    } else if (isOrImplements(fieldType, COLLECTION_TYPE) || fieldType.isDerivedFrom(COLLECTION_TYPE) || isOrImplements(fieldType, MAP_TYPE) || fieldType.isDerivedFrom(MAP_TYPE)) {
        statement = createConstructorStatementCollection(fNode);
    } else if (fieldType.isResolved()) {
        addError(createErrorMessage(cNode.getName(), fNode.getName(), fieldType.getName(), "compiling"), fNode);
        statement = EmptyStatement.INSTANCE;
    } else {
        statement = createConstructorStatementGuarded(cNode, fNode);
    }
    return statement;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement)

Example 15 with FieldNode

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

the class ImmutableASTTransformation method createConstructorMapCommon.

private static void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
    final List<FieldNode> fList = cNode.getFields();
    for (FieldNode fNode : fList) {
        // public fields will be rejected elsewhere
        if (fNode.isPublic())
            continue;
        // a property
        if (cNode.getProperty(fNode.getName()) != null)
            continue;
        if (fNode.isFinal() && fNode.isStatic())
            continue;
        // internal field
        if (fNode.getName().contains("$") || fNode.isSynthetic())
            continue;
        if (fNode.isFinal() && fNode.getInitialExpression() != null)
            body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
        body.addStatement(createConstructorStatementDefault(fNode));
    }
    doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, params(new Parameter(HASHMAP_TYPE, "args")), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) Parameter(org.codehaus.groovy.ast.Parameter)

Aggregations

FieldNode (org.codehaus.groovy.ast.FieldNode)203 ClassNode (org.codehaus.groovy.ast.ClassNode)125 MethodNode (org.codehaus.groovy.ast.MethodNode)57 Expression (org.codehaus.groovy.ast.expr.Expression)57 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)53 Parameter (org.codehaus.groovy.ast.Parameter)49 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)49 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)43 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)42 ArrayList (java.util.ArrayList)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)37 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)35 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)32 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)31 PropertyNode (org.codehaus.groovy.ast.PropertyNode)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)26 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)21 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)18 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)15