How do I open a library that defines a class from sbt?

For example, I would like to know which library from the specified class declares the class, for example ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP?

Any decent IDE offers a function where you can search for types like this (find a type in Eclipse), but how can this be done in SBT? Is there any task / command / plugin that can help me?

A feature like this is also useful for class collision detection (if multiple tanks will define the same class): see this related question How to find duplicate classes between dependencies with SBT .

+3


source to share


1 answer


The method I thought I knew was actually not working well.

Analyzing (not working)

core> consoleProject
[info] Starting scala interpreter...

scala> val a = (compile in Compile).eval
a: sbt.inc.Analysis = Analysis: 69 Scala sources, 1092 classes, 2 external source dependencies, 4 binary dependencies

scala> val stamps = a.stamps
stamps: sbt.inc.Stamps = Stamps for: 1092 products, 69 sources, 4 binaries, 4 classNames

scala> val classNames = stamps.classNames
classNames: Map[java.io.File,String] = Map(/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar -> java.lang.Object, /Users/eugene/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.6.jar -> scala.Function1, /Users/eugene/.ivy2/cache/org.typelevel/machinist_2.11/jars/machinist_2.11-0.3.0.jar -> machinist.Ops, /Users/eugene/.ivy2/local/org.spire-math/algebra_2.11/0.2.0-SNAPSHOT/jars/algebra_2.11.jar -> algebra.Eq)

      

At first glance it looks useful, but the direction of the map is from File

to String

, so it is not really very useful.



Java reflection

If you can get Class

, you can do something like this:

scala> a.getClass.getClassLoader match { case ucl: java.net.URLClassLoader => ucl.getResource(a.getClass.getName.replace('.', '/') + ".class")  }
res13: java.net.URL = jar:file:/Users/eugene/.conscript/boot/scala-2.10.5/org.scala-sbt/sbt/0.13.9-RC1/incremental-compiler-0.13.9-RC1.jar!/sbt/inc/MAnalysis.class

      

+3


source







All Articles