All Downloads are FREE. Search and download functionalities are using the official Maven repository.

remoter.compiler.builder.BooleanParamBuilder Maven / Gradle / Ivy

Go to download

Remoter makes developing android remote services intuitive without messing with aidl.

The newest version!
package remoter.compiler.builder;

import com.squareup.javapoet.MethodSpec;

import javax.annotation.processing.Messager;
import javax.lang.model.element.Element;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;

/**
 * A {@link ParamBuilder} for boolean type parameters
 */
class BooleanParamBuilder extends ParamBuilder {


    protected BooleanParamBuilder(Messager messager, Element element) {
        super(messager, element);
    }


    @Override
    public void writeParamsToProxy(VariableElement param, ParamType paramType, MethodSpec.Builder methodBuilder) {
        if (param.asType().getKind() == TypeKind.ARRAY) {
            if (paramType == ParamType.OUT) {
                writeArrayOutParamsToProxy(param, methodBuilder);
            } else {
                methodBuilder.addStatement("data.writeBooleanArray(" + param.getSimpleName() + ")");
            }
        } else {
            methodBuilder.addStatement("data.writeInt(" + param.getSimpleName() + " ? 1 : 0 )");
        }
    }

    @Override
    public void readResultsFromProxy(TypeMirror resultType, MethodSpec.Builder methodBuilder) {
        if (resultType.getKind() == TypeKind.ARRAY) {
            methodBuilder.addStatement("result = reply.createBooleanArray()");
        } else {
            methodBuilder.addStatement("result = reply.readInt() == 1");
        }
    }

    @Override
    public void readResultsFromStub(TypeMirror resultType, MethodSpec.Builder methodBuilder) {
        if (resultType.getKind() == TypeKind.ARRAY) {
            methodBuilder.addStatement("reply.writeBooleanArray(result)");
        } else {
            methodBuilder.addStatement("reply.writeInt(result ? 1 : 0 )");
        }
    }

    @Override
    public void readOutResultsFromStub(VariableElement param, ParamType paramType, String paramName, MethodSpec.Builder methodBuilder) {
        if (param.asType().getKind() == TypeKind.ARRAY) {
            methodBuilder.addStatement("reply.writeBooleanArray(" + paramName + ")");
        }
    }


    @Override
    public void writeParamsToStub(VariableElement param, ParamType paramType, String paramName, MethodSpec.Builder methodBuilder) {
        super.writeParamsToStub(param, paramType, paramName, methodBuilder);
        if (param.asType().getKind() == TypeKind.ARRAY) {
            if (paramType == ParamType.OUT) {
                writeOutParamsToStub(param, paramType, paramName, methodBuilder);
            } else {
                methodBuilder.addStatement(paramName + " = data.createBooleanArray()");
            }
        } else {
            methodBuilder.addStatement(paramName + " = 0 != data.readInt()");
        }
    }

    @Override
    public void readOutParamsFromProxy(VariableElement param, ParamType paramType, MethodSpec.Builder methodBuilder) {
        if (param.asType().getKind() == TypeKind.ARRAY && paramType != ParamType.IN) {
            methodBuilder.beginControlFlow("if (" + param.getSimpleName() +" != null)");
            methodBuilder.addStatement("reply.readBooleanArray(" + param.getSimpleName() + ")");
            methodBuilder.endControlFlow();
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy