Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract andClose #553

Merged
merged 1 commit into from
Sep 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions core/src/main/scala/com/typesafe/tools/mima/core/ClassPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.typesafe.tools.mima.core

import java.nio.file._

import scala.collection.JavaConverters._

private object AbsFile {
def apply(p: Path): AbsFile = {
val name = p.getFileName.toString.stripPrefix("/")
Expand All @@ -11,7 +13,9 @@ private object AbsFile {
}

private[core] final case class AbsFile(name: String)(path: String, bytes: () => Array[Byte]) {
// Not just Path b/c Path#equals uses FS (AbstractFile didn't) breaking `distinct`.
// Not defined as a simple wrapper of java.nio.file.Path, because Path#equals uses its FileSystem,
// differently to scala-reflect's AbstractFile, which breaks things like `distinct`.

def toByteArray = bytes()
override def toString = path
}
Expand Down Expand Up @@ -46,13 +50,7 @@ private[mima] object ClassPath {
private def expandCp(cp: String) = split(cp).flatMap(s => fromJarOrDir(new java.io.File(s)))
private def javaBootCp = expandCp(System.getProperty("sun.boot.class.path", ""))

import scala.collection.JavaConverters._
private def list(p: Path) = {
val stream = Files.newDirectoryStream(p)
val list = stream.asScala.toStream.sortBy(_.toString)
stream.close()
list
}
private def list(p: Path) = andClose(Files.newDirectoryStream(p))(_.asScala.toStream.sortBy(_.toString))
private def listDir(p: Path) = if (Files.isDirectory(p)) list(p) else Stream.empty
private def readLink(p: Path) = if (Files.isSymbolicLink(p)) Files.readSymbolicLink(p) else p
private def rootPath(p: Path) = FileSystems.newFileSystem(p, null).getPath("/")
Expand Down Expand Up @@ -95,4 +93,6 @@ private[mima] object ClassPath {
def classes(pkg: String) = aggregates.flatMap(_.classes(pkg)).distinct
def asClassPathString = join(aggregates.map(_.asClassPathString).distinct)
}

private def andClose[A <: AutoCloseable, B](xs: A)(f: A => B): B = try f(xs) finally xs.close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer 👍 😄

}