Search in sources :

Example 41 with ClassNode

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

the class GenericsVisitor method visitField.

public void visitField(FieldNode node) {
    ClassNode type = node.getType();
    checkGenericsUsage(type, type.redirect());
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 42 with ClassNode

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

the class GenericsVisitor method getPrintName.

private String getPrintName(GenericsType gt) {
    String ret = gt.getName();
    ClassNode[] upperBounds = gt.getUpperBounds();
    ClassNode lowerBound = gt.getLowerBound();
    if (upperBounds != null) {
        ret += " extends ";
        for (int i = 0; i < upperBounds.length; i++) {
            ret += getPrintName(upperBounds[i]);
            if (i + 1 < upperBounds.length)
                ret += " & ";
        }
    } else if (lowerBound != null) {
        ret += " super " + getPrintName(lowerBound);
    }
    return ret;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 43 with ClassNode

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

the class GenericsVisitor method checkGenericsUsage.

private void checkGenericsUsage(ClassNode n, ClassNode cn) {
    if (n.isGenericsPlaceHolder())
        return;
    GenericsType[] nTypes = n.getGenericsTypes();
    GenericsType[] cnTypes = cn.getGenericsTypes();
    // raw type usage is always allowed
    if (nTypes == null)
        return;
    // parameterize a type by using all of the parameters only
    if (cnTypes == null) {
        addError("The class " + n.getName() + " refers to the class " + cn.getName() + " and uses " + nTypes.length + " parameters, but the referred class takes no parameters", n);
        return;
    }
    if (nTypes.length != cnTypes.length) {
        addError("The class " + n.getName() + " refers to the class " + cn.getName() + " and uses " + nTypes.length + " parameters, but the referred class needs " + cnTypes.length, n);
        return;
    }
    // check bounds
    for (int i = 0; i < nTypes.length; i++) {
        ClassNode nType = nTypes[i].getType();
        ClassNode cnType = cnTypes[i].getType();
        if (!nType.isDerivedFrom(cnType)) {
            if (cnType.isInterface() && nType.implementsInterface(cnType))
                continue;
            addError("The type " + nTypes[i].getName() + " is not a valid substitute for the bounded parameter <" + getPrintName(cnTypes[i]) + ">", n);
        }
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 44 with ClassNode

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

the class MopWriter method createMopMethods.

public void createMopMethods() {
    ClassNode classNode = controller.getClassNode();
    if (classNode.declaresInterface(ClassHelper.GENERATED_CLOSURE_Type)) {
        return;
    }
    Set<MopKey> currentClassSignatures = buildCurrentClassSignatureSet(classNode.getMethods());
    visitMopMethodList(classNode.getMethods(), true, Collections.EMPTY_SET);
    visitMopMethodList(classNode.getSuperClass().getAllDeclaredMethods(), false, currentClassSignatures);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode)

Example 45 with ClassNode

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

the class MopWriter method generateMopCalls.

/**
     * generates a Meta Object Protocol method, that is used to call a non public
     * method, or to make a call to super.
     *
     * @param mopCalls list of methods a mop call method should be generated for
     * @param useThis  true if "this" should be used for the naming
     */
protected void generateMopCalls(LinkedList<MethodNode> mopCalls, boolean useThis) {
    for (MethodNode method : mopCalls) {
        String name = getMopMethodName(method, useThis);
        Parameter[] parameters = method.getParameters();
        String methodDescriptor = BytecodeHelper.getMethodDescriptor(method.getReturnType(), method.getParameters());
        MethodVisitor mv = controller.getClassVisitor().visitMethod(ACC_PUBLIC | ACC_SYNTHETIC, name, methodDescriptor, null, null);
        controller.setMethodVisitor(mv);
        mv.visitVarInsn(ALOAD, 0);
        int newRegister = 1;
        OperandStack operandStack = controller.getOperandStack();
        for (Parameter parameter : parameters) {
            ClassNode type = parameter.getType();
            operandStack.load(parameter.getType(), newRegister);
            // increment to next register, double/long are using two places
            newRegister++;
            if (type == ClassHelper.double_TYPE || type == ClassHelper.long_TYPE)
                newRegister++;
        }
        operandStack.remove(parameters.length);
        ClassNode declaringClass = method.getDeclaringClass();
        // JDK 8 support for default methods in interfaces
        // this should probably be strenghtened when we support the A.super.foo() syntax
        int opcode = declaringClass.isInterface() ? INVOKEINTERFACE : INVOKESPECIAL;
        mv.visitMethodInsn(opcode, BytecodeHelper.getClassInternalName(declaringClass), method.getName(), methodDescriptor, opcode == INVOKEINTERFACE);
        BytecodeHelper.doReturn(mv, method.getReturnType());
        mv.visitMaxs(0, 0);
        mv.visitEnd();
        controller.getClassNode().addMethod(name, ACC_PUBLIC | ACC_SYNTHETIC, method.getReturnType(), parameters, null, null);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) MethodNode(org.codehaus.groovy.ast.MethodNode) Parameter(org.codehaus.groovy.ast.Parameter) MethodVisitor(org.objectweb.asm.MethodVisitor)

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