0

Currently I'm trying this way:

element.enclosedElements.filter { it.kind == ElementKind.FIELD }.filter {
            it.modifiers.contains(Modifier.FINAL) &&
                    !it.modifiers.contains(Modifier.STATIC)
        }

but the problem with this way is that it is also returned member fields that are not in the constructor.

data class Post(
    val id: Int,
    val title: String,
    val content: String?,
    val a: Char,
    val b: Char?,
    val author_id: Int?,
    val is_public: Boolean,
    val is_updated: Boolean?,
) {
    private val myCustomField = true // I don't need this field when processing this class in annotation processor.
}

Note: I've tried this library: https://github.com/Takhion/kotlin-metadata but this library doesn't support the latest version of Kotlin.

Thanks.

Riajul
  • 1,140
  • 15
  • 20
  • Assuming there is only one public constructor, `(element.enclosedElements.single { it.kind == ElementKind.CONSTRUCTOR && it.modifiers.contains(Modifier.PUBLIC) } as ExecutableElement).parameters` should do the thing – Михаил Нафталь Mar 27 '21 at 11:05
  • Thanks, your suggestions were right, you might want to post the comment as an answer. @МихаилНафталь – Riajul Mar 29 '21 at 04:26

1 Answers1

1

Assuming there is only one public constructor (will throw exceptionIllegalArgumentException if more than one constructor exists):

(element.enclosedElements.single { 
    it.kind == ElementKind.CONSTRUCTOR && it.modifiers.contains(Modifier.PUBLIC) 
} as ExecutableElement).parameters

Getting all public constructors.

val publicConstructors = element.enclosedElements.filter {
    it.kind == ElementKind.CONSTRUCTOR && it.modifiers.contains(Modifier.PUBLIC)
}.map { it as ExecutableElement }
Riajul
  • 1,140
  • 15
  • 20