1

I want to build some statistics on which Java standard library classes (those in java.*/javax.*) and methods are used in some Scala source code (and how often).

What would be the most straightforward way to do that? Is there a chance to also cover transitive dependencies, e. g. when some Lift class depends on a Scala standard library method which in turn depends on a Java standard libraries method?

0

5 Answers 5

2

JDepend looks like it could do this: http://clarkware.com/software/JDepend.html

1

You might want to take a look at the answer to this question which could probably be adapted to solve your problem?

1

You can do this with Structure101. Load up a S101 project from the Scala/Java bytecode, go to the Collaboration perspective (Structure tab) and select the root packages of the Scala code in the left hand side treeview, and the Java packages on the rhs. This will show you which Java items (to package.class.method/field levels of detail) are used by your Scala project, and which Scala items use anything in the Java libraries, and you can drill down to see the specific dependencies.

You can also get at transitive dependencies using tagging. Select the item, right click, tag/uses/indirectly. This will mark the items with a blue dot that rolls up through each perspective. You can hide all but the transitive closure (invert tags, then hide tagged). A useful view would probably be got by going to the Slice perspective, choosing the "outer class" slice, main cluster, and use the group by parent option. This will show all the classes on a single diagram, grouped by their parent packages. If you have a very big project, you could switch to the DSM view, which is more scalable than the directed graph.

0

IntelliJ IDEA has a feature called Analyze Dependencies that will do what you want.

You can export the dependency analysis to a text file and it will contain entries like:

<file path="$PROJECT_DIR$/src/main/scala/com/foo/config/Bootstrap.scala">
  <dependency path="$PROJECT_DIR$/src/main/scala/com/foo/ws/auth/Auth.scala" />
  <dependency path="$PROJECT_DIR$/src/main/scala/com/foo/domain/auth/User.scala" />
  <dependency path="$MAVEN_REPOSITORY$/org/scala-lang/scala-library/2.9.1/scala-library-2.9.1.jar!/scala/package.class" />
  <dependency path="/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/../Classes/classes.jar!/java/io/InputStream.class" />
  ....

Note how the last line listed is a dependency on the Java InputStream class.

It does transitive dependencies as well.

0

I wrote a Scala compiler plugin, which solved this issue for me. It is not pretty but it does its job.

The main part doing the work looks like this:

for (tree <- unit.body) {
  tree match {
    case tree@DefDef(mods, name, tparams, vparamss, tpt, rhs)
      if isJavaAndNotAccessor(mods, rhs.symbol) =>
      printJava(rhs, "DefDef")

    case tree@ValDef(mods, name, tpt, rhs) =>
      if (isJavaAndNotAccessor(mods, tpt.symbol))
        printJava(tpt, "ValDef")
      tpt.tpe.typeArgs.foreach(t => findAllJavaTypes(t.baseTypeSeq.toList).foreach(sym => printJavaCustom(sym, posWithFallback(sym.pos, tree.pos), "TpeArg")))

    case tree@Template(parents, self, body) =>
      parents.foreach(p => findAllJavaTypes(p.tpe.baseTypeSeq.toList).foreach(sym => printJavaCustom(sym, posWithFallback(p.pos, tree.pos), "TpeDef")))

    case tree@Select(qual, name) =>
      if (isJavaAndNotJLObjectInit(tree.symbol) && (tree.symbol.isMethod || tree.symbol.isValue || tree.symbol.isStaticMember))
        printJava(tree, "Select")

    case _ =>
  }
}

private def findAllJavaTypes(types: List[Type]): List[Symbol] = {
  def find0(types: List[Type], result: List[Type]): List[Type] =
    if (types == List())
      result
    else
      find0(types.flatMap(_.typeArgs), types.filter(tpe => isJavaAndNotJLObject(tpe.typeSymbol)) ::: result)

  find0(types, List()).distinct.map(_.typeSymbol)
}

Code is hosted here: https://github.com/soc/scala-compilerplugin-javausagecounter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.