Java Code Examples for org.codehaus.groovy.ast.expr.PropertyExpression#setImplicitThis()

The following examples show how to use org.codehaus.groovy.ast.expr.PropertyExpression#setImplicitThis() . 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: VariableExpressionTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Expression tryTransformDelegateToProperty(final VariableExpression expr) {
    // we need to transform variable expressions that go to a delegate
    // to a property expression, as ACG would lose the information in
    // processClassVariable before it reaches any makeCall, that could handle it
    Object val = expr.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (val == null) return null;

    // TODO: handle the owner and delegate cases better for nested scenarios and potentially remove the need for the implicit this case
    Expression receiver = varX("owner".equals(val) ? (String) val : "delegate".equals(val) ? (String) val : "this");
    // GROOVY-9136 -- object expression should not overlap source range of property; property stands in for original variable expression
    receiver.setLineNumber(expr.getLineNumber());
    receiver.setColumnNumber(expr.getColumnNumber());

    PropertyExpression pexp = propX(receiver, expr.getName());
    pexp.getProperty().setSourcePosition(expr);
    pexp.copyNodeMetaData(expr);
    pexp.setImplicitThis(true);

    ClassNode owner = expr.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER);
    if (owner != null) {
        receiver.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, owner);
        receiver.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, val);
    }
    pexp.removeNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);

    return pexp;
}
 
Example 2
Source File: StaticInvocationWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
private boolean tryImplicitReceiver(final Expression origin, final Expression message, final Expression arguments, final MethodCallerMultiAdapter adapter, final boolean safe, final boolean spreadSafe) {
    Object implicitReceiver = origin.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    if (implicitReceiver == null && origin instanceof MethodCallExpression) {
        implicitReceiver = ((MethodCallExpression) origin).getObjectExpression().getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
    }
    if (implicitReceiver != null) {
        String[] path = ((String) implicitReceiver).split("\\.");
        // GROOVY-6021
        PropertyExpression pexp = propX(varX("this", ClassHelper.CLOSURE_TYPE), path[0]);
        pexp.setImplicitThis(true);
        for (int i = 1, n = path.length; i < n; i += 1) {
            pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, ClassHelper.CLOSURE_TYPE);
            pexp = propX(pexp, path[i]);
        }
        pexp.putNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER, implicitReceiver);
        origin.removeNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
        if (origin instanceof PropertyExpression) {
            PropertyExpression rewritten = propX(pexp, ((PropertyExpression) origin).getProperty(), ((PropertyExpression) origin).isSafe());
            rewritten.setSpreadSafe(((PropertyExpression) origin).isSpreadSafe());
            rewritten.visit(controller.getAcg());

            rewritten.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, origin.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE));
        } else {
            makeCall(origin, pexp, message, arguments, adapter, safe, spreadSafe, false);
        }
        return true;
    }
    return false;
}
 
Example 3
Source File: GeneralUtils.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static PropertyExpression thisPropX(final boolean implicit, final String property) {
    PropertyExpression pexp = propX(varX("this"), property);
    pexp.setImplicitThis(implicit);
    return pexp;
}
 
Example 4
Source File: StaticTypesBinaryExpressionMultiTypeDispatcher.java    From groovy with Apache License 2.0 4 votes vote down vote up
private void transformSpreadOnLHS(final BinaryExpression expression) {
    PropertyExpression spreadExpression = (PropertyExpression) expression.getLeftExpression();
    Expression receiver = spreadExpression.getObjectExpression();

    int counter = labelCounter.incrementAndGet();
    CompileStack compileStack = controller.getCompileStack();
    OperandStack operandStack = controller.getOperandStack();

    // create an empty arraylist
    VariableExpression result = varX(this.getClass().getSimpleName() + "$spreadresult" + counter, ARRAYLIST_CLASSNODE);
    ConstructorCallExpression newArrayList = ctorX(ARRAYLIST_CLASSNODE);
    newArrayList.setNodeMetaData(DIRECT_METHOD_CALL_TARGET, ARRAYLIST_CONSTRUCTOR);
    Expression decl = declX(result, newArrayList);
    decl.visit(controller.getAcg());
    // if (receiver != null)
    receiver.visit(controller.getAcg());
    Label ifnull = compileStack.createLocalLabel("ifnull_" + counter);
    MethodVisitor mv = controller.getMethodVisitor();
    mv.visitJumpInsn(IFNULL, ifnull);
    operandStack.remove(1); // receiver consumed by if()
    Label nonull = compileStack.createLocalLabel("nonull_" + counter);
    mv.visitLabel(nonull);
    ClassNode componentType = StaticTypeCheckingVisitor.inferLoopElementType(
            controller.getTypeChooser().resolveType(receiver, controller.getClassNode()));
    Parameter iterator = new Parameter(componentType, "for$it$" + counter);
    VariableExpression iteratorAsVar = varX(iterator);
    PropertyExpression pexp = spreadExpression instanceof AttributeExpression
        ? new AttributeExpression(iteratorAsVar, spreadExpression.getProperty(), true)
        : new PropertyExpression(iteratorAsVar, spreadExpression.getProperty(), true);
    pexp.setImplicitThis(spreadExpression.isImplicitThis());
    pexp.setSourcePosition(spreadExpression);
    BinaryExpression assignment = binX(pexp, expression.getOperation(), expression.getRightExpression());
    MethodCallExpression add = callX(result, "add", assignment);
    add.setMethodTarget(ARRAYLIST_ADD_METHOD);
    // for (e in receiver) { result.add(e?.method(arguments) }
    ForStatement stmt = new ForStatement(
            iterator,
            receiver,
            stmt(add)
    );
    stmt.visit(controller.getAcg());
    // else { empty list }
    mv.visitLabel(ifnull);
    // end of if/else
    // return result list
    result.visit(controller.getAcg());
}