YABK

last modified: 2-jul-2017 (23:31)

YABK (Yet Another Boilerplate Killer) is an annotation processor for generating mutators/accessor and parcelable implementation of java POJOs.

Background

For the applications which depend heavily on WEB APIs, we have to model network responses and in Android sometimes we need to make them Parcel too.

A lot of these models, doesn’t contain any logic but methods for accessing or setting fields. Generating these fields or parcelables every time we have to update the model is cumbersome.

YABK addresses this issue and generates accessor/mutators and parcelable implementation of models at compile time.

Getting Started

For processing any POJO with Yabk, add @YabkProcess annotation on required class.

@YabkProcess
abstract class $CustomerCodeModel {

    String customerId;
    String status;

}

Yabk generated class for above example

public class CustomerCodeModel extends $CustomerCodeModel implements Parcelable {
  public static final Creator<CustomerCodeModel> CREATOR = new Creator<CustomerCodeModel>() {
          @Override
          public CustomerCodeModel createFromParcel(Parcel in) {
              return new CustomerCodeModel(in);
          }

          @Override
          public CustomerCodeModel[] newArray(int size) {
              return new CustomerCodeModel[size];
          }
      };

  public CustomerCodeModel() {
  }

  protected CustomerCodeModel(Parcel in) {
    this.status = in.readString();
    this.customerId = in.readString();
  }

  public void setStatus(String status) {
    this.status = status;
  }

  public String getStatus() {
    return this.status;
  }

  public void setCustomerId(String customerId) {
    this.customerId = customerId;
  }

  public String getCustomerId() {
    return this.customerId;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(status);
    dest.writeString(customerId);
  }

  @Override
  public int describeContents() {
    return 0;
  }
}

Class rules

Class naming rules

Generating required methods only

Yabk provides option for generating only accessor or mutator methods. By default it generates both getters and setters. e.g.

For generating only accessors, annotate the class with following annotation

@YabkProcess(methods = Methods.ACCESSORS)

For mutators only, use following annotation

@YabkProcess(methods = Methods.MUTATORS)

Replacing null strings with empty strings

For generating accessor methods which check for null strings and convert them to empty strings, specify nonNullStrings = true in @YabkProcess annotation. e.g.

@YabkProcess(nonNullStrings = true)

Limitations

Add to Gradle

Add following lines to your app level dependency block

compile  'io.github.allaudin:yabk:latest-version'
annotationProcessor  'io.github.allaudin:yabk-compiler:latest-version'

Download Lastest Version

Yabk

Yabk Compiler

License

Copyright 2017 M.Allaudin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.