Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ClassCollector.kt
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
You can’t perform that action at this time.