All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jtransc.ast.ast_annotation.kt Maven / Gradle / Ivy

package com.jtransc.ast

import com.jtransc.ds.stripNulls
import com.jtransc.ds.toTypedArray2
import kotlin.reflect.KProperty1

data class AstAnnotation(
	val type: AstType.REF,
	val elements: Map,
    val runtimeVisible: Boolean
) {
	private fun getAllDescendantAnnotations(value: Any?): List {
		return when (value) {
			is AstAnnotation -> getAllDescendantAnnotations(elements.values.toList())
			is List<*> -> value.filterIsInstance() + value.filterIsInstance>().flatMap { getAllDescendantAnnotations(it) }
			else -> listOf()
		}
	}

	fun getAllDescendantAnnotations(): List {
		var out = arrayListOf()
		out.add(this)
		out.addAll(getAllDescendantAnnotations(this))
		return out
	}
}

operator fun List?.get(name: FqName): AstAnnotation? {
	val ref = AstType.REF(name)
	return this?.firstOrNull { it.type == ref }
}

operator fun List?.get(name: FqName, field: String): Any? {
	val value = this?.get(name)?.elements?.get(field)
	return when (value) {
		null -> null
		is List<*> -> value.toTypedArray2()
		else -> value
	}
}

operator fun List?.contains(name: FqName): Boolean {
	return this.get(name) != null
}

operator inline fun List?.contains(clazz: Class): Boolean {
	return FqName(clazz.name) in this
}

inline fun  List?.contains2(): Boolean {
	return FqName(T::class.java.name) in this
}

operator inline fun  List?.get(field: KProperty1): T? {
	val cClass = C::class.java
	val tClass = T::class.java
	val value = this?.get(cClass.name.fqname, field.name)
	val valueClass = value?.javaClass
	if (valueClass != null && valueClass != tClass) {
		if (tClass.isArray && valueClass.isArray) {
			if (java.lang.reflect.Array.getLength(value) == 0) {
				return java.lang.reflect.Array.newInstance(tClass.componentType, 0) as T?
			}
		}
		println("different! $valueClass, $tClass")
	}
	return value as T?
}

inline fun  List.getAnnotation(field: KProperty1): List {
	return this.map { it.annotations.get(field) }.stripNulls()
}

inline fun  List?.contains(): Boolean {
	return C::class.java.name.fqname in this
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy