Search in sources :

Example 56 with AnnotatedNode

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

the class TraitASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!Traits.TRAIT_CLASSNODE.equals(anno.getClassNode()))
        return;
    unit = source;
    init(nodes, source);
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, Traits.TRAIT_TYPE_NAME))
            return;
        checkNoConstructor(cNode);
        checkExtendsClause(cNode);
        generateMethodsWithDefaultArgs(cNode);
        replaceExtendsByImplements(cNode);
        ClassNode helperClassNode = createHelperClass(cNode);
        resolveHelperClassIfNecessary(helperClassNode);
    }
}
Also used : InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode)

Example 57 with AnnotatedNode

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

the class MyIntegerAnnoTraceASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit source) {
    if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
        throw new RuntimeException("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
    }
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    File f = new File("temp/log.txt");
    try {
        ResourceGroovyMethods.append(f, parent.getClass().getSimpleName() + " " + parent.getAnnotations().get(0).getMember("value").getText() + " ");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) IOException(java.io.IOException) File(java.io.File)

Example 58 with AnnotatedNode

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

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];
    DelegateDescription delegate = null;
    if (parent instanceof FieldNode) {
        FieldNode fieldNode = (FieldNode) parent;
        delegate = new DelegateDescription();
        delegate.delegate = fieldNode;
        delegate.annotation = node;
        delegate.name = fieldNode.getName();
        delegate.type = fieldNode.getType();
        delegate.owner = fieldNode.getOwner();
        delegate.getOp = varX(fieldNode);
        delegate.origin = "field";
    } else if (parent instanceof MethodNode) {
        MethodNode methodNode = (MethodNode) parent;
        delegate = new DelegateDescription();
        delegate.delegate = methodNode;
        delegate.annotation = node;
        delegate.name = methodNode.getName();
        delegate.type = methodNode.getReturnType();
        delegate.owner = methodNode.getDeclaringClass();
        delegate.getOp = callThisX(delegate.name);
        delegate.origin = "method";
        if (methodNode.getParameters().length > 0) {
            addError("You can only delegate to methods that take no parameters, but " + delegate.name + " takes " + methodNode.getParameters().length + " parameters.", parent);
            return;
        }
    }
    if (delegate != null) {
        if (delegate.type.equals(ClassHelper.OBJECT_TYPE) || delegate.type.equals(GROOVYOBJECT_TYPE)) {
            addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() + ". Please add an explicit type but not java.lang.Object or groovy.lang.GroovyObject.", parent);
            return;
        }
        if (delegate.type.equals(delegate.owner)) {
            addError(MY_TYPE_NAME + " " + delegate.origin + " '" + delegate.name + "' has an inappropriate type: " + delegate.type.getName() + ". Delegation to own type not supported. Please use a different type.", parent);
            return;
        }
        final List<MethodNode> delegateMethods = getAllMethods(delegate.type);
        for (ClassNode next : delegate.type.getAllInterfaces()) {
            delegateMethods.addAll(getAllMethods(next));
        }
        final boolean skipInterfaces = memberHasValue(node, MEMBER_INTERFACES, false);
        final boolean includeDeprecated = memberHasValue(node, MEMBER_DEPRECATED, true) || (delegate.type.isInterface() && !skipInterfaces);
        final boolean allNames = memberHasValue(node, MEMBER_ALL_NAMES, true);
        delegate.excludes = getMemberStringList(node, MEMBER_EXCLUDES);
        delegate.includes = getMemberStringList(node, MEMBER_INCLUDES);
        delegate.excludeTypes = getMemberClassList(node, MEMBER_EXCLUDE_TYPES);
        delegate.includeTypes = getMemberClassList(node, MEMBER_INCLUDE_TYPES);
        checkIncludeExcludeUndefinedAware(node, delegate.excludes, delegate.includes, delegate.excludeTypes, delegate.includeTypes, MY_TYPE_NAME);
        final List<MethodNode> ownerMethods = getAllMethods(delegate.owner);
        for (MethodNode mn : delegateMethods) {
            addDelegateMethod(delegate, ownerMethods, mn, includeDeprecated, allNames);
        }
        for (PropertyNode prop : getAllProperties(delegate.type)) {
            if (prop.isStatic() || !prop.isPublic())
                continue;
            String name = prop.getName();
            addGetterIfNeeded(delegate, prop, name, allNames);
            addSetterIfNeeded(delegate, prop, name, allNames);
        }
        if (skipInterfaces)
            return;
        final Set<ClassNode> allInterfaces = getInterfacesAndSuperInterfaces(delegate.type);
        final Set<ClassNode> ownerIfaces = delegate.owner.getAllInterfaces();
        Map<String, ClassNode> genericsSpec = createGenericsSpec(delegate.owner);
        genericsSpec = createGenericsSpec(delegate.type, genericsSpec);
        for (ClassNode iface : allInterfaces) {
            if (Modifier.isPublic(iface.getModifiers()) && !ownerIfaces.contains(iface)) {
                final ClassNode[] ifaces = delegate.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);
                delegate.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 59 with AnnotatedNode

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

the class ExternalizeMethodsASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!checkNotInterface(cNode, MY_TYPE_NAME))
            return;
        cNode.addInterface(EXTERNALIZABLE_TYPE);
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        List<String> excludes = getMemberStringList(anno, "excludes");
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
            return;
        List<FieldNode> list = getInstancePropertyFields(cNode);
        if (includeFields) {
            list.addAll(getInstanceNonPropertyFields(cNode));
        }
        createWriteExternal(cNode, excludes, list);
        createReadExternal(cNode, excludes, list);
    }
}
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 60 with AnnotatedNode

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

the class ExternalizeVerifierASTTransformation method visit.

public void visit(ASTNode[] nodes, SourceUnit source) {
    init(nodes, source);
    AnnotatedNode parent = (AnnotatedNode) nodes[1];
    AnnotationNode anno = (AnnotationNode) nodes[0];
    if (!MY_TYPE.equals(anno.getClassNode()))
        return;
    if (parent instanceof ClassNode) {
        ClassNode cNode = (ClassNode) parent;
        if (!hasNoargConstructor(cNode)) {
            addError(MY_TYPE_NAME + ": An Externalizable class requires a no-arg constructor but none found", cNode);
        }
        if (!implementsExternalizable(cNode)) {
            addError(MY_TYPE_NAME + ": An Externalizable class must implement the Externalizable interface", cNode);
        }
        boolean includeFields = memberHasValue(anno, "includeFields", true);
        boolean checkPropertyTypes = memberHasValue(anno, "checkPropertyTypes", true);
        List<String> excludes = getMemberStringList(anno, "excludes");
        if (!checkPropertyList(cNode, excludes, "excludes", anno, MY_TYPE_NAME, includeFields))
            return;
        List<FieldNode> list = getInstancePropertyFields(cNode);
        if (includeFields) {
            list.addAll(getInstanceNonPropertyFields(cNode));
        }
        checkProps(list, excludes, checkPropertyTypes);
    }
}
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)

Aggregations

AnnotatedNode (org.codehaus.groovy.ast.AnnotatedNode)67 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)67 ClassNode (org.codehaus.groovy.ast.ClassNode)59 FieldNode (org.codehaus.groovy.ast.FieldNode)31 MethodNode (org.codehaus.groovy.ast.MethodNode)18 Expression (org.codehaus.groovy.ast.expr.Expression)17 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)10 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)10 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)10 DeclarationExpression (org.codehaus.groovy.ast.expr.DeclarationExpression)9 GroovyBugError (org.codehaus.groovy.GroovyBugError)8 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)7 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)7 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)7 VariableScopeVisitor (org.codehaus.groovy.classgen.VariableScopeVisitor)6 ArrayList (java.util.ArrayList)5 Statement (org.codehaus.groovy.ast.stmt.Statement)5 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)4 PropertyNode (org.codehaus.groovy.ast.PropertyNode)4