All Projects → mirrajabi → annotation-processor-sample

mirrajabi / annotation-processor-sample

Licence: Apache-2.0 License
An annotation processor which implements "Builder pattern" for your java classes.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to annotation-processor-sample

AnnotationProcessing
✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Stars: ✭ 47 (+113.64%)
Mutual labels:  annotation, annotation-processor
MethodScope
Reduce repetitive inheritance works in OOP world using @MethodScope.
Stars: ✭ 33 (+50%)
Mutual labels:  annotation, annotation-processor
simple-annotation-processor
Simple annotation processor example. Inspired by the idea of "How ButterKnife works?"
Stars: ✭ 54 (+145.45%)
Mutual labels:  annotation, annotation-processor
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (+27.27%)
Mutual labels:  annotation, annotation-processor
AnnotationProcessorStarter
Project to set up basics of a Java annotation processor
Stars: ✭ 19 (-13.64%)
Mutual labels:  annotation, annotation-processor
RapidORM
Quick solutions for Android ORM
Stars: ✭ 24 (+9.09%)
Mutual labels:  annotation-processor
delse
PyTorch code for Deep Extreme Level Set Evolution (CVPR 2019)
Stars: ✭ 62 (+181.82%)
Mutual labels:  annotation
smartstruct
Dart Code Generator for generating mapper classes
Stars: ✭ 20 (-9.09%)
Mutual labels:  annotation-processor
PinFloyd
MapKit annotations clustering for iOS
Stars: ✭ 29 (+31.82%)
Mutual labels:  annotation
neogen
A better annotation generator. Supports multiple languages and annotation conventions.
Stars: ✭ 339 (+1440.91%)
Mutual labels:  annotation
redcoat
A lightweight web-based annotation tool for labelling entity recognition data.
Stars: ✭ 19 (-13.64%)
Mutual labels:  annotation
TextGridTools
Read, write, and manipulate Praat TextGrid files with Python
Stars: ✭ 84 (+281.82%)
Mutual labels:  annotation
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (+86.36%)
Mutual labels:  annotation-processor
kotlin-cursor
Kotlin Annotation Processor to generate fromCursor and toContentValues of data classes.
Stars: ✭ 34 (+54.55%)
Mutual labels:  annotation-processor
acl2020-interactive-entity-linking
No description or website provided.
Stars: ✭ 26 (+18.18%)
Mutual labels:  annotation
BalloonPopup
Forget Android Toast! BalloonPopup displays a round or squared popup and attaches it to a View, like a callout. Uses the Builder pattern for maximum ease. The popup can automatically hide and can persist when the value is updated.
Stars: ✭ 32 (+45.45%)
Mutual labels:  builder-pattern
companion
This repository has been archived, currently maintained version is at https://github.com/iii-companion/companion
Stars: ✭ 21 (-4.55%)
Mutual labels:  annotation
WinAnalytics
A light-weight android library that can be quickly integrated into any app to use analytics tools.
Stars: ✭ 23 (+4.55%)
Mutual labels:  annotation-processor
Dexter
Manage multidexing using simple annotations and gradle tasks.
Stars: ✭ 41 (+86.36%)
Mutual labels:  annotation-processor
open-semantic-desktop-search
Virtual Machine for Desktop Search with Open Semantic Search
Stars: ✭ 22 (+0%)
Mutual labels:  annotation

annotation-processor-sample

This is not a "Builder pattern" tutorial or something like that. It is about how we can make an annotation processor to avoid boilerplates and repeated codes.

What does this apt do?

Well lets take a look at a very simple type of bean with getters and builder setters:

public class TestClass {
    private long id;
    private String name;
    private int someField;
    private Activity activity;

    public long getId() {
        return id;
    }

    public TestClass setId(long id) {
        this.id = id;
        return this;
    }

    public String getName() {
        return name;
    }

    public TestClass setName(String name) {
        this.name = name;
        return this;
    }

    public int getSomeField() {
        return someField;
    }

    public TestClass setSomeField(int someField) {
        this.someField = someField;
        return this;
    }

    public Activity getActivity() {
        return activity;
    }

    public TestClass setActivity(Activity activity) {
        this.activity = activity;
        return this;
    }
}

And we instantiate this class like

TestClass testClass = new TestClass()
    ...
    .setSomeField(...)
    .setName(...)

So we have four fields and implemented 4+4 getter and setter methods. Now, what if we had 15 fields? we would have to make 15 getters and 15 setters. That's a total of 30! Of course today its very easy to use IDEs to make that boilerplate for us. but this is just an example of how much we use boilerplate codes. there are a whole lot of other examples for this topic. For example take a look at ButterKnife. This library took us out of the darkness. It made our world a better place.

with this apt(Annotation Processing Tool) all you have to do is to annotate your POJO with @Builder and make our fields non private

@Builder
public class TestClass {
    long id;
    String name;
    int someField;
    Activity activity;
}

@Builder does no magic. it just creates all the boilerplates we need and puts them inside another java class called %CLASS_NAME%Builder.java under app/build/generated/source/apt/debug/%PACKAGE_NAME%/ folder.

For example TestClassBuilder.java looks like this :

// This file is auto-generated and should not be edited.
package ir.mirrajabi.aptsample;

import android.app.Activity;
import java.lang.String;

public class TestClassBuilder {
  private TestClass buildable;

  private TestClassBuilder() {
  }

  public TestClassBuilder someField(int someField) {
    buildable.someField = someField;
    return this;
  }

  public TestClassBuilder activity(Activity activity) {
    buildable.activity = activity;
    return this;
  }

  public TestClassBuilder id(long id) {
    buildable.id = id;
    return this;
  }

  public TestClassBuilder name(String name) {
    buildable.name = name;
    return this;
  }

  public static TestClassBuilder having() {
    return new TestClassBuilder();
  }

  TestClass get() {
    return this.buildable;
  }
}

And we make and instance of our TestClass like this:

TestClass testClass = TestClassBuilder.having()
                .activity(this)
                .someField(5)
                .id(654)
                .name("Sample")
                .get();

But remember, we just annotated our class with @Builder and nothing else. the compiler did the rest for us.

I guess thats enough for a readme :))

Useful reads

Used libraries

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].