Search in sources :

Example 51 with MethodNode

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

the class StaticTypeCheckingVisitor method visitPrefixOrPostifExpression.

private void visitPrefixOrPostifExpression(final Expression origin, final Expression innerExpression, final int operationType) {
    boolean isPostfix = origin instanceof PostfixExpression;
    ClassNode exprType = getType(innerExpression);
    String name = operationType == PLUS_PLUS ? "next" : operationType == MINUS_MINUS ? "previous" : null;
    if (isPrimitiveType(exprType) || isPrimitiveType(getUnwrapper(exprType))) {
        if (operationType == PLUS_PLUS || operationType == MINUS_MINUS) {
            if (!isPrimitiveType(exprType)) {
                MethodNode node = findMethodOrFail(new VariableExpression("_dummy_", exprType), exprType, name);
                if (node != null) {
                    storeTargetMethod(origin, node);
                    storeType(origin, isPostfix ? exprType : getMathWideningClassNode(exprType));
                    return;
                }
            }
            storeType(origin, exprType);
            return;
        }
        addUnsupportedPreOrPostfixExpressionError(origin);
        return;
    } else if (implementsInterfaceOrIsSubclassOf(exprType, Number_TYPE) && (operationType == PLUS_PLUS || operationType == MINUS_MINUS)) {
        // special case for numbers, improve type checking as we can expect ++ and -- to return the same type
        MethodNode node = findMethodOrFail(innerExpression, exprType, name);
        if (node != null) {
            storeTargetMethod(origin, node);
            storeType(origin, getMathWideningClassNode(exprType));
            return;
        }
    }
    // not a primitive type. We must find a method which is called next
    if (name == null) {
        addUnsupportedPreOrPostfixExpressionError(origin);
        return;
    }
    MethodNode node = findMethodOrFail(innerExpression, exprType, name);
    if (node != null) {
        storeTargetMethod(origin, node);
        storeType(origin, isPostfix ? exprType : inferReturnTypeGenerics(exprType, node, ArgumentListExpression.EMPTY_ARGUMENTS));
    }
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 52 with MethodNode

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

the class StaticTypeCheckingVisitor method prettyPrintMethodList.

protected static String prettyPrintMethodList(List<MethodNode> nodes) {
    StringBuilder sb = new StringBuilder("[");
    for (int i = 0, nodesSize = nodes.size(); i < nodesSize; i++) {
        final MethodNode node = nodes.get(i);
        sb.append(node.getReturnType().toString(false));
        sb.append(" ");
        sb.append(node.getDeclaringClass().toString(false));
        sb.append("#");
        sb.append(toMethodParametersString(node.getName(), extractTypesFromParameters(node.getParameters())));
        if (i < nodesSize - 1)
            sb.append(", ");
    }
    sb.append("]");
    return sb.toString();
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) ClosureSignatureHint(groovy.transform.stc.ClosureSignatureHint)

Example 53 with MethodNode

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

the class SynchronizedASTTransformation 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;
    String value = getMemberStringValue(node, "value");
    if (parent instanceof MethodNode) {
        MethodNode mNode = (MethodNode) parent;
        if (mNode.isAbstract()) {
            addError("Error during " + MY_TYPE_NAME + " processing: annotation not allowed on abstract method '" + mNode.getName() + "'", mNode);
            return;
        }
        ClassNode cNode = mNode.getDeclaringClass();
        String lockExpr = determineLock(value, cNode, mNode);
        if (lockExpr == null)
            return;
        Statement origCode = mNode.getCode();
        Statement newCode = new SynchronizedStatement(varX(lockExpr), origCode);
        mNode.setCode(newCode);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) Statement(org.codehaus.groovy.ast.stmt.Statement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement)

Example 54 with MethodNode

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

the class StaticTypeCheckingVisitor method hasSetter.

@Deprecated
protected SetterInfo hasSetter(final PropertyExpression pexp) {
    String propertyName = pexp.getPropertyAsString();
    if (propertyName == null)
        return null;
    Expression objectExpression = pexp.getObjectExpression();
    List<Receiver<String>> receivers = new LinkedList<Receiver<String>>();
    List<Receiver<String>> owners = makeOwnerList(objectExpression);
    addReceivers(receivers, owners, pexp.isImplicitThis());
    String capName = MetaClassHelper.capitalize(propertyName);
    boolean isAttributeExpression = pexp instanceof AttributeExpression;
    for (Receiver<String> receiver : receivers) {
        ClassNode testClass = receiver.getType();
        LinkedList<ClassNode> queue = new LinkedList<ClassNode>();
        queue.add(testClass);
        if (testClass.isInterface()) {
            queue.addAll(testClass.getAllInterfaces());
        }
        while (!queue.isEmpty()) {
            ClassNode current = queue.removeFirst();
            current = current.redirect();
            // check that a setter also exists
            String setterName = "set" + capName;
            List<MethodNode> setterMethods = findSetters(current, setterName, false);
            if (!setterMethods.isEmpty()) {
                //                    storeType(pexp, setterMethod.getParameters()[0].getType());
                return new SetterInfo(current, setterName, setterMethods);
            }
            if (!isAttributeExpression && current.getSuperClass() != null) {
                queue.add(current.getSuperClass());
            }
        }
    }
    return null;
}
Also used : LowestUpperBoundClassNode(org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) LinkedList(java.util.LinkedList) MethodNode(org.codehaus.groovy.ast.MethodNode)

Example 55 with MethodNode

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

the class StaticTypeCheckingVisitor method disambiguateMethods.

private List<MethodNode> disambiguateMethods(List<MethodNode> methods, ClassNode receiver, ClassNode[] argTypes, final Expression expr) {
    if (methods.size() > 1 && receiver != null && argTypes != null) {
        List<MethodNode> filteredWithGenerics = new LinkedList<MethodNode>();
        for (MethodNode methodNode : methods) {
            if (typeCheckMethodsWithGenerics(receiver, argTypes, methodNode)) {
                filteredWithGenerics.add(methodNode);
            }
        }
        if (filteredWithGenerics.size() == 1) {
            return filteredWithGenerics;
        }
        methods = extension.handleAmbiguousMethods(methods, expr);
    }
    return methods;
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) LinkedList(java.util.LinkedList)

Aggregations

MethodNode (org.codehaus.groovy.ast.MethodNode)294 ClassNode (org.codehaus.groovy.ast.ClassNode)193 InnerClassNode (org.codehaus.groovy.ast.InnerClassNode)94 Parameter (org.codehaus.groovy.ast.Parameter)79 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)65 FieldNode (org.codehaus.groovy.ast.FieldNode)57 LinkedList (java.util.LinkedList)50 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)50 Expression (org.codehaus.groovy.ast.expr.Expression)49 LowestUpperBoundClassNode (org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode)47 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)43 AnnotationNode (org.codehaus.groovy.ast.AnnotationNode)39 ArrayList (java.util.ArrayList)36 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)36 Statement (org.codehaus.groovy.ast.stmt.Statement)34 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)33 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)29 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)27 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)27