generated from JetBrains/intellij-platform-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add reflection support for ToolchainVariable
- Added reflection support to dynamically create instances of ToolchainVariable subclasses. - Introduced a new method `all()` to retrieve all instances of ToolchainVariable subclasses.
- Loading branch information
Showing
3 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 34 additions & 2 deletions
36
core/src/main/kotlin/com/phodal/shirecore/provider/variable/model/ToolchainVariable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
package com.phodal.shirecore.provider.variable.model | ||
|
||
import org.reflections.Reflections | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.companionObjectInstance | ||
import kotlin.reflect.full.declaredFunctions | ||
import kotlin.reflect.full.functions | ||
|
||
interface ToolchainVariable { | ||
val variableName: String; | ||
var value: Any?; | ||
val description: String; | ||
val description: String | ||
|
||
companion object { | ||
private val subclasses: Set<KClass<out ToolchainVariable>> by lazy { | ||
val reflections = Reflections("com.phodal.shirecore.provider.variable.model") | ||
reflections.getSubTypesOf(ToolchainVariable::class.java) | ||
.map { it.kotlin } | ||
.toSet() | ||
} | ||
|
||
fun from(variableName: String): ToolchainVariable? { | ||
return VcsToolchainVariable.from(variableName) ?: TerminalVariable.from(variableName) | ||
for (subclass in subclasses) { | ||
val companion = subclass.companionObjectInstance ?: continue | ||
val fromFunction = companion::class.declaredFunctions.find { it.name == "from" } ?: continue | ||
val result = fromFunction.call(companion, variableName) as? ToolchainVariable | ||
if (result != null) { | ||
return result | ||
} | ||
} | ||
return null | ||
} | ||
|
||
fun all(): List<ToolchainVariable> { | ||
val allVariables = mutableListOf<ToolchainVariable>() | ||
for (subclass in subclasses) { | ||
val valuesFunction = subclass.functions.find { it.name == "values" } ?: continue | ||
val enumConstants = valuesFunction.call() as Array<ToolchainVariable> | ||
allVariables.addAll(enumConstants) | ||
} | ||
|
||
return allVariables | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters