processor


License

License

Categories

Categories

Kotlin Languages Auto Application Layer Libs Code Generators
GroupId

GroupId

com.github.stoyicker.auto-factory-kotlin
ArtifactId

ArtifactId

processor
Last Version

Last Version

0.0.2
Release Date

Release Date

Type

Type

jar
Description

Description

processor
processor
Project URL

Project URL

https://github.com/stoyicker/auto-factory-kotlin
Source Code Management

Source Code Management

https://github.com/stoyicker/auto-factory-kotlin

Download processor

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.stoyicker.auto-factory-kotlin/processor/ -->
<dependency>
    <groupId>com.github.stoyicker.auto-factory-kotlin</groupId>
    <artifactId>processor</artifactId>
    <version>0.0.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.stoyicker.auto-factory-kotlin/processor/
implementation 'com.github.stoyicker.auto-factory-kotlin:processor:0.0.2'
// https://jarcasting.com/artifacts/com.github.stoyicker.auto-factory-kotlin/processor/
implementation ("com.github.stoyicker.auto-factory-kotlin:processor:0.0.2")
'com.github.stoyicker.auto-factory-kotlin:processor:jar:0.0.2'
<dependency org="com.github.stoyicker.auto-factory-kotlin" name="processor" rev="0.0.2">
  <artifact name="processor" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.stoyicker.auto-factory-kotlin', module='processor', version='0.0.2')
)
libraryDependencies += "com.github.stoyicker.auto-factory-kotlin" % "processor" % "0.0.2"
[com.github.stoyicker.auto-factory-kotlin/processor "0.0.2"]

Dependencies

compile (1)

Group / Artifact Type Version
com.squareup : kotlinpoet jar 1.4.1

runtime (1)

Group / Artifact Type Version
com.github.stoyicker.auto-factory-kotlin : annotations jar 0.0.2

Project Modules

There are no modules declared in this project.

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.

Versions

Version
0.0.2
0.0.1