Search in sources :

Example 6 with CastExpression

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

the class TraitComposer method createForwarderMethod.

private static void createForwarderMethod(ClassNode trait, ClassNode targetNode, MethodNode helperMethod, MethodNode originalMethod, ClassNode helperClassNode, Map<String, ClassNode> genericsSpec, Parameter[] helperMethodParams, Parameter[] traitMethodParams, Parameter[] forwarderParams, ArgumentListExpression helperMethodArgList) {
    MethodCallExpression mce = new MethodCallExpression(new ClassExpression(helperClassNode), helperMethod.getName(), helperMethodArgList);
    mce.setImplicitThis(false);
    genericsSpec = GenericsUtils.addMethodGenerics(helperMethod, genericsSpec);
    ClassNode[] exceptionNodes = correctToGenericsSpecRecurse(genericsSpec, copyExceptions(helperMethod.getExceptions()));
    ClassNode fixedReturnType = correctToGenericsSpecRecurse(genericsSpec, helperMethod.getReturnType());
    Expression forwardExpression = genericsSpec.isEmpty() ? mce : new CastExpression(fixedReturnType, mce);
    int access = helperMethod.getModifiers();
    // we could rely on the first parameter name ($static$self) but that information is not
    // guaranteed to be always present
    boolean isHelperForStaticMethod = helperMethodParams[0].getOriginType().equals(ClassHelper.CLASS_Type);
    if (Modifier.isPrivate(access) && !isHelperForStaticMethod) {
        // see GROOVY-7213
        return;
    }
    if (!isHelperForStaticMethod) {
        access = access ^ Opcodes.ACC_STATIC;
    }
    MethodNode forwarder = new MethodNode(helperMethod.getName(), access, fixedReturnType, forwarderParams, exceptionNodes, new ExpressionStatement(forwardExpression));
    List<AnnotationNode> copied = new LinkedList<AnnotationNode>();
    // at this point, should *always* stay empty
    List<AnnotationNode> notCopied = Collections.emptyList();
    GeneralUtils.copyAnnotatedNodeAnnotations(helperMethod, copied, notCopied);
    if (!copied.isEmpty()) {
        forwarder.addAnnotations(copied);
    }
    if (originalMethod != null) {
        GenericsType[] newGt = GenericsUtils.applyGenericsContextToPlaceHolders(genericsSpec, originalMethod.getGenericsTypes());
        newGt = removeNonPlaceHolders(newGt);
        forwarder.setGenericsTypes(newGt);
    }
    // add a helper annotation indicating that it is a bridge method
    AnnotationNode bridgeAnnotation = new AnnotationNode(Traits.TRAITBRIDGE_CLASSNODE);
    bridgeAnnotation.addMember("traitClass", new ClassExpression(trait));
    bridgeAnnotation.addMember("desc", new ConstantExpression(BytecodeHelper.getMethodDescriptor(helperMethod.getReturnType(), traitMethodParams)));
    forwarder.addAnnotation(bridgeAnnotation);
    if (!shouldSkipMethod(targetNode, forwarder.getName(), forwarderParams)) {
        targetNode.addMethod(forwarder);
    }
    createSuperForwarder(targetNode, forwarder, genericsSpec);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) LinkedList(java.util.LinkedList) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) GenericsType(org.codehaus.groovy.ast.GenericsType) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 7 with CastExpression

use of org.codehaus.groovy.ast.expr.CastExpression in project grails-core by grails.

the class DefaultASTValidateableHelper method addValidateMethod.

protected void addValidateMethod(final ClassNode classNode) {
    String fieldsToValidateParameterName = "$fieldsToValidate";
    final MethodNode listArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, new Parameter[] { new Parameter(new ClassNode(List.class), fieldsToValidateParameterName) });
    if (listArgValidateMethod == null) {
        final BlockStatement validateMethodCode = new BlockStatement();
        final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
        validateInstanceArguments.addExpression(new VariableExpression("this"));
        validateInstanceArguments.addExpression(new VariableExpression(fieldsToValidateParameterName, ClassHelper.LIST_TYPE));
        final ClassNode validationSupportClassNode = ClassHelper.make(ValidationSupport.class);
        final StaticMethodCallExpression invokeValidateInstanceExpression = new StaticMethodCallExpression(validationSupportClassNode, "validateInstance", validateInstanceArguments);
        validateMethodCode.addStatement(new ExpressionStatement(invokeValidateInstanceExpression));
        final Parameter fieldsToValidateParameter = new Parameter(new ClassNode(List.class), fieldsToValidateParameterName);
        classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, new Parameter[] { fieldsToValidateParameter }, EMPTY_CLASS_ARRAY, validateMethodCode));
    }
    final MethodNode noArgValidateMethod = classNode.getMethod(VALIDATE_METHOD_NAME, ZERO_PARAMETERS);
    if (noArgValidateMethod == null) {
        final BlockStatement validateMethodCode = new BlockStatement();
        final ArgumentListExpression validateInstanceArguments = new ArgumentListExpression();
        validateInstanceArguments.addExpression(new CastExpression(new ClassNode(List.class), new ConstantExpression(null)));
        final Expression callListArgValidateMethod = new MethodCallExpression(new VariableExpression("this"), VALIDATE_METHOD_NAME, validateInstanceArguments);
        validateMethodCode.addStatement(new ReturnStatement(callListArgValidateMethod));
        classNode.addMethod(new MethodNode(VALIDATE_METHOD_NAME, Modifier.PUBLIC, ClassHelper.boolean_TYPE, ZERO_PARAMETERS, EMPTY_CLASS_ARRAY, validateMethodCode));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) EmptyExpression(org.codehaus.groovy.ast.expr.EmptyExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) ArrayList(java.util.ArrayList) List(java.util.List) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 8 with CastExpression

use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.

the class Verifier method addDefaultParameters.

protected void addDefaultParameters(DefaultArgsAction action, MethodNode method) {
    Parameter[] parameters = method.getParameters();
    int counter = 0;
    List paramValues = new ArrayList();
    int size = parameters.length;
    for (int i = size - 1; i >= 0; i--) {
        Parameter parameter = parameters[i];
        if (parameter != null && parameter.hasInitialExpression()) {
            paramValues.add(i);
            paramValues.add(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
            counter++;
        }
    }
    for (int j = 1; j <= counter; j++) {
        Parameter[] newParams = new Parameter[parameters.length - j];
        ArgumentListExpression arguments = new ArgumentListExpression();
        int index = 0;
        int k = 1;
        for (Parameter parameter : parameters) {
            if (parameter == null) {
                throw new GroovyBugError("Parameter should not be null for method " + methodNode.getName());
            } else {
                if (k > counter - j && parameter.hasInitialExpression()) {
                    arguments.addExpression(new CastExpression(parameter.getType(), parameter.getInitialExpression()));
                    k++;
                } else if (parameter.hasInitialExpression()) {
                    newParams[index++] = parameter;
                    arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
                    k++;
                } else {
                    newParams[index++] = parameter;
                    arguments.addExpression(new CastExpression(parameter.getType(), new VariableExpression(parameter.getName())));
                }
            }
        }
        action.call(arguments, newParams, method);
    }
    for (Parameter parameter : parameters) {
        // remove default expression and store it as node metadata
        parameter.putNodeMetaData(Verifier.INITIAL_EXPRESSION, parameter.getInitialExpression());
        parameter.setInitialExpression(null);
    }
}
Also used : ArrayList(java.util.ArrayList) GroovyBugError(org.codehaus.groovy.GroovyBugError) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) List(java.util.List) ArrayList(java.util.ArrayList) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 9 with CastExpression

use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.

the class AnnotationConstantsVisitor method transformConstantExpression.

private static Expression transformConstantExpression(Expression val, ClassNode returnType) {
    ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
    if (val instanceof ConstantExpression) {
        Expression result = revertType(val, returnWrapperType);
        if (result != null) {
            return result;
        }
        return val;
    }
    if (val instanceof CastExpression) {
        CastExpression castExp = (CastExpression) val;
        Expression castee = castExp.getExpression();
        if (castee instanceof ConstantExpression) {
            if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
                return castee;
            }
            Expression result = revertType(castee, returnWrapperType);
            if (result != null) {
                return result;
            }
            return castee;
        }
    }
    return val;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 10 with CastExpression

use of org.codehaus.groovy.ast.expr.CastExpression in project groovy by apache.

the class OperandStack method pushDynamicName.

public void pushDynamicName(Expression name) {
    if (name instanceof ConstantExpression) {
        ConstantExpression ce = (ConstantExpression) name;
        Object value = ce.getValue();
        if (value instanceof String) {
            pushConstant(ce);
            return;
        }
    }
    new CastExpression(ClassHelper.STRING_TYPE, name).visit(controller.getAcg());
}
Also used : ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

CastExpression (org.codehaus.groovy.ast.expr.CastExpression)24 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)18 Expression (org.codehaus.groovy.ast.expr.Expression)16 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)14 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)12 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)10 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)10 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)9 ArrayList (java.util.ArrayList)7 ClassNode (org.codehaus.groovy.ast.ClassNode)7 MethodNode (org.codehaus.groovy.ast.MethodNode)7 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)7 ArrayExpression (org.codehaus.groovy.ast.expr.ArrayExpression)6 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)6 List (java.util.List)5 Parameter (org.codehaus.groovy.ast.Parameter)5 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)5 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)5 LinkedList (java.util.LinkedList)4