-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example class
- Loading branch information
Barış Söbe
committed
Apr 1, 2020
1 parent
572cef7
commit 05c486f
Showing
26 changed files
with
779 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
5 changes: 5 additions & 0 deletions
5
fastbuilderannotations/src/main/java/com/bsobe/fastbuilderannotations/FastBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
29 changes: 29 additions & 0 deletions
29
fastbuildercompiler/src/main/java/com/bsobe/fastbuildercompiler/AnnotationFinder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) } | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
fastbuildercompiler/src/main/java/com/bsobe/fastbuildercompiler/BuilderFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
fastbuildercompiler/src/main/java/com/bsobe/fastbuildercompiler/FileOperations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.