Skip to content

Commit

Permalink
add annotation and compiler module
Browse files Browse the repository at this point in the history
add example class
  • Loading branch information
Barış Söbe committed Apr 1, 2020
1 parent 572cef7 commit 05c486f
Show file tree
Hide file tree
Showing 26 changed files with 779 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion 29
Expand All @@ -27,11 +28,14 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation project(':fastbuilderannotations')
kapt project(':fastbuildercompiler')
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/bsobe/fastbuilder/model/Item.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bsobe.fastbuilder.model

import com.bsobe.fastbuilderannotations.FastBuilder

@FastBuilder
data class Item(
val name: String,
val value: Long? = null
)
8 changes: 6 additions & 2 deletions app/src/test/java/com/bsobe/fastbuilder/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.bsobe.fastbuilder

import com.bsobe.fastbuilder.model.ItemBuilder
import org.junit.Assert.assertEquals
import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
Expand All @@ -13,5 +13,9 @@ class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
val item = ItemBuilder()
.name("name")
.value(14L)
.build()
}
}
71 changes: 71 additions & 0 deletions fastbuilderannotations/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml
.idea/codeStyles/

# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md
/.idea/
9 changes: 9 additions & 0 deletions fastbuilderannotations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
sourceCompatibility = "8"
targetCompatibility = "8"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bsobe.fastbuilderannotations

@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class FastBuilder()
71 changes: 71 additions & 0 deletions fastbuildercompiler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml
.idea/codeStyles/

# Keystore files
# Uncomment the following line if you do not want to check your keystore files in.
#*.jks

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md
/.idea/
12 changes: 12 additions & 0 deletions fastbuildercompiler/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: 'java-library'
apply plugin: 'kotlin'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation project(':fastbuilderannotations')
implementation 'com.squareup:kotlinpoet:1.5.0'
implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.70"
}
sourceCompatibility = "8"
targetCompatibility = "8"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bsobe.fastbuildercompiler

import javax.annotation.processing.Messager
import javax.lang.model.element.Element
import javax.lang.model.element.TypeElement
import javax.tools.Diagnostic

object AnnotationFinder {

fun searchOn(
logger: Messager,
rootElements: Set<Element>,
supportedAnnotations: MutableSet<out TypeElement>
): Sequence<TypeElement> {
return rootElements.asSequence()
.filterIsInstance<TypeElement>()
.map { Pair(it.annotationMirrors.asSequence(), it) }
.filter {
supportedAnnotations.find { annotation ->
it.first.find { annotationMirror ->
annotationMirror.annotationType == annotation.asType()
} != null
} != null
}.map {
it.second
}
.onEach { logger.printMessage(Diagnostic.Kind.NOTE, it.toString()) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.bsobe.fastbuildercompiler

import com.bsobe.fastbuildercompiler.types.Type
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asTypeName
import javax.lang.model.element.Element
import kotlin.reflect.KTypeProjection
import kotlin.reflect.KVariance
import kotlin.reflect.full.createType

object BuilderFactory {

fun addClassProperties(element: Element, classBuilder: TypeSpec.Builder): PropertySpec {
val propertyType = element.asType().asTypeName()
KTypeProjection(KVariance.INVARIANT, Int::class.createType())
val decidedPropertyType = Type.decideType(propertyType)
val decidedType = decidedPropertyType.getKotlinType(element, propertyType)
val property = PropertySpec
.builder(
name = element.simpleName.toString(),
type = decidedType,
modifiers = *arrayOf(KModifier.PRIVATE)
)
.initializer(decidedPropertyType.getPropertyDefaultValue(element, propertyType))
.mutable(mutable = true)
.build()
classBuilder.addProperty(propertySpec = property)
return property
}

fun addClassMethod(
element: Element,
classBuilder: TypeSpec.Builder,
generatedClassName: ClassName
) {
val propertyType = element.asType().asTypeName()
val elementName = element.simpleName.toString()
val decidedPropertyType = Type.decideType(propertyType)
val decidedType = decidedPropertyType.getKotlinType(element, propertyType)
val parameterSpec = ParameterSpec
.builder(
name = elementName,
type = decidedType
)
.build()

val function = FunSpec
.builder(elementName)
.addParameter(parameterSpec)
.returns(generatedClassName)
.addStatement("return \n\tapply { this.$elementName = $elementName }")
.build()

classBuilder.addFunction(function)
}

fun addBuildMethod(
className: ClassName,
propertyNames: List<String>,
classBuilder: TypeSpec.Builder
) {
val returnStatementBuilder = StringBuilder("return ${className.simpleName}(")
propertyNames.forEachIndexed { index, propertyName ->
returnStatementBuilder.append("\n\t$propertyName = $propertyName")
if (index != propertyNames.lastIndex) {
returnStatementBuilder.append(",")
}
}
returnStatementBuilder.append("\n)")
val function = FunSpec
.builder("build")
.returns(className)
.addStatement(returnStatementBuilder.toString())
.build()
classBuilder.addFunction(function)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bsobe.fastbuildercompiler

object FileOperations {

private const val BUILDER_CLASS_NAME_SUFFIX: String = "Builder"

fun generateBuilderClassName(originalFileName: String): String =
originalFileName + BUILDER_CLASS_NAME_SUFFIX
}
Loading

0 comments on commit 05c486f

Please sign in to comment.