Skip to content

Commit

Permalink
Fixed deprecations and improved resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
lefou committed Nov 3, 2023
1 parent 25672ca commit 5cc728d
Show file tree
Hide file tree
Showing 31 changed files with 59 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ class ScalaModelReader @Inject() (executeManager: ExecuteManager) extends ModelR
}

private def registerExecutors(m: Model, options: util.Map[String, _], tasks: immutable.Seq[Task]): Unit = {
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
executeManager.register(m, tasks.map(new ScalaTask(_).asInstanceOf[ExecuteTask]).asJava)
executeManager.install(m, options)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ import java.util.concurrent.atomic.AtomicInteger
import java.util.jar.JarFile
import scala.collection.mutable
import scala.io.Source
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, Position}
import scala.reflect.internal.util.{AbstractFileClassLoader, BatchSourceFile, CodeAction, Position}
import scala.reflect.io.{AbstractFile, VirtualDirectory}
import scala.tools.nsc.reporters.{FilteringReporter, Reporter}
import scala.tools.nsc.{Global, Settings}
import scala.util.Using
import scala.util.matching.Regex

object Eval {
Expand All @@ -41,6 +42,11 @@ object Eval {
class CompilerException(val messages: List[List[String]]) extends Exception(
"Compiler exception " + messages.map(_.mkString("\n")).mkString("\n"))

trait MessageCollector {
val messages: mutable.Seq[List[String]]
val counts: Map[_root_.scala.reflect.internal.Reporter.Severity, AtomicInteger]
}

}

/**
Expand Down Expand Up @@ -141,12 +147,12 @@ class Eval(target: Option[File]) {
def apply[T](files: File*): T = {
if (target.isDefined) {
val targetDir = target.get
val unprocessedSource = files.map { scala.io.Source.fromFile(_).mkString }.mkString("\n")
val unprocessedSource = files.map { f => Using.resource(Source.fromFile(f))(_.mkString) }.mkString("\n")
val processed = sourceForString(unprocessedSource)
val sourceChecksum = uniqueId(processed, None)
val checksumFile = new File(targetDir, "checksum")
val lastChecksum = if (checksumFile.exists) {
Source.fromFile(checksumFile).getLines().take(1).toList.head
Using.resource(Source.fromFile(checksumFile))(_.getLines().take(1).toList.head)
} else {
-1
}
Expand All @@ -166,7 +172,7 @@ class Eval(target: Option[File]) {
cleanBaseName, sourceChecksum)
applyProcessed(className, processed, resetState = false)
} else {
apply(files.map { scala.io.Source.fromFile(_).mkString }.mkString("\n"), true)
apply(files.map {f => Using.resource(Source.fromFile(f))(_.mkString) }.mkString("\n"), true)
}
}

Expand Down Expand Up @@ -202,7 +208,7 @@ class Eval(target: Option[File]) {
* delegates to toSource(code: String)
*/
def toSource(file: File): String = {
toSource(scala.io.Source.fromFile(file).mkString)
toSource(Using.resource(Source.fromFile(file))(_.mkString))
}

/**
Expand All @@ -215,7 +221,7 @@ class Eval(target: Option[File]) {
/**
* Compile an entire source file into the virtual classloader.
*/
def compile(code: String) {
def compile(code: String): Unit = {
compiler(sourceForString(code))
}

Expand All @@ -224,14 +230,14 @@ class Eval(target: Option[File]) {
* loaded classes with `compile`, they can be referenced/imported in code run by `inPlace`.
*/
def inPlace[T](code: String): T = {
apply[T](code, false)
apply[T](code, resetState = false)
}

/**
* Check if code is Eval-able.
* @throws CompilerException if not Eval-able.
*/
def check(code: String) {
def check(code: String): Unit = {
val id = uniqueId(sourceForString(code))
val className = "Evaluator__" + id
val wrappedCode = wrapCodeInClass(className, code)
Expand All @@ -244,7 +250,7 @@ class Eval(target: Option[File]) {
* @throws CompilerException if not Eval-able.
*/
def check(files: File*): Unit = {
val code = files.map { scala.io.Source.fromFile(_).mkString }.mkString("\n")
val code = files.map { f => Using.resource(Source.fromFile(f))(_.mkString) }.mkString("\n")
check(code)
}

Expand Down Expand Up @@ -414,7 +420,7 @@ class Eval(target: Option[File]) {
apply(code, maximumRecursionDepth)

def apply(code: String, maxDepth: Int): String = {
val lines = code.lines map { line: String =>
val lines = code.linesIterator map { line: String =>
val tokens = line.trim.split(' ')
if (tokens.length == 2 && tokens(0).equals("#include")) {
val path = tokens(1)
Expand Down Expand Up @@ -463,11 +469,6 @@ class Eval(target: Option[File]) {
val cache = new mutable.HashMap[String, Class[_]]()
val target = compilerOutputDir

trait MessageCollector {
val messages: mutable.Seq[List[String]]
val counts: Map[_root_.scala.reflect.internal.Reporter.Severity, AtomicInteger]
}

val reporter = messageHandler getOrElse new FilteringReporter with MessageCollector {
val settings = StringCompiler.this.settings
val messages = new mutable.ListBuffer[List[String]]
Expand All @@ -480,7 +481,7 @@ class Eval(target: Option[File]) {

override def hasErrors: Boolean = super.hasErrors || (counts(ERROR).get() > 0)

override def doReport(pos: Position, msg: String, severity: Severity): Unit = {
override def doReport(pos: Position, msg: String, severity: Severity, actions: List[CodeAction]): Unit = {
counts(severity).intValue()
val severityName = severity match {
case ERROR => "error: "
Expand All @@ -504,8 +505,8 @@ class Eval(target: Option[File]) {
})
}

override def reset: Unit = {
super.reset
override def reset(): Unit = {
super.reset()
messages.clear()
counts.foreach(p => p._2.set(0))
}
Expand Down Expand Up @@ -544,14 +545,14 @@ class Eval(target: Option[File]) {
}

object Debug {
val enabled =
val enabled: Boolean =
System.getProperty("eval.debug") != null

def printWithLineNumbers(code: String) {
def printWithLineNumbers(code: String): Unit = {
printf("Code follows (%d bytes)\n", code.length)

var numLines = 0
code.lines foreach { line: String =>
code.linesIterator foreach { line: String =>
numLines += 1
println(numLines.toString.padTo(5, ' ') + "| " + line)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PrettiedBuild(b: Build) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Build => MavenBuild}

class ConvertibleMavenBuild(mb: MavenBuild) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class PrettiedBuildBase(b: BuildBase) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{BuildBase => MavenBuildBase}

class ConvertibleMavenBuildBase(mb: MavenBuildBase) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PrettiedCiManagement(c: CiManagement) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{CiManagement => MavenCiManagement}

class ConvertibleMavenCiManagement(mcm: MavenCiManagement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object Config extends Dynamic {
def sanitizeElementName(k: String): String = {
val r = elementStartCharMapping.foldLeft(k)((k, m) => m._1.replaceAllIn(k, found => m._2))
if (r.length() > 1) {
r(0) + elementCharMapping.foldLeft(r.substring(1))((k, m) => m._1.replaceAllIn(k, found => m._2))
r.substring(0, 1) + elementCharMapping.foldLeft(r.substring(1))((k, m) => m._1.replaceAllIn(k, found => m._2))
} else r
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PrettiedContributor(c: Contributor) {
}


import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Contributor => MavenContributor}

class ConvertibleMavenContributor(mc: MavenContributor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class PrettiedDependency(d: Dependency) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Dependency => MavenDependency, Exclusion => MavenExclusion}

class ConvertibleMavenDependency(md: MavenDependency) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class PrettiedDependencyManagement(dm: DependencyManagement) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Dependency => MavenDependency, DependencyManagement => MavenDependencyManagement}

class ConvertibleMavenDependencyManagement(mdm: MavenDependencyManagement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object Developer {
roles: immutable.Seq[String] = Nil,
timezone: String = null,
url: String = null
) =
): Developer =
new Developer(
Option(id),
Option(email),
Expand All @@ -66,7 +66,7 @@ class PrettiedDeveloper(d: Developer) {
}


import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Developer => MavenDeveloper}

class ConvertibleMavenDeveloper(mc: MavenDeveloper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object DistributionManagement {
downloadUrl: String = null,
relocation: Relocation = null,
status: String = null
) =
): DistributionManagement =
new DistributionManagement(
Option(repository),
Option(snapshotRepository),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object Execution {
goals: immutable.Seq[String] = immutable.Seq.empty,
inherited: Boolean = true,
configuration: Config = null
) = {
): Execution = {
new Execution(
id,
Option(phase),
Expand All @@ -51,7 +51,7 @@ class PrettiedExecution(e: Execution) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{PluginExecution => MavenExecution}

class ConvertibleMavenExecution(me: MavenExecution) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object IssueManagement {
def apply(
system: String = null,
url: String = null
) =
): IssueManagement =
new IssueManagement(
Option(system),
Option(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object License {
url: String = null,
distribution: String = null,
comments: String = null
) =
): License =
new License(
Option(name),
Option(url),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object MailingList {
post: String = null,
archive: String = null,
otherArchives: immutable.Seq[String] = Nil
) =
): MailingList =
new MailingList(
Option(name),
Option(subscribe),
Expand Down Expand Up @@ -54,7 +54,7 @@ class PrettiedMailingList(ml: MailingList) {


import org.apache.maven.model.{MailingList => MavenMailingList}
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

class ConvertibleMavenMailingList(mml: MavenMailingList) {
def asScala: MailingList = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class PrettiedModel(m: Model) {
}

import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{ Model => MavenModel }

class ConvertibleMavenModel(mm: MavenModel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class PrettiedNotifier(n: Notifier) {
}


import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Notifier => MavenNotifier}

class ConvertibleMavenNotifier(mn: MavenNotifier) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Organization {
def apply(
name: String = null,
url: String = null
) =
): Organization =
new Organization(
Option(name),
Option(url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Parent {
def apply(
gav: Gav = null,
relativePath: String = "../pom.xml"
) =
): Parent =
new Parent(
Option(gav),
relativePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Plugin {
dependencies: immutable.Seq[Dependency] = immutable.Seq.empty,
inherited: Boolean = true,
configuration: Config = null
) =
): Plugin =
new Plugin(
gav,
extensions,
Expand All @@ -53,7 +53,7 @@ class PrettiedPlugin(p: Plugin) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._

import org.apache.maven.model.{Plugin => MavenPlugin}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PrettiedPluginManagement(p: PluginManagement) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{PluginManagement => MavenPluginManagement}

class ConvertibleMavenPluginManagement(mpm: MavenPluginManagement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object Profile {
modules: immutable.Seq[String] = Nil,
pluginRepositories: immutable.Seq[Repository] = Nil,
repositories: immutable.Seq[Repository] = Nil
) =
): Profile =
new Profile(
id,
Option(activation),
Expand Down Expand Up @@ -71,7 +71,7 @@ class PrettiedProfile(p: Profile) {


import org.sonatype.maven.polyglot.scala.MavenConverters._
import scala.collection.JavaConverters._
import scala.jdk.CollectionConverters._
import org.apache.maven.model.{Profile => MavenProfile}

class ConvertibleMavenProfile(mp: MavenProfile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object Relocation {
artifactId: String = null,
version: String = null,
message: String = null
) =
): Relocation =
new Relocation(
Option(groupId),
Option(artifactId),
Expand Down
Loading

0 comments on commit 5cc728d

Please sign in to comment.