Giter VIP home page Giter VIP logo

auto-factory-kotlin's Introduction

auto-factory-kotlin

An annotation processor for Kotlin and Android that generates factories for your code. Compatible with auto-factory out of the box.

CircleCI codecov

Usage

Download

Add the dependencies to your project:

Kotlin
repositories {
  jcenter()
}
dependencies {
    // 1. Add the annotations
    compileOnly "com.github.stoyicker.auto-factory-kotlin:annotations:<version>"
    // 2. Add the processor (you must be using kotlin-kapt)
    kapt "com.github.stoyicker.auto-factory-kotlin:processor:<version>"
}
Groovy
repositories {
  jcenter()
}
dependencies {
    // 1. Add the annotations
    compileOnly("com.github.stoyicker.auto-factory-kotlin:annotations:<version>")
    // 2. Add the processor (you must be using kotlin-kapt)
    kapt("com.github.stoyicker.auto-factory-kotlin:processor:<version>")
}

Now annotate your class:

@AutoFactory
class SomeClass(@Provided @Named("AQualifier") private val providedDepA: String, private val providedDepB: String)

The following code will be generated:

@Generated(value = "org.github.stoyicker.auto.factory.kotlin.processor.AutoFactoryKotlinProcessor")
@Inject
class SomeClassFactory(@Named("AQualifier") private val providedDepAProvider: Provider<String>) {
  fun create(depB: String) = SomeClass(providedDepAProvider.get(), depB)
}

Features

Annotation targets

Both classes and typealias can be annotated. If you annotate a class, the generated factory contains methods for all of the public and internal constructors in the target class. If you annotate a typealias, only methods for public constructors will be generated to ensure that there are no scenarios where compilation could be broken.

class SomeClass(private val dep: String) {
  @AutoFactory
  constructor() : this("a value")
}

The following code will be generated:

@Generated(value = "org.github.stoyicker.auto.factory.kotlin.processor.AutoFactoryKotlinProcessor")
@Inject
class SomeClassFactory {
  fun create() = SomeClass()
}

Generics

Generics are 100% respected. For example, with this code:

class SomeClass<A : Any> {
  @AutoFactory
  constructor(a: A, list: List<String>) : this()
}

The following code will be generated:

@Generated(value = "org.github.stoyicker.auto.factory.kotlin.processor.AutoFactoryKotlinProcessor")
@Inject
class SomeClassFactory<A : Any> {
  fun create(a: A, list: List<String>) = SomeClass(a, list)
}

Factories for external classes

Factories for types whose source is out of your control can be requested via typealias. For example, you can request a factory for the public Any constructors like this:

@AutoFactory(name = "AnyFactory") // name is not mandatory, but it is a good idea to keep name consistent with the default rules
typealias AnyAlias = Any

The following code will be generated:

@Generated(value = "org.github.stoyicker.auto.factory.kotlin.processor.AutoFactoryKotlinProcessor")
@Inject
class AnyAliasFactory {
  fun create() = AnyAlias()
}

Default arguments

Default arguments are respected. If a factory method is generated for a constructor which has at least one argument with default values, such defaults will be honored in the generated code. For example, if you have:

@AutoFactory
class SomeClass(private val dep: String = "hello default arguments")

The following code will be generated:

@Generated(value = "org.github.stoyicker.auto.factory.kotlin.processor.AutoFactoryKotlinProcessor")
@Inject
class SomeClassFactory {
  fun create(dep: String = "hello default arguments") = SomeClass(dep)
}

Annotations and options

See the annotations for documentation.

License

Copyright 2019 Jorge Antonio Diaz-Benito Soriano

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.

The annotations module is derived from the annotations at google/auto with minor modifications to their source documentation.

auto-factory-kotlin's People

Contributors

stoyicker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

auto-factory-kotlin's Issues

TODO

  • Replace the test output folder in the CircleCI yaml
  • Replace properties in dependencies ("a.b.c" -> rootProject."a.b.c")
  • The accessor of the generated class should be the same as the class annotated (or the target class of the typealias)
  • Throw an error if the same class that is annotated also has one or more of its constructors annotated
  • License
  • Respect qualifiers in arguments
  • Create Generated
  • Annotating a class works
  • Annotating a typealias works
  • Respect default parameters
  • Generics in class definition and constructor. Also respect parameters with generics.
  • Tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.