Scala Nuggets: Finding All Classes in a Package

Table of contents
Reading Time: 2 minutes

Recently, while doing a project for one of our clients in the US, we had the opportunity to build a plugin based framework for the product. While, the details of the plugin framework are reserved as a topic of another post, in this one we would see how easy it is to find all the classes in a package using a utility provided by Clapper. The name of the utility is ClassUtil and you can get more details from their site.

Our scenario was that we should be able to discover any new plugins which are added for the product at the time when the product is initialized. For this the above utility came in pretty handy.

The simple code as you would expect in Scala is

[sourcecode language=”scala”]
def init() {
val classpath = List(".").map(new File(_))
val finder = ClassFinder(classpath)
val classes = finder.getClasses
val classMap = ClassFinder.classInfoMap(classes)
val plugins = ClassFinder.concreteSubclasses("com.inphina.plugin.ProdPlugin", classMap)

plugins.foreach {
pluginString =>
val plugin = Class.forName(pluginString.name).newInstance().asInstanceOf[ProdPlugin]
pluginMap += (plugin.name -> pluginString.name)
}
}
[/sourcecode]

As you would notice, we start from the current location and scan for all the files in that location which extend our trait called com.inphina.plugin.ProdPlugin
This, returns back a list of all classes which are extending the trait. Next, we instantiate the plugin and put it in a map for our product to consume it. There are several other ways in which you can filter your classes and you would get more details here.

If you are using SBT 0.10x then you can add the dependency in build.sbt with the following

[sourcecode language=”text”]
name := "InphinaProductManager"
version := "1.0"
organization := "com.inphina.prod"
scalaVersion := "2.9.0-1"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq(
"se.scalablesolutions.akka" % "akka-actor" % "1.1.3",
"se.scalablesolutions.akka" % "akka-typed-actor" % "1.1.3",
"se.scalablesolutions.akka" % "akka-amqp" % "1.1.3",
"se.scalablesolutions.akka" % "akka-testkit" % "1.1.3"
)

libraryDependencies += "org.scalatest" % "scalatest_2.9.0" % "1.6.1"

libraryDependencies += "org.clapper" % "classutil_2.9.0" % "0.3.6"
[/sourcecode]

A word of caution, finding classes in such a way is an involved process and might not be performant for your scenario. For our case, we have to do it only once at the start of the product and the number of plugins would not exceed a score.

Written by 

Vikas is the CEO and Co-Founder of Knoldus Inc. Knoldus does niche Reactive and Big Data product development on Scala, Spark, and Functional Java. Knoldus has a strong focus on software craftsmanship which ensures high-quality software development. It partners with the best in the industry like Lightbend (Scala Ecosystem), Databricks (Spark Ecosystem), Confluent (Kafka) and Datastax (Cassandra). Vikas has been working in the cutting edge tech industry for 20+ years. He was an ardent fan of Java with multiple high load enterprise systems to boast of till he met Scala. His current passions include utilizing the power of Scala, Akka and Play to make Reactive and Big Data systems for niche startups and enterprises who would like to change the way software is developed. To know more, send a mail to hello@knoldus.com or visit www.knoldus.com

3 thoughts on “Scala Nuggets: Finding All Classes in a Package2 min read

  1. Hey Vikas,

    Neat! How I wish I was using Scala 🙂

    Looking forward to your post on the design of the plugin framework. I have been thinking about introducing a plugin based design in my current project. Need some fodder for those grey cells.

    Is it implemented in Scala? Did you find any usecases where it would have been an order of magnitude more difficult to do it in Java?

    Cheers,
    Sonny

    1. Hey Sonny, yeah would be posting the design soon. Yes, its is implemented in Scala but it would be quite simple in Java as well. We are using Scala in the current project for web scalability but we get concise and better looking code as a bonus 🙂

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading