Skip to content

Commit

Permalink
feat: java field parsing in annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Apr 6, 2024
1 parent 4e96bd6 commit 4fc138b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ open class GenerateLoadersEntryPointsTask : DefaultTask() {
val sourceFiles = HashSet(mainSourceSet.java.sourceDirectories.asFileTree.files)
.plus(kotlinMainSourceSet.kotlin.sourceDirectories.asFileTree.files)

val addons = sourceFiles.mapNotNull { it.parseAddonMeta() }
val addons = sourceFiles.mapNotNull { it.parseAddonMeta(sourceFiles) }
if (addons.isEmpty()) return

AddonEntryPoint.processAddons(project, addons)
File(project.buildDir, CACHE_FILE_PATH).writeText("")
// File(project.buildDir, CACHE_FILE_PATH).writeText("")
}

companion object {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/su/plo/voice/plugin/parser/AddonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import java.io.File

interface AddonParser {

fun parseAddon(file: File): AddonMeta?
fun parseAddon(sourceFiles: Collection<File>, file: File): AddonMeta?
}
30 changes: 30 additions & 0 deletions src/main/kotlin/su/plo/voice/plugin/parser/FieldParser.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package su.plo.voice.plugin.parser

import com.github.javaparser.StaticJavaParser
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration
import java.io.File
import kotlin.jvm.optionals.getOrNull

fun parseStringField(sourceFiles: Collection<File>, className: String, fieldName: String): String {
sourceFiles.forEach { file ->
val fileName = file.name

if (fileName.endsWith(".java")) {
val parsedFile = StaticJavaParser.parse(file)

val foundClass = parsedFile.childNodes
.filterIsInstance<ClassOrInterfaceDeclaration>()
.firstOrNull { it.nameAsString == className }
?: return@forEach

val field = foundClass.getFieldByName(fieldName).getOrNull() ?: return@forEach

return field.getVariable(0).initializer.get().asStringLiteralExpr().asString()
}
// else if (fileName.endsWith(".kt")) {
//
// }
}

throw IllegalStateException("Field $fieldName in $className not found")
}
13 changes: 10 additions & 3 deletions src/main/kotlin/su/plo/voice/plugin/parser/JavaAddonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import java.io.File

object JavaAddonParser : AddonParser {

override fun parseAddon(file: File): AddonMeta? {
override fun parseAddon(sourceFiles: Collection<File>, file: File): AddonMeta? {
val parsedFile = try {
StaticJavaParser.parse(file)
// todo: "Text Block Literals are not supported." Just skip for now, but it can be a problem with modern java code
Expand All @@ -25,6 +25,7 @@ object JavaAddonParser : AddonParser {
.getOrNull(0) ?: return@forEach

return parseAddon(
sourceFiles,
addonAnnotation,
parsedFile.packageDeclaration.get().nameAsString + "." + node.nameAsString
)
Expand All @@ -34,7 +35,7 @@ object JavaAddonParser : AddonParser {
}
}

private fun parseAddon(addonAnnotation: AnnotationExpr, entryPoint: String): AddonMeta {
private fun parseAddon(sourceFiles: Collection<File>, addonAnnotation: AnnotationExpr, entryPoint: String): AddonMeta {
var id: String? = null
var name: String? = null
var loaderScope: AddonLoaderScope? = null
Expand Down Expand Up @@ -62,7 +63,13 @@ private fun parseAddon(addonAnnotation: AnnotationExpr, entryPoint: String): Add
}

"version" -> {
version = (annotationNode.value as StringLiteralExpr).asString()
version = if (annotationNode.value.isFieldAccessExpr) {
val fieldAccess = annotationNode.value.asFieldAccessExpr()

parseStringField(sourceFiles, fieldAccess.scope.toString(), fieldAccess.nameAsString)
} else {
(annotationNode.value as StringLiteralExpr).asString()
}
}

"license" -> {
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/su/plo/voice/plugin/parser/KotlinAddonParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.File

object KotlinAddonParser : AddonParser {

override fun parseAddon(file: File): AddonMeta? {
override fun parseAddon(sourceFiles: Collection<File>, file: File): AddonMeta? {
val source = AstSource.File(file.absolutePath.toString())

val ast = KotlinGrammarAntlrKotlinParser.parseKotlinFile(source)
Expand All @@ -40,6 +40,7 @@ object KotlinAddonParser : AddonParser {
.getOrNull(0) ?: return@forEach

return parseAddon(
sourceFiles,
addonAnnotation,
packageName!! + "." + astFile.identifier!!.identifier
)
Expand All @@ -48,7 +49,7 @@ object KotlinAddonParser : AddonParser {
return null
}

private fun parseAddon(addonAnnotation: KlassAnnotation, entryPoint: String): AddonMeta? {
private fun parseAddon(sourceFiles: Collection<File>, addonAnnotation: KlassAnnotation, entryPoint: String): AddonMeta? {
var id: String? = null
var name: String? = null
var loaderScope: AddonLoaderScope? = null
Expand Down Expand Up @@ -78,7 +79,13 @@ object KotlinAddonParser : AddonParser {
}

"version" -> {
version = getString(expression)
version = if (expression is KlassIdentifier) {
val terminals = findChild(expression, DefaultAstTerminal::class.java)

parseStringField(sourceFiles, expression.identifier, terminals[1].text)
} else {
getString(expression)
}
}

"license" -> {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/su/plo/voice/plugin/util/addon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import su.plo.voice.plugin.parser.JavaAddonParser
import su.plo.voice.plugin.parser.KotlinAddonParser
import java.io.File

fun File.parseAddonMeta() = parseAddon(this)
fun File.parseAddonMeta(sourceFiles: Collection<File>) = parseAddon(sourceFiles, this)

private fun parseAddon(file: File): AddonMeta? {
private fun parseAddon(sourceFiles: Collection<File>, file: File): AddonMeta? {
val fileName = file.name

if (fileName.endsWith(".java")) {
return JavaAddonParser.parseAddon(file)
return JavaAddonParser.parseAddon(sourceFiles, file)
} else if (fileName.endsWith(".kt")) {
return KotlinAddonParser.parseAddon(file)
return KotlinAddonParser.parseAddon(sourceFiles, file)
}

return null
Expand Down

0 comments on commit 4fc138b

Please sign in to comment.