Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. object ClassCollector {
  2.  
  3. private val KINDS = setOf(JavaFileObject.Kind.CLASS)
  4.  
  5. private val fileManager: JavaFileManager
  6.  
  7. init {
  8. val compiler = ToolProvider.getSystemJavaCompiler()
  9. fileManager = compiler.getStandardFileManager(DiagnosticCollector<JavaFileObject>(), null, null)
  10. }
  11.  
  12. fun collect(vararg pkgs: String): Collection<Class<*>> {
  13. return pkgs.map { collect(it) }.flatten()
  14. }
  15.  
  16. fun collect(pkg: String): Collection<Class<*>> {
  17. return fileManager.list(StandardLocation.CLASS_PATH, pkg, KINDS, true).map {
  18. val path = it.toUri().path.replace(".class", "").replace("/", ".")
  19. val p = path.lastIndexOf(pkg)
  20. Class.forName(if (p > 0) path.substring(p, path.length) else path)
  21. }
  22. }
  23.  
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement