4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-05-29 00:35:46 +02:00

replace thicc reflections lib with lightweight one!

jar is 2 mb smaller!
This commit is contained in:
LordMZTE 2020-08-16 16:47:36 +02:00
parent a5d5562d93
commit caa540d3af
2 changed files with 22 additions and 16 deletions

View file

@ -63,7 +63,7 @@ dependencies {
//Other //Other
"org.slf4j:slf4j-simple:2.0.0-alpha1", "org.slf4j:slf4j-simple:2.0.0-alpha1",
"org.reflections:reflections:0.9.12" "eu.infomas:annotation-detector:3.0.5"
).forEach {implementation(it)} ).forEach {implementation(it)}
testImplementation("junit:junit:4.12") testImplementation("junit:junit:4.12")

View file

@ -1,13 +1,12 @@
package ley.anvil.modpacktools.command package ley.anvil.modpacktools.command
import eu.infomas.annotation.AnnotationDetector
import ley.anvil.modpacktools.CONFIG import ley.anvil.modpacktools.CONFIG
import ley.anvil.modpacktools.MPJH import ley.anvil.modpacktools.MPJH
import net.sourceforge.argparse4j.inf.ArgumentParserException import net.sourceforge.argparse4j.inf.ArgumentParserException
import org.reflections.Reflections
import org.reflections.scanners.SubTypesScanner
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.full.createInstance import kotlin.reflect.full.createInstance
import kotlin.reflect.full.hasAnnotation import kotlin.reflect.full.isSubclassOf
/** /**
* The command loader will scan the given package for [ICommand] classes and add them to the command list * The command loader will scan the given package for [ICommand] classes and add them to the command list
@ -50,17 +49,18 @@ class CommandLoader(private val pkg: String) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
private fun loadCommands() { private fun loadCommands() {
//Get ICommands in package val reporter = object : AnnotationDetector.TypeReporter {
val refs = Reflections(pkg, SubTypesScanner(false)) override fun annotations() = arrayOf(LoadCommand::class.java)
refs.getSubTypesOf(ICommand::class.java).stream() override fun reportTypeAnnotation(annotation: Class<out Annotation>?, className: String?) {
.map {it.kotlin} val clazz = Class.forName(className).kotlin
//Only annotated classes if(clazz.isSubclassOf(ICommand::class)) {
.filter {it.hasAnnotation<LoadCommand>()} clazz.objectInstance?.let {addCommand(it as ICommand)} ?: addClass(clazz as KClass<out ICommand>)
//can be object, if so use that instead of new instance }
.map {it.objectInstance ?: it} }
//create new instance if it is a class, otherwise just add the current instance }
.forEach {if(it is ICommand) addCommand(it) else addClass(it as KClass<out ICommand>)}
AnnotationDetector(reporter).detect(pkg)
} }
/** /**
@ -94,6 +94,12 @@ class CommandLoader(private val pkg: String) {
* @throws NoSuchElementException if there's no command with the given name * @throws NoSuchElementException if there's no command with the given name
* @throws ArgumentParserException if the arguments are not valid * @throws ArgumentParserException if the arguments are not valid
*/ */
@Throws(NoSuchElementException::class, ConfigMissingException::class, ModpackJsonMissingException::class, ArgumentParserException::class) @Throws(
fun runCommand(name: String, args: Array<out String>) = commands.computeIfAbsent(name) {throw NoSuchElementException("Command $name Not Found")}.runStatic(args) NoSuchElementException::class,
ConfigMissingException::class,
ModpackJsonMissingException::class,
ArgumentParserException::class
)
fun runCommand(name: String, args: Array<out String>) =
commands.computeIfAbsent(name) {throw NoSuchElementException("Command $name Not Found")}.runStatic(args)
} }