Search in sources :

Example 11 with CastExpression

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

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)

Example 12 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 13 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 14 with CastExpression

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

the class CastExpressionOptimizer method transformCastExpression.

public Expression transformCastExpression(final CastExpression cast) {
    if (cast.isCoerce()) {
        Expression expression = cast.getExpression();
        ClassNode exprInferredType = transformer.getTypeChooser().resolveType(expression, transformer.getClassNode());
        ClassNode castType = cast.getType();
        if (castType.isArray() && expression instanceof ListExpression) {
            ArrayExpression arrayExpression = new ArrayExpression(castType.getComponentType(), ((ListExpression) expression).getExpressions());
            arrayExpression.setSourcePosition(cast);
            return transformer.transform(arrayExpression);
        }
        if (isOptimizable(exprInferredType, castType)) {
            // coerce is not needed
            CastExpression trn = new CastExpression(castType, transformer.transform(expression));
            trn.setSourcePosition(cast);
            trn.copyNodeMetaData(cast);
            return trn;
        }
    } else if (ClassHelper.char_TYPE.equals(cast.getType())) {
        Expression expression = cast.getExpression();
        if (expression instanceof ConstantExpression) {
            ConstantExpression ce = (ConstantExpression) expression;
            if (ClassHelper.STRING_TYPE.equals(ce.getType())) {
                String val = (String) ce.getValue();
                if (val != null && val.length() == 1) {
                    ConstantExpression result = new ConstantExpression(val.charAt(0), true);
                    result.setSourcePosition(cast);
                    return result;
                }
            }
        }
    }
    return transformer.superTransform(cast);
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) Expression(org.codehaus.groovy.ast.expr.Expression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Example 15 with CastExpression

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

the class EqualsAndHashCodeASTTransformation method createEquals.

public static void createEquals(ClassNode cNode, boolean includeFields, boolean callSuper, boolean useCanEqual, List<String> excludes, List<String> includes) {
    if (useCanEqual)
        createCanEqual(cNode);
    // make a public method if none exists otherwise try a private method with leading underscore
    boolean hasExistingEquals = hasDeclaredMethod(cNode, "equals", 1);
    if (hasExistingEquals && hasDeclaredMethod(cNode, "_equals", 1))
        return;
    final BlockStatement body = new BlockStatement();
    VariableExpression other = varX("other");
    // some short circuit cases for efficiency
    body.addStatement(ifS(equalsNullX(other), returnS(constX(Boolean.FALSE, true))));
    body.addStatement(ifS(sameX(varX("this"), other), returnS(constX(Boolean.TRUE, true))));
    if (useCanEqual) {
        body.addStatement(ifS(notX(isInstanceOfX(other, GenericsUtils.nonGeneric(cNode))), returnS(constX(Boolean.FALSE, true))));
    } else {
        body.addStatement(ifS(notX(hasClassX(other, GenericsUtils.nonGeneric(cNode))), returnS(constX(Boolean.FALSE, true))));
    }
    VariableExpression otherTyped = varX("otherTyped", GenericsUtils.nonGeneric(cNode));
    CastExpression castExpression = new CastExpression(GenericsUtils.nonGeneric(cNode), other);
    castExpression.setStrict(true);
    body.addStatement(declS(otherTyped, castExpression));
    if (useCanEqual) {
        body.addStatement(ifS(notX(callX(otherTyped, "canEqual", varX("this"))), returnS(constX(Boolean.FALSE, true))));
    }
    List<PropertyNode> pList = getInstanceProperties(cNode);
    for (PropertyNode pNode : pList) {
        if (shouldSkip(pNode.getName(), excludes, includes))
            continue;
        boolean canBeSelf = StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(pNode.getOriginType(), cNode);
        if (!canBeSelf) {
            body.addStatement(ifS(notX(hasEqualPropertyX(pNode, otherTyped)), returnS(constX(Boolean.FALSE, true))));
        } else {
            body.addStatement(ifS(notX(hasSamePropertyX(pNode, otherTyped)), ifElseS(differentSelfRecursivePropertyX(pNode, otherTyped), returnS(constX(Boolean.FALSE, true)), ifS(notX(bothSelfRecursivePropertyX(pNode, otherTyped)), ifS(notX(hasEqualPropertyX(pNode, otherTyped)), returnS(constX(Boolean.FALSE, true)))))));
        }
    }
    List<FieldNode> fList = new ArrayList<FieldNode>();
    if (includeFields) {
        fList.addAll(getInstanceNonPropertyFields(cNode));
    }
    for (FieldNode fNode : fList) {
        if (shouldSkip(fNode.getName(), excludes, includes))
            continue;
        body.addStatement(ifS(notX(hasSameFieldX(fNode, otherTyped)), ifElseS(differentSelfRecursiveFieldX(fNode, otherTyped), returnS(constX(Boolean.FALSE, true)), ifS(notX(bothSelfRecursiveFieldX(fNode, otherTyped)), ifS(notX(hasEqualFieldX(fNode, otherTyped)), returnS(constX(Boolean.FALSE, true)))))));
    }
    if (callSuper) {
        body.addStatement(ifS(notX(isTrueX(callSuperX("equals", other))), returnS(constX(Boolean.FALSE, true))));
    }
    // default
    body.addStatement(returnS(constX(Boolean.TRUE, true)));
    cNode.addMethod(new MethodNode(hasExistingEquals ? "_equals" : "equals", hasExistingEquals ? ACC_PRIVATE : ACC_PUBLIC, ClassHelper.boolean_TYPE, params(param(OBJECT_TYPE, other.getName())), ClassNode.EMPTY_ARRAY, body));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MethodNode(org.codehaus.groovy.ast.MethodNode) PropertyNode(org.codehaus.groovy.ast.PropertyNode) ArrayList(java.util.ArrayList) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression)

Aggregations

CastExpression (org.codehaus.groovy.ast.expr.CastExpression)23 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)17 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)15 Expression (org.codehaus.groovy.ast.expr.Expression)15 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)13 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)11 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)9 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)9 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 List (java.util.List)5 Parameter (org.codehaus.groovy.ast.Parameter)5 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)5 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)5 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)5 LinkedList (java.util.LinkedList)4