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

The following examples show how to use org.codehaus.groovy.ast.Parameter#setType() . 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: StaticTypesMethodReferenceExpressionWriter.java    From groovy with Apache License 2.0 6 votes vote down vote up
private Parameter[] createParametersWithExactType(MethodNode abstractMethodNode, ClassNode[] inferredParameterTypes) {
    Parameter[] originalParameters = abstractMethodNode.getParameters();

    // We MUST clone the parameters to avoid impacting the original parameter type of SAM
    Parameter[] parameters = GeneralUtils.cloneParams(originalParameters);
    if (parameters == null) {
        parameters = Parameter.EMPTY_ARRAY;
    }

    for (int i = 0; i < parameters.length; i++) {
        Parameter parameter = parameters[i];
        ClassNode parameterType = parameter.getType();
        ClassNode inferredType = inferredParameterTypes[i];

        if (null == inferredType) {
            continue;
        }

        ClassNode type = convertParameterType(parameterType, inferredType);

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

    return parameters;
}
 
Example 2
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 3
Source File: Verifier.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static void adjustTypesIfStaticMainMethod(MethodNode node) {
    if (node.getName().equals("main") && node.isStatic()) {
        Parameter[] params = node.getParameters();
        if (params.length == 1) {
            Parameter param = params[0];
            if (param.getType() == null || param.getType() == ClassHelper.OBJECT_TYPE) {
                param.setType(ClassHelper.STRING_TYPE.makeArray());
                ClassNode returnType = node.getReturnType();
                if (returnType == ClassHelper.OBJECT_TYPE) {
                    node.setReturnType(ClassHelper.VOID_TYPE);
                }
            }
        }
    }
}
 
Example 4
Source File: ClosureWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void addFieldsAndGettersForLocalVariables(final InnerClassNode answer, final Parameter[] localVariableParams) {
    for (Parameter param : localVariableParams) {
        String paramName = param.getName();
        ClassNode type = param.getType();
        VariableExpression initialValue = new VariableExpression(paramName);
        initialValue.setAccessedVariable(param);
        initialValue.setUseReferenceDirectly(true);
        ClassNode realType = type;
        type = ClassHelper.makeReference();
        param.setType(ClassHelper.makeReference());
        FieldNode paramField = answer.addField(paramName, ACC_PRIVATE | ACC_SYNTHETIC, type, initialValue);
        paramField.setOriginType(ClassHelper.getWrapper(param.getOriginType()));
        paramField.setHolder(true);
        String methodName = Verifier.capitalize(paramName);

        // let's add a getter & setter
        Expression fieldExp = new FieldExpression(paramField);
        markAsGenerated(answer,
            answer.addMethod(
                "get" + methodName,
                ACC_PUBLIC,
                realType.getPlainNodeReference(),
                Parameter.EMPTY_ARRAY,
                ClassNode.EMPTY_ARRAY,
                new ReturnStatement(fieldExp)),
            true);
    }
}