Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with FieldNode

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

the class SortableASTTransformation method createComparatorFor.

private static void createComparatorFor(ClassNode classNode, PropertyNode property) {
    String propName = property.getName();
    String className = classNode.getName() + "$" + StringGroovyMethods.capitalize(propName) + "Comparator";
    ClassNode superClass = makeClassSafeWithGenerics(AbstractComparator.class, classNode);
    InnerClassNode cmpClass = new InnerClassNode(classNode, className, ACC_PRIVATE | ACC_STATIC, superClass);
    classNode.getModule().addClass(cmpClass);
    cmpClass.addMethod(new MethodNode("compare", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), ARG0), param(newClass(classNode), ARG1)), ClassNode.EMPTY_ARRAY, createCompareMethodBody(property)));
    String fieldName = "this$" + StringGroovyMethods.capitalize(propName) + "Comparator";
    // private final Comparator this$<property>Comparator = new <type>$<property>Comparator();
    FieldNode cmpField = classNode.addField(fieldName, ACC_STATIC | ACC_FINAL | ACC_PRIVATE | ACC_SYNTHETIC, COMPARATOR_TYPE, ctorX(cmpClass));
    classNode.addMethod(new MethodNode("comparatorBy" + StringGroovyMethods.capitalize(propName), ACC_PUBLIC | ACC_STATIC, COMPARATOR_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(fieldX(cmpField))));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) FieldNode(org.codehaus.groovy.ast.FieldNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode)

Aggregations

FieldNode (org.codehaus.groovy.ast.FieldNode)204 ClassNode (org.codehaus.groovy.ast.ClassNode)126 Expression (org.codehaus.groovy.ast.expr.Expression)58 MethodNode (org.codehaus.groovy.ast.MethodNode)57 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)54 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Parameter (org.codehaus.groovy.ast.Parameter)49 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)43 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)43 ArrayList (java.util.ArrayList)40 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)36 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)33 AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)31 PropertyNode (org.codehaus.groovy.ast.PropertyNode)30 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)27 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)19 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 CastExpression (org.codehaus.groovy.ast.expr.CastExpression)15