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

The following examples show how to use org.codehaus.groovy.ast.expr.PropertyExpression#putNodeMetaData() . 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 6 votes vote down vote up
private static Expression tryTransformPrivateFieldAccess(final VariableExpression expr) {
    FieldNode field = expr.getNodeMetaData(StaticTypesMarker.PV_FIELDS_ACCESS);
    if (field == null) {
        field = expr.getNodeMetaData(StaticTypesMarker.PV_FIELDS_MUTATION);
    }
    if (field != null) {
        // access to a private field from a section of code that normally doesn't have access to it, like a closure or an inner class
        PropertyExpression pexp = thisPropX(true, expr.getName());
        // store the declaring class so that the class writer knows that it will have to call a bridge method
        pexp.getObjectExpression().putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, field.getDeclaringClass());
        pexp.putNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE, field.getOriginType());
        pexp.getProperty().setSourcePosition(expr);
        return pexp;
    }
    return null;
}
 
Example 2
Source File: AbstractTypeCheckingExtension.java    From groovy with Apache License 2.0 5 votes vote down vote up
/**
 * Instructs the type checker that a property access is dynamic.
 * Calling this method automatically sets the handled flag to true.
 * @param pexp the property or attribute expression
 * @param returnType the type of the property
 */
public void makeDynamic(PropertyExpression pexp, ClassNode returnType) {
    context.getEnclosingMethod().putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, Boolean.TRUE);
    pexp.putNodeMetaData(StaticTypesMarker.DYNAMIC_RESOLUTION, returnType);
    storeType(pexp, returnType);
    setHandled(true);
    if (debug) {
        LOG.info("Turning '"+pexp.getText()+"' into a dynamic property access of type "+returnType.toString(false));
    }
}
 
Example 3
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 4
Source File: StaticTypesCallSiteWriter.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void makeGroovyObjectGetPropertySite(final Expression receiver, final String propertyName, final boolean safe, final boolean implicitThis) {
    ClassNode receiverType = controller.getClassNode();
    if (!AsmClassGenerator.isThisExpression(receiver) || controller.isInGeneratedFunction()) {
        receiverType = controller.getTypeChooser().resolveType(receiver, receiverType);
    }

    String property = propertyName;
    if (implicitThis && controller.getInvocationWriter() instanceof StaticInvocationWriter) {
        Expression currentCall = ((StaticInvocationWriter) controller.getInvocationWriter()).getCurrentCall();
        if (currentCall != null && currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER) != null) {
            property = currentCall.getNodeMetaData(StaticTypesMarker.IMPLICIT_RECEIVER);
            String[] props = property.split("\\.");
            BytecodeExpression thisLoader = bytecodeX(CLOSURE_TYPE, mv -> mv.visitVarInsn(ALOAD, 0));
            PropertyExpression pexp = propX(thisLoader, constX(props[0]), safe);
            for (int i = 1, n = props.length; i < n; i += 1) {
                pexp.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, CLOSURE_TYPE);
                pexp = propX(pexp, props[i]);
            }
            pexp.visit(controller.getAcg());
            return;
        }
    }

    if (makeGetPropertyWithGetter(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetPrivateFieldWithBridgeMethod(receiver, receiverType, property, safe, implicitThis)) return;
    if (makeGetField(receiver, receiverType, property, safe, implicitThis)) return;

    MethodCallExpression call = callX(receiver, "getProperty", args(constX(property)));
    call.setImplicitThis(implicitThis);
    call.setMethodTarget(GROOVYOBJECT_GETPROPERTY_METHOD);
    call.setSafe(safe);
    call.visit(controller.getAcg());
}