Skip to content

Instantly share code, notes, and snippets.

@bryanjswift
Created January 19, 2012 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanjswift/1637690 to your computer and use it in GitHub Desktop.
Save bryanjswift/1637690 to your computer and use it in GitHub Desktop.
Work around CaseClassSigParser exceptions in sbt:console

When doing something in an SBT console which requires the loading of Case Class instances via fig or jerkson if the code is executed within inClassLoader it will produce a result rather than a ClassNotFoundException.

For example in Persnicketly:

val articles = inClassLoader(classOf[com.persnicketly.persistence.Connection$]) {
  ScoredArticleDao.select(from = 60, count = 100)
}

Will successfully load MongoDB configuration from config.json, access the db and retrieve articles.

def inClassLoader[T](cls: Class[_])(f: => T): T = {
val prev = Thread.currentThread.getContextClassLoader
try {
Thread.currentThread.setContextClassLoader(
cls.getClassLoader
)
f
} finally {
Thread.currentThread.setContextClassLoader(prev)
}
}
val articles = inClassLoader(classOf[com.persnicketly.persistence.Connection$]) {
ScoredArticleDao.select(from = 60, count = 100)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment