From caa540d3af326ae05d86ab8982f08db9ee5c79ea Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Sun, 16 Aug 2020 16:47:36 +0200 Subject: [PATCH] replace thicc reflections lib with lightweight one! jar is 2 mb smaller! --- build.gradle.kts | 2 +- .../modpacktools/command/CommandLoader.kt | 36 +++++++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 2ec2144..6bc8098 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -63,7 +63,7 @@ dependencies { //Other "org.slf4j:slf4j-simple:2.0.0-alpha1", - "org.reflections:reflections:0.9.12" + "eu.infomas:annotation-detector:3.0.5" ).forEach {implementation(it)} testImplementation("junit:junit:4.12") diff --git a/src/main/kotlin/ley/anvil/modpacktools/command/CommandLoader.kt b/src/main/kotlin/ley/anvil/modpacktools/command/CommandLoader.kt index d5c9f68..baf1dc6 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/command/CommandLoader.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/command/CommandLoader.kt @@ -1,13 +1,12 @@ package ley.anvil.modpacktools.command +import eu.infomas.annotation.AnnotationDetector import ley.anvil.modpacktools.CONFIG import ley.anvil.modpacktools.MPJH import net.sourceforge.argparse4j.inf.ArgumentParserException -import org.reflections.Reflections -import org.reflections.scanners.SubTypesScanner import kotlin.reflect.KClass 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 @@ -50,17 +49,18 @@ class CommandLoader(private val pkg: String) { @Suppress("UNCHECKED_CAST") private fun loadCommands() { - //Get ICommands in package - val refs = Reflections(pkg, SubTypesScanner(false)) + val reporter = object : AnnotationDetector.TypeReporter { + override fun annotations() = arrayOf(LoadCommand::class.java) - refs.getSubTypesOf(ICommand::class.java).stream() - .map {it.kotlin} - //Only annotated classes - .filter {it.hasAnnotation()} - //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)} + override fun reportTypeAnnotation(annotation: Class?, className: String?) { + val clazz = Class.forName(className).kotlin + if(clazz.isSubclassOf(ICommand::class)) { + clazz.objectInstance?.let {addCommand(it as ICommand)} ?: addClass(clazz as KClass) + } + } + } + + 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 ArgumentParserException if the arguments are not valid */ - @Throws(NoSuchElementException::class, ConfigMissingException::class, ModpackJsonMissingException::class, ArgumentParserException::class) - fun runCommand(name: String, args: Array) = commands.computeIfAbsent(name) {throw NoSuchElementException("Command $name Not Found")}.runStatic(args) + @Throws( + NoSuchElementException::class, + ConfigMissingException::class, + ModpackJsonMissingException::class, + ArgumentParserException::class + ) + fun runCommand(name: String, args: Array) = + commands.computeIfAbsent(name) {throw NoSuchElementException("Command $name Not Found")}.runStatic(args) }