Search in sources :

Example 16 with FieldNode

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

the class IndexedPropertyASTTransformation 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 FieldNode) {
        FieldNode fNode = (FieldNode) parent;
        ClassNode cNode = fNode.getDeclaringClass();
        if (cNode.getProperty(fNode.getName()) == null) {
            addError("Error during " + MY_TYPE_NAME + " processing. Field '" + fNode.getName() + "' doesn't appear to be a property; incorrect visibility?", fNode);
            return;
        }
        ClassNode fType = fNode.getType();
        if (fType.isArray()) {
            addArraySetter(fNode);
            addArrayGetter(fNode);
        } else if (fType.isDerivedFrom(LIST_TYPE)) {
            addListSetter(fNode);
            addListGetter(fNode);
        } else {
            addError("Error during " + MY_TYPE_NAME + " processing. Non-Indexable property '" + fNode.getName() + "' found. Type must be array or list but found " + fType.getName(), fNode);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 17 with FieldNode

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

the class ReadWriteLockASTTransformation method determineLock.

private String determineLock(String value, ClassNode targetClass, boolean isStatic, String myTypeName) {
    if (value != null && value.length() > 0 && !value.equalsIgnoreCase(DEFAULT_INSTANCE_LOCKNAME)) {
        FieldNode existingLockField = targetClass.getDeclaredField(value);
        if (existingLockField == null) {
            addError("Error during " + myTypeName + " processing: lock field with name '" + value + "' not found in class " + targetClass.getName(), targetClass);
            return null;
        }
        if (existingLockField.isStatic() != isStatic) {
            addError("Error during " + myTypeName + " processing: lock field with name '" + value + "' should " + (isStatic ? "" : "not ") + "be static", existingLockField);
            return null;
        }
        return value;
    }
    if (isStatic) {
        FieldNode field = targetClass.getDeclaredField(DEFAULT_STATIC_LOCKNAME);
        if (field == null) {
            int visibility = ACC_PRIVATE | ACC_STATIC | ACC_FINAL;
            targetClass.addField(DEFAULT_STATIC_LOCKNAME, visibility, LOCK_TYPE, createLockObject());
        } else if (!field.isStatic()) {
            addError("Error during " + myTypeName + " processing: " + DEFAULT_STATIC_LOCKNAME + " field must be static", field);
            return null;
        }
        return DEFAULT_STATIC_LOCKNAME;
    }
    FieldNode field = targetClass.getDeclaredField(DEFAULT_INSTANCE_LOCKNAME);
    if (field == null) {
        int visibility = ACC_PRIVATE | ACC_FINAL;
        targetClass.addField(DEFAULT_INSTANCE_LOCKNAME, visibility, LOCK_TYPE, createLockObject());
    } else if (field.isStatic()) {
        addError("Error during " + myTypeName + " processing: " + DEFAULT_INSTANCE_LOCKNAME + " field must not be static", field);
        return null;
    }
    return DEFAULT_INSTANCE_LOCKNAME;
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 18 with FieldNode

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

the class OptimizerVisitor method addMissingFields.

private void addMissingFields() {
    for (Object missingField : missingFields) {
        FieldNode f = (FieldNode) missingField;
        currentClass.addField(f);
    }
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode)

Example 19 with FieldNode

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

the class InnerClassVisitor method visitProperty.

@Override
public void visitProperty(PropertyNode node) {
    final FieldNode field = node.getField();
    final Expression init = field.getInitialExpression();
    field.setInitialValueExpression(null);
    super.visitProperty(node);
    field.setInitialValueExpression(init);
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression)

Example 20 with FieldNode

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

the class InnerClassVisitor method visitConstructorCallExpression.

@Override
public void visitConstructorCallExpression(ConstructorCallExpression call) {
    super.visitConstructorCallExpression(call);
    if (!call.isUsingAnonymousInnerClass()) {
        passThisReference(call);
        return;
    }
    InnerClassNode innerClass = (InnerClassNode) call.getType();
    ClassNode outerClass = innerClass.getOuterClass();
    ClassNode superClass = innerClass.getSuperClass();
    if (superClass instanceof InnerClassNode && !superClass.isInterface() && !(superClass.isStaticClass() || ((superClass.getModifiers() & ACC_STATIC) == ACC_STATIC))) {
        insertThis0ToSuperCall(call, innerClass);
    }
    if (!innerClass.getDeclaredConstructors().isEmpty())
        return;
    if ((innerClass.getModifiers() & ACC_STATIC) != 0)
        return;
    VariableScope scope = innerClass.getVariableScope();
    if (scope == null)
        return;
    // expressions = constructor call arguments
    List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
    // block = init code for the constructor we produce
    BlockStatement block = new BlockStatement();
    // parameters = parameters of the constructor
    final int additionalParamCount = 1 + scope.getReferencedLocalVariablesCount();
    List<Parameter> parameters = new ArrayList<Parameter>(expressions.size() + additionalParamCount);
    // superCallArguments = arguments for the super call == the constructor call arguments
    List<Expression> superCallArguments = new ArrayList<Expression>(expressions.size());
    // first we add a super() call for all expressions given in the 
    // constructor call expression
    int pCount = additionalParamCount;
    for (Expression expr : expressions) {
        pCount++;
        // add one parameter for each expression in the
        // constructor call
        Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + pCount);
        parameters.add(param);
        // add to super call
        superCallArguments.add(new VariableExpression(param));
    }
    // add the super call
    ConstructorCallExpression cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression(superCallArguments));
    block.addStatement(new ExpressionStatement(cce));
    // we need to add "this" to access unknown methods/properties
    // this is saved in a field named this$0
    pCount = 0;
    expressions.add(pCount, VariableExpression.THIS_EXPRESSION);
    boolean isStatic = isStaticThis(innerClass, scope);
    ClassNode outerClassType = getClassNode(outerClass, isStatic);
    if (!isStatic && inClosure)
        outerClassType = ClassHelper.CLOSURE_TYPE;
    outerClassType = outerClassType.getPlainNodeReference();
    Parameter thisParameter = new Parameter(outerClassType, "p" + pCount);
    parameters.add(pCount, thisParameter);
    thisField = innerClass.addField("this$0", PUBLIC_SYNTHETIC, outerClassType, null);
    addFieldInit(thisParameter, thisField, block);
    // for each shared variable we add a reference and save it as field
    for (Iterator it = scope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
        pCount++;
        org.codehaus.groovy.ast.Variable var = (org.codehaus.groovy.ast.Variable) it.next();
        VariableExpression ve = new VariableExpression(var);
        ve.setClosureSharedVariable(true);
        ve.setUseReferenceDirectly(true);
        expressions.add(pCount, ve);
        ClassNode rawReferenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference();
        Parameter p = new Parameter(rawReferenceType, "p" + pCount);
        parameters.add(pCount, p);
        p.setOriginType(var.getOriginType());
        final VariableExpression initial = new VariableExpression(p);
        initial.setSynthetic(true);
        initial.setUseReferenceDirectly(true);
        final FieldNode pField = innerClass.addFieldFirst(ve.getName(), PUBLIC_SYNTHETIC, rawReferenceType, initial);
        pField.setHolder(true);
        pField.setOriginType(ClassHelper.getWrapper(var.getOriginType()));
    }
    innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(new Parameter[parameters.size()]), ClassNode.EMPTY_ARRAY, block);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) Iterator(java.util.Iterator) Parameter(org.codehaus.groovy.ast.Parameter) VariableScope(org.codehaus.groovy.ast.VariableScope)

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