Skip to content

Commit

Permalink
data class generation added
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanTcv committed Dec 13, 2020
1 parent eadb673 commit 7af95fb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
15 changes: 12 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ package com.r12.mutable.annotations
*
* className - Your own custom class name
* mutableSuffix - If className is blank will generate default name, for instance - MutableInspection or InspectionMutable.
* dataClass - generate data class or not
*/
@Retention(AnnotationRetention.SOURCE)
@Target(allowedTargets = [AnnotationTarget.CLASS])
annotation class Mutable(val className: String = "", val mutableSuffix: Boolean = true)
annotation class Mutable(
val className: String = "",
val mutableSuffix: Boolean = true,
val dataClass: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ internal fun Element.fileName(): String {

internal fun Element.getPackage(elements: Elements): String = elements.packageOf(this)

internal fun Elements.packageOf(element: Element) = getPackageOf(element).toString()
internal fun Elements.packageOf(element: Element) = getPackageOf(element).toString()

internal fun Element.isDataClass(): Boolean = getAnnotation(Mutable::class.java).dataClass
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.r12.mutable.processor.entities.Field
import com.r12.mutable.processor.extensions.fileName
import com.r12.mutable.processor.extensions.formatType
import com.r12.mutable.processor.extensions.getPackage
import com.r12.mutable.processor.extensions.isDataClass
import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.metadata.ImmutableKmClass
import com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview
Expand All @@ -23,13 +24,14 @@ internal class InterfaceGeneratorFactory(

val fields = element.getFields()
val isAbstract = element.isAbstract()
val isDataClass = !isAbstract && element.isDataClass()

val typeSpec = TypeSpec
.classBuilder(fileName)
.addSuperinterface(kmClass.superInterface())
.addProperties(fields.map(Field::toPropertySpec))
.primaryConstructor(fields.generateConstructor())
.addModifiers(kmClass.getModifiers(isAbstract))
.addModifiers(kmClass.getModifiers(isAbstract, isDataClass))
.addExtension(element, fullPath, isAbstract)
.build()

Expand Down Expand Up @@ -87,10 +89,15 @@ internal class InterfaceGeneratorFactory(
return this
}

private fun ImmutableKmClass.getModifiers(isAbstract: Boolean): List<KModifier> {
private fun ImmutableKmClass.getModifiers(isAbstract: Boolean, isDataClass: Boolean): List<KModifier> {
val list = mutableListOf<KModifier>()

if (isAbstract) list.add(KModifier.ABSTRACT) else list.add(KModifier.OPEN)
val kModifier = if (isAbstract) {
KModifier.ABSTRACT
} else {
if (isDataClass) KModifier.DATA else KModifier.OPEN
}
list.add(kModifier)
if (isInternal) list.add(KModifier.INTERNAL)

return list
Expand Down

0 comments on commit 7af95fb

Please sign in to comment.