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

com.workday.postman.codegen.ParcelableSaveStatementWriter Maven / Gradle / Ivy

Go to download

A java library that uses code generation to handle the details of implementing the Parcelable interface on Android.

There is a newer version: 1.3
Show newest version
/*
 * Copyright 2015 Workday, Inc.
 *
 * This software is available under the MIT license.
 * Please see the LICENSE.txt file in this project.
 */

package com.workday.postman.codegen;

import com.squareup.javawriter.JavaWriter;
import com.workday.meta.MetaTypes;

import java.io.IOException;
import java.util.Collection;

import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;

/**
 * @author nathan.taylor
 * @since 2013-12-30
 */
class ParcelableSaveStatementWriter
        implements SaveStatementWriter {

    private final MetaTypes metaTypes;

    ParcelableSaveStatementWriter(MetaTypes metaTypes) {
        this.metaTypes = metaTypes;
    }

    @Override
    public boolean isApplicable(VariableElement field) {
        return metaTypes.isSubtype(field.asType(), Names.PARCELABLE);
    }

    @Override
    public void writeFieldReadStatement(VariableElement field,
                                        Collection postCreateChildMethods,
                                        JavaWriter writer)
            throws IOException {

        writer.emitStatement("object.%s = bundle.getParcelable(\"%s\")",
                             field.getSimpleName(),
                             field.getSimpleName());

        for (ExecutableElement method : postCreateChildMethods) {
            writer.emitStatement("object.%s(object.%s)",
                                 method.getSimpleName(),
                                 field.getSimpleName());
        }
    }

    @Override
    public void writeFieldWriteStatement(VariableElement field, JavaWriter writer)
            throws IOException {
        writer.emitStatement("bundle.putParcelable(\"%s\", object.%s)",
                             field.getSimpleName(),
                             field.getSimpleName());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy