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

The following examples show how to use org.codehaus.groovy.ast.expr.Expression#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: BinaryExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static DeclarationExpression optimizeConstantInitialization(final BinaryExpression originalDeclaration, final Token operation, final ConstantExpression constant, final Expression leftExpression, final ClassNode declarationType) {
    Expression cexp = constX(convertConstant((Number) constant.getValue(), ClassHelper.getWrapper(declarationType)), true);
    cexp.setType(declarationType);
    cexp.setSourcePosition(constant);
    DeclarationExpression result = new DeclarationExpression(
            leftExpression,
            operation,
            cexp
    );
    result.setSourcePosition(originalDeclaration);
    result.copyNodeMetaData(originalDeclaration);
    return result;
}
 
Example 2
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to make a direct method call on a bridge method, if it exists.
 */
protected boolean tryBridgeMethod(final MethodNode target, final Expression receiver, final boolean implicitThis, final TupleExpression args, final ClassNode thisClass) {
    ClassNode lookupClassNode;
    if (target.isProtected()) {
        lookupClassNode = controller.getClassNode();
        while (lookupClassNode != null && !lookupClassNode.isDerivedFrom(target.getDeclaringClass())) {
            lookupClassNode = lookupClassNode.getOuterClass();
        }
        if (lookupClassNode == null) {
            return false;
        }
    } else {
        lookupClassNode = target.getDeclaringClass().redirect();
    }
    Map<MethodNode, MethodNode> bridges = lookupClassNode.getNodeMetaData(StaticCompilationMetadataKeys.PRIVATE_BRIDGE_METHODS);
    MethodNode bridge = bridges == null ? null : bridges.get(target);
    if (bridge != null) {
        Expression fixedReceiver = receiver;
        if (implicitThis) {
            if (!controller.isInGeneratedFunction()) {
                fixedReceiver = propX(classX(lookupClassNode), "this");
            } else if (thisClass != null) {
                ClassNode current = thisClass.getOuterClass();
                fixedReceiver = varX("thisObject", current);
                // adjust for multiple levels of nesting if needed
                while (current.getOuterClass() != null && !lookupClassNode.equals(current)) {
                    FieldNode thisField = current.getField("this$0");
                    current = current.getOuterClass();
                    if (thisField != null) {
                        fixedReceiver = propX(fixedReceiver, "this$0");
                        fixedReceiver.setType(current);
                    }
                }
            }
        }
        ArgumentListExpression newArgs = args(target.isStatic() ? nullX() : fixedReceiver);
        for (Expression expression : args.getExpressions()) {
            newArgs.addExpression(expression);
        }
        return writeDirectMethodCall(bridge, implicitThis, fixedReceiver, newArgs);
    }
    return false;
}