Search in sources :

Example 46 with ClassNode

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

the class MopWriter method getMopMethodName.

/**
     * creates a MOP method name from a method
     *
     * @param method  the method to be called by the mop method
     * @param useThis if true, then it is a call on "this", "super" else
     * @return the mop method name
     */
public static String getMopMethodName(MethodNode method, boolean useThis) {
    ClassNode declaringNode = method.getDeclaringClass();
    int distance = 0;
    for (; declaringNode != null; declaringNode = declaringNode.getSuperClass()) {
        distance++;
    }
    return (useThis ? "this" : "super") + "$" + distance + "$" + method.getName();
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 47 with ClassNode

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

the class ImportCustomizer method addImport.

private void addImport(final String className) {
    final ClassNode node = ClassHelper.make(className);
    imports.add(new Import(ImportType.regular, node.getNameWithoutPackage(), node));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 48 with ClassNode

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

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);
        createHelperClass(cNode);
    }
}
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 49 with ClassNode

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

the class TraitComposer method doCreateSuperForwarder.

/**
     * Creates a method to dispatch to "super traits" in a "stackable" fashion. The generated method looks like this:
     * <p>
     * <code>ReturnType trait$super$method(Class clazz, Arg1 arg1, Arg2 arg2, ...) {
     *     if (SomeTrait.is(A) { return SomeOtherTrait$Trait$Helper.method(this, arg1, arg2) }
     *     super.method(arg1,arg2)
     * }</code>
     * </p>
     * @param targetNode
     * @param forwarderMethod
     * @param interfacesToGenerateForwarderFor
     * @param genericsSpec
     */
private static void doCreateSuperForwarder(ClassNode targetNode, MethodNode forwarderMethod, ClassNode[] interfacesToGenerateForwarderFor, Map<String, ClassNode> genericsSpec) {
    Parameter[] parameters = forwarderMethod.getParameters();
    Parameter[] superForwarderParams = new Parameter[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        ClassNode originType = parameter.getOriginType();
        superForwarderParams[i] = new Parameter(correctToGenericsSpecRecurse(genericsSpec, originType), parameter.getName());
    }
    for (int i = 0; i < interfacesToGenerateForwarderFor.length; i++) {
        final ClassNode current = interfacesToGenerateForwarderFor[i];
        final ClassNode next = i < interfacesToGenerateForwarderFor.length - 1 ? interfacesToGenerateForwarderFor[i + 1] : null;
        String forwarderName = Traits.getSuperTraitMethodName(current, forwarderMethod.getName());
        if (targetNode.getDeclaredMethod(forwarderName, superForwarderParams) == null) {
            ClassNode returnType = correctToGenericsSpecRecurse(genericsSpec, forwarderMethod.getReturnType());
            Statement delegate = next == null ? createSuperFallback(forwarderMethod, returnType) : createDelegatingForwarder(forwarderMethod, next);
            MethodNode methodNode = targetNode.addMethod(forwarderName, Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, returnType, superForwarderParams, ClassNode.EMPTY_ARRAY, delegate);
            methodNode.setGenericsTypes(forwarderMethod.getGenericsTypes());
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) Parameter(org.codehaus.groovy.ast.Parameter)

Example 50 with ClassNode

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

the class TraitComposer method createSuperForwarder.

/**
     * Creates, if necessary, a super forwarder method, for stackable traits.
     * @param forwarder a forwarder method
     * @param genericsSpec
     */
private static void createSuperForwarder(ClassNode targetNode, MethodNode forwarder, final Map<String, ClassNode> genericsSpec) {
    List<ClassNode> interfaces = new ArrayList<ClassNode>(Traits.collectAllInterfacesReverseOrder(targetNode, new LinkedHashSet<ClassNode>()));
    String name = forwarder.getName();
    Parameter[] forwarderParameters = forwarder.getParameters();
    LinkedHashSet<ClassNode> traits = new LinkedHashSet<ClassNode>();
    List<MethodNode> superForwarders = new LinkedList<MethodNode>();
    for (ClassNode node : interfaces) {
        if (Traits.isTrait(node)) {
            MethodNode method = node.getDeclaredMethod(name, forwarderParameters);
            if (method != null) {
                // a similar method exists, we need a super bridge
                // trait$super$foo(Class currentTrait, ...)
                traits.add(node);
                superForwarders.add(method);
            }
        }
    }
    for (MethodNode superForwarder : superForwarders) {
        doCreateSuperForwarder(targetNode, superForwarder, traits.toArray(new ClassNode[traits.size()]), genericsSpec);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) ArrayList(java.util.ArrayList) Parameter(org.codehaus.groovy.ast.Parameter) LinkedList(java.util.LinkedList)

Aggregations

ClassNode (org.codehaus.groovy.ast.ClassNode)869 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)348 MethodNode (org.codehaus.groovy.ast.MethodNode)193 GenericsType (org.codehaus.groovy.ast.GenericsType)154 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)148 Expression (org.codehaus.groovy.ast.expr.Expression)146 Parameter (org.codehaus.groovy.ast.Parameter)135 FieldNode (org.codehaus.groovy.ast.FieldNode)126 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)118 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)117 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)113 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)103 ArrayList (java.util.ArrayList)95 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)92 PropertyExpression (org.codehaus.groovy.ast.expr.PropertyExpression)75 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)74 LinkedList (java.util.LinkedList)71 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)71 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)62 ListExpression (org.codehaus.groovy.ast.expr.ListExpression)61