Java Code Examples for org.codehaus.groovy.ast.expr.FieldExpression#getField()

The following examples show how to use org.codehaus.groovy.ast.expr.FieldExpression#getField() . 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: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * RHS instance field. should move most of the code in the BytecodeHelper
 */
public void loadInstanceField(final FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    ClassNode type = field.getType();

    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));

    if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) {
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "get", "()Ljava/lang/Object;", false);
        controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
    } else {
        controller.getOperandStack().push(type);
    }
}
 
Example 2
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 6 votes vote down vote up
private void storeStaticField(final FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    ClassNode type = field.getType();

    controller.getOperandStack().doGroovyCast(field);

    if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) {
        controller.getOperandStack().box();
        mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        mv.visitFieldInsn(PUTSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
    }

    controller.getOperandStack().remove(1);
}
 
Example 3
Source File: TraitReceiverTransformer.java    From groovy with Apache License 2.0 5 votes vote down vote up
private Expression transformFieldExpression(final FieldExpression exp) {
    FieldNode field = exp.getField();
    MethodCallExpression mce = new MethodCallExpression(
            createFieldHelperReceiver(),
            Traits.helperGetterName(field),
            ArgumentListExpression.EMPTY_ARGUMENTS
    );
    mce.setSourcePosition(exp);
    mce.setImplicitThis(false);
    markDynamicCall(mce, field, field.isStatic());
    return mce;
}
 
Example 4
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
public void loadStaticField(final FieldExpression expression) {
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    ClassNode type = field.getType();

    if (field.isHolder() && !controller.isInGeneratedFunctionConstructor()) {
        mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "get", "()Ljava/lang/Object;", false);
        controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
    } else {
        mv.visitFieldInsn(GETSTATIC, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
        controller.getOperandStack().push(type);
    }
}
 
Example 5
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
private void storeThisInstanceField(final FieldExpression expression) {
    OperandStack operandStack = controller.getOperandStack();
    MethodVisitor mv = controller.getMethodVisitor();
    FieldNode field = expression.getField();
    ClassNode type = field.getType();

    if (field.isHolder() && expression.isUseReferenceDirectly()) {
        // rhs is ready to use reference, just put it in the field
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
    } else if (field.isHolder()) {
        // rhs is normal value, set the value in the Reference
        operandStack.doGroovyCast(field.getOriginType());
        operandStack.box();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
        mv.visitInsn(SWAP);
        mv.visitMethodInsn(INVOKEVIRTUAL, "groovy/lang/Reference", "set", "(Ljava/lang/Object;)V", false);
    } else {
        // rhs is normal value, set normal value
        operandStack.doGroovyCast(field.getOriginType());
        mv.visitVarInsn(ALOAD, 0);
        operandStack.push(controller.getClassNode());
        operandStack.swap();
        mv.visitFieldInsn(PUTFIELD, getFieldOwnerName(field), field.getName(), BytecodeHelper.getTypeDescription(type));
    }
}