Search in sources :

Example 26 with FieldNode

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

the class EnumVisitor method completeEnum.

private void completeEnum(ClassNode enumClass) {
    boolean isAic = isAnonymousInnerClass(enumClass);
    // create MIN_VALUE and MAX_VALUE fields
    FieldNode minValue = null, maxValue = null, values = null;
    if (!isAic) {
        ClassNode enumRef = enumClass.getPlainNodeReference();
        // create values field
        values = new FieldNode("$VALUES", PRIVATE_FS | Opcodes.ACC_SYNTHETIC, enumRef.makeArray(), enumClass, null);
        values.setSynthetic(true);
        addMethods(enumClass, values);
        checkForAbstractMethods(enumClass);
        // create MIN_VALUE and MAX_VALUE fields
        minValue = new FieldNode("MIN_VALUE", PUBLIC_FS, enumRef, enumClass, null);
        maxValue = new FieldNode("MAX_VALUE", PUBLIC_FS, enumRef, enumClass, null);
    }
    addInit(enumClass, minValue, maxValue, values, isAic);
}
Also used : EnumConstantClassNode(org.codehaus.groovy.ast.EnumConstantClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 27 with FieldNode

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

the class TraitReceiverTransformer method createFieldHelperCall.

private Expression createFieldHelperCall(Expression exp, ClassNode weavedType, String propName) {
    String method = Traits.helperGetterName(new FieldNode(propName, 0, ClassHelper.OBJECT_TYPE, weavedType, null));
    MethodCallExpression mce = new MethodCallExpression(createFieldHelperReceiver(), method, ArgumentListExpression.EMPTY_ARGUMENTS);
    mce.setSourcePosition(exp);
    mce.setImplicitThis(false);
    return mce;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression)

Example 28 with FieldNode

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

the class TraitReceiverTransformer method transformBinaryExpression.

private Expression transformBinaryExpression(final BinaryExpression exp, final ClassNode weavedType) {
    Expression leftExpression = exp.getLeftExpression();
    Expression rightExpression = exp.getRightExpression();
    Token operation = exp.getOperation();
    if (operation.getText().equals("=")) {
        String leftFieldName = null;
        // it's an assignment
        if (leftExpression instanceof VariableExpression && ((VariableExpression) leftExpression).getAccessedVariable() instanceof FieldNode) {
            leftFieldName = ((VariableExpression) leftExpression).getAccessedVariable().getName();
        } else if (leftExpression instanceof FieldExpression) {
            leftFieldName = ((FieldExpression) leftExpression).getFieldName();
        } else if (leftExpression instanceof PropertyExpression && (((PropertyExpression) leftExpression).isImplicitThis() || "this".equals(((PropertyExpression) leftExpression).getObjectExpression().getText()))) {
            leftFieldName = ((PropertyExpression) leftExpression).getPropertyAsString();
            FieldNode fn = tryGetFieldNode(weavedType, leftFieldName);
            if (fieldHelper == null || fn == null && !fieldHelper.hasPossibleMethod(Traits.helperSetterName(new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null)), rightExpression)) {
                return createAssignmentToField(rightExpression, operation, leftFieldName);
            }
        }
        if (leftFieldName != null) {
            FieldNode fn = weavedType.getDeclaredField(leftFieldName);
            FieldNode staticField = tryGetFieldNode(weavedType, leftFieldName);
            if (fn == null) {
                fn = new FieldNode(leftFieldName, 0, ClassHelper.OBJECT_TYPE, weavedType, null);
            }
            Expression receiver = createFieldHelperReceiver();
            boolean isStatic = staticField != null && staticField.isStatic();
            if (fn.isStatic()) {
                // DO NOT USE isStatic variable here!
                receiver = new PropertyExpression(receiver, "class");
            }
            String method = Traits.helperSetterName(fn);
            MethodCallExpression mce = new MethodCallExpression(receiver, method, new ArgumentListExpression(super.transform(rightExpression)));
            mce.setSourcePosition(exp);
            mce.setImplicitThis(false);
            markDynamicCall(mce, staticField, isStatic);
            return mce;
        }
    }
    Expression leftTransform = transform(leftExpression);
    Expression rightTransform = transform(rightExpression);
    Expression ret = exp instanceof DeclarationExpression ? new DeclarationExpression(leftTransform, operation, rightTransform) : new BinaryExpression(leftTransform, operation, rightTransform);
    ret.setSourcePosition(exp);
    ret.copyNodeMetaData(exp);
    return ret;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) Token(org.codehaus.groovy.syntax.Token) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression)

Example 29 with FieldNode

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

the class BindableASTTransformation method needsPropertyChangeSupport.

/**
     * Snoops through the declaring class and all parents looking for methods
     * <code>void addPropertyChangeListener(PropertyChangeListener)</code>,
     * <code>void removePropertyChangeListener(PropertyChangeListener)</code>, and
     * <code>void firePropertyChange(String, Object, Object)</code>. If any are defined all
     * must be defined or a compilation error results.
     *
     * @param declaringClass the class to search
     * @param sourceUnit the source unit, for error reporting. {@code @NotNull}.
     * @return true if property change support should be added
     */
protected boolean needsPropertyChangeSupport(ClassNode declaringClass, SourceUnit sourceUnit) {
    boolean foundAdd = false, foundRemove = false, foundFire = false;
    ClassNode consideredClass = declaringClass;
    while (consideredClass != null) {
        for (MethodNode method : consideredClass.getMethods()) {
            // just check length, MOP will match it up
            foundAdd = foundAdd || method.getName().equals("addPropertyChangeListener") && method.getParameters().length == 1;
            foundRemove = foundRemove || method.getName().equals("removePropertyChangeListener") && method.getParameters().length == 1;
            foundFire = foundFire || method.getName().equals("firePropertyChange") && method.getParameters().length == 3;
            if (foundAdd && foundRemove && foundFire) {
                return false;
            }
        }
        consideredClass = consideredClass.getSuperClass();
    }
    // check if a super class has @Bindable annotations
    consideredClass = declaringClass.getSuperClass();
    while (consideredClass != null) {
        if (hasBindableAnnotation(consideredClass))
            return false;
        for (FieldNode field : consideredClass.getFields()) {
            if (hasBindableAnnotation(field))
                return false;
        }
        consideredClass = consideredClass.getSuperClass();
    }
    if (foundAdd || foundRemove || foundFire) {
        sourceUnit.getErrorCollector().addErrorAndContinue(new SimpleMessage("@Bindable cannot be processed on " + declaringClass.getName() + " because some but not all of addPropertyChangeListener, removePropertyChange, and firePropertyChange were declared in the current or super classes.", sourceUnit));
        return false;
    }
    return true;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage)

Example 30 with FieldNode

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

the class BindableASTTransformation method visit.

/**
     * Handles the bulk of the processing, mostly delegating to other methods.
     *
     * @param nodes   the ast nodes
     * @param source  the source unit for the nodes
     */
public void visit(ASTNode[] nodes, SourceUnit source) {
    if (!(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: wrong types: $node.class / $parent.class");
    }
    AnnotationNode node = (AnnotationNode) nodes[0];
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    if (VetoableASTTransformation.hasVetoableAnnotation(parent)) {
        // VetoableASTTransformation will handle both @Bindable and @Vetoable
        return;
    }
    ClassNode declaringClass = parent.getDeclaringClass();
    if (parent instanceof FieldNode) {
        if ((((FieldNode) parent).getModifiers() & Opcodes.ACC_FINAL) != 0) {
            source.getErrorCollector().addErrorAndContinue(new SyntaxErrorMessage(new SyntaxException("@groovy.beans.Bindable cannot annotate a final property.", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), source));
        }
        if (VetoableASTTransformation.hasVetoableAnnotation(parent.getDeclaringClass())) {
            // VetoableASTTransformation will handle both @Bindable and @Vetoable
            return;
        }
        addListenerToProperty(source, node, declaringClass, (FieldNode) parent);
    } else if (parent instanceof ClassNode) {
        addListenerToClass(source, (ClassNode) parent);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

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