All Projects → ThinkingLogic → Kotlin Builder Annotation

ThinkingLogic / Kotlin Builder Annotation

Licence: mit
A minimal viable replacement for the Lombok @Builder plugin for Kotlin code

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin Builder Annotation

symbok-bundle
Symfony annotations bundle
Stars: ✭ 50 (-25.37%)
Mutual labels:  lombok, annotations
AnnotationProcessorStarter
Project to set up basics of a Java annotation processor
Stars: ✭ 19 (-71.64%)
Mutual labels:  annotations, annotation-processor
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (-58.21%)
Mutual labels:  annotations, annotation-processor
Kpoet
An expressive DSL built on top of JavaPoet to make writing code almost as easy as writing the code yourself.
Stars: ✭ 58 (-13.43%)
Mutual labels:  annotation-processor, annotations
dagger2-ktx
Kotlin extension bridge library for Dagger2 (proof-of-concept)
Stars: ✭ 41 (-38.81%)
Mutual labels:  annotations, annotation-processor
Library-Spring
The library web application where you can borrow books. It's Spring MVC and Hibernate project.
Stars: ✭ 73 (+8.96%)
Mutual labels:  lombok, annotations
AnnotationProcessing
✔️ㅤ[ARTICLE] Writing your own Annotation Processors in Android
Stars: ✭ 47 (-29.85%)
Mutual labels:  annotations, annotation-processor
Placeholderview
This library provides advance views for lists and stacks. Some of the views are build on top of RecyclerView and others are written in their own. Annotations are compiled by annotation processor to generate bind classes. DOCS -->
Stars: ✭ 2,104 (+3040.3%)
Mutual labels:  annotation-processor, annotations
Zerocell
Simple, efficient Excel to POJO library for Java
Stars: ✭ 53 (-20.9%)
Mutual labels:  annotation-processor, annotations
AutoBindings
Set of annotations that aims to make your Android development experience easier along with lint checks.
Stars: ✭ 15 (-77.61%)
Mutual labels:  annotations, annotation-processor
Kripton
A Java/Kotlin library for Android platform, to manage bean's persistence in SQLite, SharedPreferences, JSON, XML, Properties, Yaml, CBOR.
Stars: ✭ 110 (+64.18%)
Mutual labels:  annotation-processor, annotations
Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (+408.96%)
Mutual labels:  annotation-processor, annotations
simple-preferences
Android Library to simplify SharedPreferences use with code generation.
Stars: ✭ 48 (-28.36%)
Mutual labels:  annotations, annotation-processor
ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (-41.79%)
Mutual labels:  annotations, annotation-processor
WinAnalytics
A light-weight android library that can be quickly integrated into any app to use analytics tools.
Stars: ✭ 23 (-65.67%)
Mutual labels:  annotations, annotation-processor
Gsonpath
A Java annotation processor library which generates gson type adapters using basic JsonPath style annotations
Stars: ✭ 54 (-19.4%)
Mutual labels:  annotation-processor, annotations
Tgm
Team Game Manager - Minecraft PVP Suite
Stars: ✭ 35 (-47.76%)
Mutual labels:  lombok
Metasra Pipeline
MetaSRA: normalized sample-specific metadata for the Sequence Read Archive
Stars: ✭ 33 (-50.75%)
Mutual labels:  annotation-processor
Hamcrest Pojo Matcher Generator
Autogenerated java hamcrest matchers for pojo with help of AnnotationProcessor
Stars: ✭ 31 (-53.73%)
Mutual labels:  annotation-processor
Inspector
A tiny class validation library.
Stars: ✭ 64 (-4.48%)
Mutual labels:  annotation-processor

kotlin-builder-annotation

A builder annotation for Kotlin interoperability with Java. This project aims to be a minimal viable replacement for the Lombok @Builder plugin for Kotlin code.

Build Status License

Usage

Import kotlin-builder-annotation and kotlin-builder-processor

And configure the Kotlin annotation processor (kapt).

Gradle
...
apply plugin: 'kotlin-kapt'
...
dependencies {
    ...
    implementation 'com.thinkinglogic.builder:kotlin-builder-annotation:1.2.1'
    kapt 'com.thinkinglogic.builder:kotlin-builder-processor:1.2.1'
}
Maven
...
<dependencies>
    <dependency>
        <groupId>com.thinkinglogic.builder</groupId>
        <artifactId>kotlin-builder-annotation</artifactId>
        <version>1.2.1</version>
    </dependency>
    ...
</dependencies>
...
<execution>
    <id>kapt</id>
    <goals>
        <goal>kapt</goal>
    </goals>
    <configuration>
        <sourceDirs>
            <sourceDir>src/main/kotlin</sourceDir>
            <sourceDir>src/main/java</sourceDir>
        </sourceDirs>
        <annotationProcessorPaths>
            <!-- Specify your annotation processors here. -->
            <annotationProcessorPath>
                <groupId>com.thinkinglogic.builder</groupId>
                <artifactId>kotlin-builder-processor</artifactId>
                <version>1.2.1</version>
            </annotationProcessorPath>
        </annotationProcessorPaths>
    </configuration>
</execution>

Annotate your class(es) with the @Builder annotation

import com.thinkinglogic.builder.annotation.Builder

@Builder
data class MyDataClass(
        val notNullString: String,
        val nullableString: String?
)

That's it! Client code can now use a builder to construct instances of your class.

Unlike Lombok there's no bytecode manipulation, so we don't expose a MyDataClass.builder() static method. Instead clients create a new MyDataClassBuilder(), for instance:

public class MyDataFactory {
    public MyDataClass create() {
        return new MyDataClassBuilder()
                .notNullString("Foo")
                .nullableString("Bar")
                .build();
    }
}

The builder will check for required fields, so
new MyDataClassBuilder().notNullString(null);
would throw an IllegalArgumentException and
new MyDataClassBuilder().nullableString("Bar").build();
would throw an IllegalStateException naming the required field ('notNullString' in this case), while
new MyDataClassBuilder().notNullString("Foo").build();
would return a new instance with a null value for 'nullableString'.

To replace Kotlin's copy() (and Lombok's toBuilder()) method, clients can pass an instance of the annotated class when constructing a builder: new MyDataClassBuilder(myDataClassInstance) - the builder will be initialised with values from the instance.

Default values

Kotlin doesn't retain information about default values after compilation, so it cannot be accessed during annotation processing. Instead we must use the @DefaultValue annotation to tell the builder about it:

import com.thinkinglogic.builder.annotation.Builder
import com.thinkinglogic.builder.annotation.DefaultValue

@Builder
data class MyDataClass(
        val notNullString: String,
        val nullableString: String?,
        @DefaultValue("myDefaultValue") val stringWithDefault: String = "myDefaultValue",
        @DefaultValue("LocalDate.MIN") val defaultDate: LocalDate = LocalDate.MIN
)

(The text value of @DefaultValue is interpreted directly as Kotlin code, but for convenience double quotes are added around a String value).

Collections containing nullable elements

Information about the nullability of elements in a collection is lost during compilation, so there is a @NullableType annotation:

import com.thinkinglogic.builder.annotation.Builder
import com.thinkinglogic.builder.annotation.NullableType

@Builder
data class MyDataClass(
        val setOfLongs: Set<Long>,
        @NullableType val setOfNullableLongs: Set<Long?>
)

Mutable collections

Information about the mutability of collections and maps is lost during compilation, so there is an @Mutable annotation:

import com.thinkinglogic.builder.annotation.Builder
import com.thinkinglogic.builder.annotation.Mutable

@Builder
data class MyDataClass(
        val setOfLongs: Set<Long>,
        @Mutable val listOfStrings: MutableList<String>
)

Constructor parameters

The @Builder annotation may be placed on a constructor instead of the class - useful if you have constructor-only parameters:

import com.thinkinglogic.builder.annotation.Builder

class MyClass
@Builder
constructor(
        forename: String,
        surname: String,
        val nickName: String?
) {
    val fullName = "$forename $surname"
}

builder() and toBuilder() methods

The @Builder annotation processor cannot modify bytecode, so it cannot generate builder() and toBuilder() methods for you, but you can add them yourself:

import com.thinkinglogic.builder.annotation.Builder

@Builder
data class MyDataClass(
        val notNullString: String,
        val nullableString: String?
) {

     fun toBuilder(): MyDataClassBuilder = MyDataClassBuilder(this)
 
     companion object {
         @JvmStatic fun builder() = MyDataClassBuilder()
     }
 }

MyDataClass.builder() and myDataClassObject.toBuilder() can now be invoked from java, enabling a complete drop-in replacement for the Lombok @Builder annotation.


Examples of all of the above may be found in the kotlin-builder-example-usage sub-project.

License

This software is Licenced under the MIT License.

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].