Created
August 22, 2017 09:10
-
-
Save monzou/e7f74e32beffd4cfbf66fa7a8f94c973 to your computer and use it in GitHub Desktop.
ClassCollector.kt
This file contains hidden or 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
object ClassCollector { | |
private val KINDS = setOf(JavaFileObject.Kind.CLASS) | |
private val fileManager: JavaFileManager | |
init { | |
val compiler = ToolProvider.getSystemJavaCompiler() | |
fileManager = compiler.getStandardFileManager(DiagnosticCollector<JavaFileObject>(), null, null) | |
} | |
fun collect(vararg pkgs: String): Collection<Class<*>> { | |
return pkgs.map { collect(it) }.flatten() | |
} | |
fun collect(pkg: String): Collection<Class<*>> { | |
return fileManager.list(StandardLocation.CLASS_PATH, pkg, KINDS, true).map { | |
val path = it.toUri().path.replace(".class", "").replace("/", ".") | |
val p = path.lastIndexOf(pkg) | |
Class.forName(if (p > 0) path.substring(p, path.length) else path) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment