Skip to content

Commit

Permalink
Quick annotation processor fix
Browse files Browse the repository at this point in the history
  • Loading branch information
amarcolini committed May 14, 2022
1 parent 3c311c4 commit 1c7e751
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.amarcolini.joos.dashboard

const val joosConfigName: String = "org.firstinspires.ftc.teamcode.dashboard.JoosConfig"
const val withConfigName: String = "org.firstinspires.ftc.teamcode.dashboard.WithConfig"
const val joosConfigName: String = "com.amarcolini.joos.dashboard.JoosConfig"
const val withConfigName: String = "com.amarcolini.joos.dashboard.WithConfig"
const val disabledName: String = "com.qualcomm.robotcore.eventloop.opmode.Disabled"

const val className: String = "ConfigResults"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,33 @@ class ConfigProcessor : AbstractProcessor() {
class ConfigElementVisitor : ElementVisitor<List<Pair<String, Pair<String, String>>>, TypeElement?> {
override fun visit(p0: Element?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> = emptyList()

override fun visitPackage(p0: PackageElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> = emptyList()
override fun visitPackage(p0: PackageElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> =
emptyList()

override fun visitType(p0: TypeElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> {
val configMembers = ArrayList<Pair<String, Pair<String, String>>>()
val name = getAltName(p0, p1) ?: p0?.simpleName.toString()
p0?.enclosedElements?.forEach { member ->
if (member.kind == ElementKind.FIELD && member.modifiers.containsAll(listOf(
Modifier.PUBLIC, Modifier.STATIC
))) {
configMembers += (getAltName(member, p1) ?: name) to (p0.qualifiedName.toString() to member.simpleName.toString())
if (member.kind == ElementKind.FIELD && member.modifiers.containsAll(
listOf(
Modifier.PUBLIC, Modifier.STATIC
)
)
) {
configMembers += (getAltName(member, p1)
?: name) to (p0.qualifiedName.toString() to member.simpleName.toString())
}
}
return configMembers
}

override fun visitVariable(p0: VariableElement, p1: TypeElement?): List<Pair<String, Pair<String, String>>> {
if (p0.kind == ElementKind.FIELD && p0.modifiers.containsAll(listOf(
Modifier.PUBLIC, Modifier.STATIC
))) {
if (p0.kind == ElementKind.FIELD && p0.modifiers.containsAll(
listOf(
Modifier.PUBLIC, Modifier.STATIC
)
)
) {
var traverser: Element? = p0.enclosingElement
var possibleParent: TypeElement? = traverser as? TypeElement
if (traverser != null && possibleParent == null) {
Expand All @@ -90,20 +98,29 @@ class ConfigElementVisitor : ElementVisitor<List<Pair<String, Pair<String, Strin
}
}
val parent = possibleParent ?: return emptyList()
return listOf((getAltName(p0, p1) ?: getAltName(parent, p1) ?: parent.simpleName.toString()) to (parent.qualifiedName.toString() to p0.simpleName.toString()))
return listOf(
(getAltName(p0, p1) ?: getAltName(parent, p1)
?: parent.simpleName.toString()) to (parent.qualifiedName.toString() to p0.simpleName.toString())
)
}
return emptyList()
}

private fun getAltName(element: Element?, joosConfig: TypeElement?): String? {
if (element == null || joosConfig == null) return null
val annotation = element.annotationMirrors?.find { it.annotationType.toString() == joosConfig.qualifiedName?.toString() } ?: return null
val annotation =
element.annotationMirrors?.find { it.annotationType.toString() == joosConfig.qualifiedName?.toString() }
?: return null
return annotation.elementValues?.values?.firstOrNull()?.value?.toString()?.ifEmpty { null }
}

override fun visitExecutable(p0: ExecutableElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> = emptyList()
override fun visitExecutable(p0: ExecutableElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> =
emptyList()

override fun visitTypeParameter(p0: TypeParameterElement?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> = emptyList()
override fun visitTypeParameter(
p0: TypeParameterElement?,
p1: TypeElement?
): List<Pair<String, Pair<String, String>>> = emptyList()

override fun visitUnknown(p0: Element?, p1: TypeElement?): List<Pair<String, Pair<String, String>>> = emptyList()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import kotlin.reflect.KProperty0
import kotlin.io.*
import kotlin.reflect.KClass

class ConfigSymbolProcessor(private val environment: SymbolProcessorEnvironment, private val logger: KSPLogger = environment.logger) : SymbolProcessor {
class ConfigSymbolProcessor(
private val environment: SymbolProcessorEnvironment,
private val logger: KSPLogger = environment.logger
) : SymbolProcessor {
private val results = ArrayList<Pair<String, String>>()
private val processedClasses = ArrayList<KSClassDeclaration>()

Expand Down Expand Up @@ -47,9 +50,13 @@ class ConfigSymbolProcessor(private val environment: SymbolProcessorEnvironment,
.initializer(
CodeBlock.builder()
.beginControlFlow("try")
.addStatement("listOf(${results.joinToString {
"\"${it.first}\"" + " to " + it.second
}})")
.addStatement(
"listOf(${
results.joinToString {
"\"${it.first}\"" + " to " + it.second
}
})"
)
.nextControlFlow("catch(_: %T)", Exception::class)
.addStatement("emptyList()")
.endControlFlow()
Expand Down Expand Up @@ -103,19 +110,22 @@ class ConfigSymbolProcessor(private val environment: SymbolProcessorEnvironment,
}
val closestClass = property.closestClassDeclaration()
if (closestClass?.classKind != ClassKind.OBJECT && !property.modifiers.contains(Modifier.JAVA_STATIC)) return
val parentClass = if (closestClass?.isCompanionObject == true) closestClass.parentDeclaration as KSClassDeclaration else closestClass
val parentClass =
if (closestClass?.isCompanionObject == true) closestClass.parentDeclaration as KSClassDeclaration else closestClass
val name = property.qualifiedName ?: return
val parentClassName = parentClass?.simpleName?.asString()?.ifEmpty { null }
val parentClassAltName = parentClass?.let { getAltName(it)?.ifEmpty { null } }
val propertyAltName = getAltName(property)?.ifEmpty { null }
logger.info(property.type.toString())
results += (propertyAltName ?: parentClassAltName ?: parentClassName ?: return) to (name.getQualifier() + "::" + name.getShortName())
results += (propertyAltName ?: parentClassAltName ?: parentClassName
?: return) to (name.getQualifier() + "::" + name.getShortName())
}

private fun getAltName(node: KSAnnotated): String? {
val joosConfig = joosConfig
return if (joosConfig != null) {
node.annotations.find { it.shortName == joosConfig.simpleName && it.annotationType.resolve().declaration.qualifiedName == joosConfig.qualifiedName }?.arguments?.get(0)?.value?.toString()
node.annotations.find { it.shortName == joosConfig.simpleName && it.annotationType.resolve().declaration.qualifiedName == joosConfig.qualifiedName }?.arguments?.get(
0
)?.value?.toString()
} else null
}

Expand Down

0 comments on commit 1c7e751

Please sign in to comment.