Java Code Examples for org.codehaus.groovy.ast.Parameter#getNodeMetaData()

The following examples show how to use org.codehaus.groovy.ast.Parameter#getNodeMetaData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Parameter[] createParametersWithExactType(final LambdaExpression expression) {
    Parameter[] parameters = expression.getParameters();
    if (parameters == null) {
        parameters = Parameter.EMPTY_ARRAY;
    }

    for (Parameter parameter : parameters) {
        ClassNode inferredType = parameter.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
        if (inferredType == null) {
            continue;
        }

        ClassNode type = convertParameterType(parameter.getType(), inferredType);

        parameter.setType(type);
        parameter.setOriginType(type);
    }

    return parameters;
}
 
Example 2
Source File: StaticTypesLambdaWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void loadSharedVariables(final LambdaExpression expression) {
    Parameter[] lambdaSharedVariableParameters = expression.getNodeMetaData(LAMBDA_SHARED_VARIABLES);

    for (Parameter parameter : lambdaSharedVariableParameters) {
        loadReference(parameter.getName(), controller);
        if (parameter.getNodeMetaData(UseExistingReference.class) == null) {
            parameter.setNodeMetaData(UseExistingReference.class, Boolean.TRUE);
        }
    }
}
 
Example 3
Source File: ClosureWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void writeClosure(final ClosureExpression expression) {
    CompileStack compileStack = controller.getCompileStack();
    MethodVisitor mv = controller.getMethodVisitor();
    ClassNode classNode = controller.getClassNode();
    AsmClassGenerator acg = controller.getAcg();

    // generate closure as public class to make sure it can be properly invoked by classes of the
    // Groovy runtime without circumventing JVM access checks (see CachedMethod for example).
    int mods = ACC_PUBLIC | ACC_FINAL;
    if (classNode.isInterface()) {
        mods |= ACC_STATIC;
    }
    ClassNode closureClass = getOrAddClosureClass(expression, mods);
    String closureClassinternalName = BytecodeHelper.getClassInternalName(closureClass);
    List<ConstructorNode> constructors = closureClass.getDeclaredConstructors();
    ConstructorNode node = constructors.get(0);

    Parameter[] localVariableParams = node.getParameters();

    mv.visitTypeInsn(NEW, closureClassinternalName);
    mv.visitInsn(DUP);
    if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall()) {
        (new ClassExpression(classNode)).visit(acg);
        (new ClassExpression(controller.getOutermostClass())).visit(acg);
    } else {
        mv.visitVarInsn(ALOAD, 0);
        controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
        loadThis();
    }

    // now let's load the various parameters we're passing
    // we start at index 2 because the first variable we pass
    // is the owner instance and at this point it is already
    // on the stack
    for (int i = 2; i < localVariableParams.length; i++) {
        Parameter param = localVariableParams[i];
        String name = param.getName();
        loadReference(name, controller);
        if (param.getNodeMetaData(ClosureWriter.UseExistingReference.class)==null) {
            param.setNodeMetaData(ClosureWriter.UseExistingReference.class,Boolean.TRUE);
        }
    }

    // we may need to pass in some other constructors
    //cv.visitMethodInsn(INVOKESPECIAL, innerClassinternalName, "<init>", prototype + ")V");
    mv.visitMethodInsn(INVOKESPECIAL, closureClassinternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, localVariableParams), false);
    controller.getOperandStack().replace(ClassHelper.CLOSURE_TYPE, localVariableParams.length);
}
 
Example 4
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
@Override
protected void loadArguments(final List<Expression> argumentList, final Parameter[] para) {
    if (para.length == 0) return;
    ClassNode lastParaType = para[para.length - 1].getOriginType();
    AsmClassGenerator acg = controller.getAcg();
    TypeChooser typeChooser = controller.getTypeChooser();
    OperandStack operandStack = controller.getOperandStack();
    int argumentListSize = argumentList.size();
    ClassNode lastArgType = argumentListSize > 0 ?
            typeChooser.resolveType(argumentList.get(argumentListSize -1), controller.getClassNode()) : null;
    if (lastParaType.isArray()
            && ((argumentListSize > para.length)
            || ((argumentListSize == (para.length - 1)) && !lastParaType.equals(lastArgType))
            || ((argumentListSize == para.length && lastArgType!=null && !lastArgType.isArray())
                && (StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(lastArgType,lastParaType.getComponentType())))
                    || ClassHelper.GSTRING_TYPE.equals(lastArgType) && ClassHelper.STRING_TYPE.equals(lastParaType.getComponentType()))
            ) {
        int stackLen = operandStack.getStackLength() + argumentListSize;
        MethodVisitor mv = controller.getMethodVisitor();
        controller.setMethodVisitor(mv);
        // varg call
        // first parameters as usual
        for (int i = 0; i < para.length - 1; i += 1) {
            visitArgument(argumentList.get(i), para[i].getType());
        }
        // last parameters wrapped in an array
        List<Expression> lastParams = new ArrayList<>();
        for (int i = para.length - 1; i < argumentListSize; i += 1) {
            lastParams.add(argumentList.get(i));
        }
        ArrayExpression array = new ArrayExpression(lastParaType.getComponentType(), lastParams);
        array.visit(acg);
        // adjust stack length
        while (operandStack.getStackLength() < stackLen) {
            operandStack.push(ClassHelper.OBJECT_TYPE);
        }
        if (argumentListSize == para.length - 1) {
            operandStack.remove(1);
        }
    } else if (argumentListSize == para.length) {
        for (int i = 0; i < argumentListSize; i++) {
            visitArgument(argumentList.get(i), para[i].getType());
        }
    } else {
        // method call with default arguments
        ClassNode classNode = controller.getClassNode();
        Expression[] arguments = new Expression[para.length];
        for (int i = 0, j = 0, n = para.length; i < n; i += 1) {
            Parameter curParam = para[i];
            ClassNode curParamType = curParam.getType();
            Expression curArg = j < argumentListSize ? argumentList.get(j) : null;
            Expression initialExpression = curParam.getNodeMetaData(StaticTypesMarker.INITIAL_EXPRESSION);
            if (initialExpression == null && curParam.hasInitialExpression())
                initialExpression = curParam.getInitialExpression();
            if (initialExpression == null && curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION) != null) {
                initialExpression = curParam.getNodeMetaData(Verifier.INITIAL_EXPRESSION);
            }
            ClassNode curArgType = curArg == null ? null : typeChooser.resolveType(curArg, classNode);

            if (initialExpression != null && !compatibleArgumentType(curArgType, curParamType)) {
                // use default expression
                arguments[i] = initialExpression;
            } else {
                arguments[i] = curArg;
                j += 1;
            }
        }
        for (int i = 0, n = arguments.length; i < n; i += 1) {
            visitArgument(arguments[i], para[i].getType());
        }
    }
}
 
Example 5
Source File: CompileStack.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void defineMethodVariables(final Parameter[] params, final boolean isInStaticContext) {
    Label startLabel  = new Label();
    thisStartLabel = startLabel;
    controller.getMethodVisitor().visitLabel(startLabel);

    makeLocalVariablesOffset(params,isInStaticContext);

    for (Parameter param : params) {
        String name = param.getName();
        BytecodeVariable answer;
        ClassNode type = param.getType();
        if (param.isClosureSharedVariable()) {
            boolean useExistingReference = param.getNodeMetaData(ClosureWriter.UseExistingReference.class) != null;
            answer = defineVar(name, param.getOriginType(), true, useExistingReference);
            answer.setStartLabel(startLabel);
            if (!useExistingReference) {
                controller.getOperandStack().load(type, currentVariableIndex);
                controller.getOperandStack().box();

                // GROOVY-4237, the original variable should always appear
                // in the variable index, otherwise some programs get into
                // trouble. So we define a dummy variable for the packaging
                // phase and let it end right away before the normal
                // reference will be used
                Label newStart = new Label();
                controller.getMethodVisitor().visitLabel(newStart);
                BytecodeVariable var = new BytecodeVariable(currentVariableIndex, param.getOriginType(), name, currentVariableIndex);
                var.setStartLabel(startLabel);
                var.setEndLabel(newStart);
                usedVariables.add(var);
                answer.setStartLabel(newStart);

                createReference(answer);
            }
        } else {
            answer = defineVar(name, type, false, false);
            answer.setStartLabel(startLabel);
        }
        stackVariables.put(name, answer);
    }

    nextVariableIndex = localVariableOffset;
}