Search in sources :

Example 96 with FieldNode

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

the class DelegateASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    if (parent instanceof FieldNode) {
        FieldNode fieldNode = (FieldNode) parent;
        final ClassNode type = fieldNode.getType();
        final ClassNode owner = fieldNode.getOwner();
        if (type.equals(ClassHelper.OBJECT_TYPE) || type.equals(GROOVYOBJECT_TYPE)) {
            addError(MY_TYPE_NAME + " field '" + fieldNode.getName() + "' has an inappropriate type: " + type.getName() + ". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent);
            return;
        }
        if (type.equals(owner)) {
            addError(MY_TYPE_NAME + " field '" + fieldNode.getName() + "' has an inappropriate type: " + type.getName() + ". Delegation to own type not supported. Please use a different type.", parent);
            return;
        }
        final List<MethodNode> fieldMethods = getAllMethods(type);
        for (ClassNode next : type.getAllInterfaces()) {
            fieldMethods.addAll(getAllMethods(next));
        }
        final boolean skipInterfaces = memberHasValue(node, MEMBER_INTERFACES, false);
        final boolean includeDeprecated = memberHasValue(node, MEMBER_DEPRECATED, true) || (type.isInterface() && !skipInterfaces);
        List<String> excludes = getMemberList(node, MEMBER_EXCLUDES);
        List<String> includes = getMemberList(node, MEMBER_INCLUDES);
        List<ClassNode> excludeTypes = getClassList(node, MEMBER_EXCLUDE_TYPES);
        List<ClassNode> includeTypes = getClassList(node, MEMBER_INCLUDE_TYPES);
        checkIncludeExclude(node, excludes, includes, excludeTypes, includeTypes, MY_TYPE_NAME);
        final List<MethodNode> ownerMethods = getAllMethods(owner);
        for (MethodNode mn : fieldMethods) {
            addDelegateMethod(node, fieldNode, owner, ownerMethods, mn, includeDeprecated, includes, excludes, includeTypes, excludeTypes);
        }
        for (PropertyNode prop : getAllProperties(type)) {
            if (prop.isStatic() || !prop.isPublic())
                continue;
            String name = prop.getName();
            addGetterIfNeeded(fieldNode, owner, prop, name, includes, excludes);
            addSetterIfNeeded(fieldNode, owner, prop, name, includes, excludes);
        }
        if (skipInterfaces)
            return;
        final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(type);
        final Set<ClassNode> ownerIfaces = owner.getAllInterfaces();
        Map<String, ClassNode> genericsSpec = createGenericsSpec(fieldNode.getDeclaringClass());
        genericsSpec = createGenericsSpec(fieldNode.getType(), genericsSpec);
        for (ClassNode iface : allInterfaces) {
            if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
                final ClassNode[] ifaces = owner.getInterfaces();
                final ClassNode[] newIfaces = new ClassNode[ifaces.length + 1];
                for (int i = 0; i < ifaces.length; i++) {
                    newIfaces[i] = correctToGenericsSpecRecurse(genericsSpec, ifaces[i]);
                }
                newIfaces[ifaces.length] = correctToGenericsSpecRecurse(genericsSpec, iface);
                owner.setInterfaces(newIfaces);
            }
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode)

Example 97 with FieldNode

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

the class ImmutableASTTransformation 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 (!node.getClassNode().getName().endsWith(".Immutable"))
        return;
    List<PropertyNode> newProperties = new ArrayList<PropertyNode>();
    if (parent instanceof ClassNode) {
        final List<String> knownImmutableClasses = getKnownImmutableClasses(node);
        final List<String> knownImmutables = getKnownImmutables(node);
        ClassNode cNode = (ClassNode) parent;
        String cName = cNode.getName();
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        makeClassFinal(cNode);
        final List<PropertyNode> pList = getInstanceProperties(cNode);
        for (PropertyNode pNode : pList) {
            adjustPropertyForImmutability(pNode, newProperties);
        }
        for (PropertyNode pNode : newProperties) {
            cNode.getProperties().remove(pNode);
            addProperty(cNode, pNode);
        }
        final List<FieldNode> fList = cNode.getFields();
        for (FieldNode fNode : fList) {
            ensureNotPublic(cName, fNode);
        }
        boolean includeSuperProperties = false;
        if (hasAnnotation(cNode, TupleConstructorASTTransformation.MY_TYPE)) {
            AnnotationNode tupleCons = cNode.getAnnotations(TupleConstructorASTTransformation.MY_TYPE).get(0);
            includeSuperProperties = memberHasValue(tupleCons, "includeSuperProperties", true);
            if (unsupportedTupleAttribute(tupleCons, "excludes"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includes"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeFields"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeProperties"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "includeSuperFields"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "callSuper"))
                return;
            if (unsupportedTupleAttribute(tupleCons, "force"))
                return;
        }
        createConstructors(cNode, knownImmutableClasses, knownImmutables, includeSuperProperties);
        if (!hasAnnotation(cNode, EqualsAndHashCodeASTTransformation.MY_TYPE)) {
            createHashCode(cNode, true, false, false, null, null);
            createEquals(cNode, false, false, false, null, null);
        }
        if (!hasAnnotation(cNode, ToStringASTTransformation.MY_TYPE)) {
            createToString(cNode, false, false, null, null, false, true);
        }
        if (memberHasValue(node, MEMBER_ADD_COPY_WITH, true) && pList.size() > 0 && !hasDeclaredMethod(cNode, COPY_WITH_METHOD, 1)) {
            createCopyWith(cNode, pList);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ArrayList(java.util.ArrayList) ToStringASTTransformation.createToString(org.codehaus.groovy.transform.ToStringASTTransformation.createToString)

Example 98 with FieldNode

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

the class PackageScopeASTTransformation method visitClassNode.

private void visitClassNode(ClassNode cNode, List<PackageScopeTarget> value) {
    String cName = cNode.getName();
    if (cNode.isInterface() && value.size() != 1 && value.get(0) != PackageScopeTarget.CLASS) {
        addError("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces except when targeting Class level.", cNode);
    }
    if (value.contains(groovy.transform.PackageScopeTarget.CLASS)) {
        if (cNode.isSyntheticPublic())
            revertVisibility(cNode);
        else
            addError("Can't use " + MY_TYPE_NAME + " for class '" + cNode.getName() + "' which has explicit visibility.", cNode);
    }
    if (value.contains(groovy.transform.PackageScopeTarget.METHODS)) {
        final List<MethodNode> mList = cNode.getMethods();
        for (MethodNode mNode : mList) {
            if (mNode.isSyntheticPublic())
                revertVisibility(mNode);
        }
    }
    if (value.contains(groovy.transform.PackageScopeTarget.CONSTRUCTORS)) {
        final List<ConstructorNode> cList = cNode.getDeclaredConstructors();
        for (MethodNode mNode : cList) {
            if (mNode.isSyntheticPublic())
                revertVisibility(mNode);
        }
    }
    if (value.contains(PackageScopeTarget.FIELDS)) {
        final List<PropertyNode> pList = cNode.getProperties();
        List<PropertyNode> foundProps = new ArrayList<PropertyNode>();
        List<String> foundNames = new ArrayList<String>();
        for (PropertyNode pNode : pList) {
            foundProps.add(pNode);
            foundNames.add(pNode.getName());
        }
        for (PropertyNode pNode : foundProps) {
            pList.remove(pNode);
        }
        final List<FieldNode> fList = cNode.getFields();
        for (FieldNode fNode : fList) {
            if (foundNames.contains(fNode.getName())) {
                revertVisibility(fNode);
            }
        }
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList)

Example 99 with FieldNode

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

the class PackageScopeASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode node = (AnnotationNode) nodes[0];
    boolean legacyMode = LEGACY_TYPE_NAME.equals(node.getClassNode().getName());
    if (!MY_TYPE.equals(node.getClassNode()) && !legacyMode)
        return;
    Expression value = node.getMember("value");
    if (parent instanceof ClassNode) {
        List<groovy.transform.PackageScopeTarget> targets;
        if (value == null)
            targets = Collections.singletonList(legacyMode ? PackageScopeTarget.FIELDS : PackageScopeTarget.CLASS);
        else
            targets = determineTargets(value);
        visitClassNode((ClassNode) parent, targets);
        parent.getAnnotations();
    } else {
        if (value != null) {
            addError("Error during " + MY_TYPE_NAME + " processing: " + TARGET_CLASS_NAME + " only allowed at class level.", parent);
            return;
        }
        if (parent instanceof MethodNode) {
            visitMethodNode((MethodNode) parent);
        } else if (parent instanceof FieldNode) {
            visitFieldNode((FieldNode) parent);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) PackageScopeTarget(groovy.transform.PackageScopeTarget) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) Expression(org.codehaus.groovy.ast.expr.Expression) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 100 with FieldNode

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

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)

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