diff --git a/README.md b/README.md index 4ce58459a728..dc869da0da79 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ Here, `` is the milestone targeted by the PR (e.g., 2.11.6), and ` val typeSuffix = a.`type` match { case "pom" => "-pom.xml" - case "bundle" | "jar" => ".jar" + case "jar" => ".jar" case "doc" => "-docs.jar" case tpe => s"-$tpe.${a.extension}" } @@ -99,6 +99,8 @@ lazy val publishSettings : Seq[Setting[_]] = Seq( if (file.exists) List(Credentials(file)) else Nil }, + // Add a "default" Ivy configuration because sbt expects the Scala distribution to have one: + ivyConfigurations += Configuration("default", "Default", true, List(Configurations.Runtime), true), publishMavenStyle := true ) @@ -236,8 +238,8 @@ def fixPom(extra: (String, scala.xml.Node)*): Setting[_] = { ) ++ extra) } } -/** Remove unwanted dependencies from the POM. */ -def removePomDependencies(deps: (String, String)*): Setting[_] = { +/** Remove unwanted dependencies from the POM and ivy.xml. */ +def removePomDependencies(deps: (String, String)*): Seq[Setting[_]] = Seq( pomPostProcess := { n => val n2 = pomPostProcess.value.apply(n) import scala.xml._ @@ -252,14 +254,40 @@ def removePomDependencies(deps: (String, String)*): Setting[_] = { case n => Seq(n) } })).transform(Seq(n2)).head + }, + deliverLocal := { + import scala.xml._ + import scala.xml.transform._ + val f = deliverLocal.value + val e = (new RuleTransformer(new RewriteRule { + override def transform(node: Node) = node match { + case e: Elem if e.label == "dependency" && { + val org = e.attribute("org").getOrElse("").toString + val name = e.attribute("name").getOrElse("").toString + deps.exists { case (g, a) => + org == g && (name == a || name == (a + "_" + scalaBinaryVersion.value)) + } + } => Seq.empty + case n => Seq(n) + } + })).transform(Seq(XML.loadFile(f))).head + XML.save(f.getAbsolutePath, e, xmlDecl = true) + f } -} +) val disableDocs = Seq[Setting[_]]( sources in (Compile, doc) := Seq.empty, publishArtifact in (Compile, packageDoc) := false ) +val disablePublishing = Seq[Setting[_]]( + publishArtifact := false, + // The above is enough for Maven repos but it doesn't prevent publishing of ivy.xml files + publish := {}, + publishLocal := {} +) + lazy val setJarLocation: Setting[_] = artifactPath in packageBin in Compile := { // two lines below are copied over from sbt's sources: @@ -397,43 +425,44 @@ lazy val compiler = configureAsSubproject(project) "/project/description" -> Compiler for the Scala Programming Language, "/project/packaging" -> jar ), - apiURL := None, - removePomDependencies( - ("org.apache.ant", "ant"), - ("org.scala-lang.modules", "scala-asm") - ) + apiURL := None ) + .settings(removePomDependencies( + ("org.apache.ant", "ant"), + ("org.scala-lang.modules", "scala-asm") + ): _*) .dependsOn(library, reflect) lazy val interactive = configureAsSubproject(project) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( name := "scala-compiler-interactive", - description := "Scala Interactive Compiler", - publishArtifact := false + description := "Scala Interactive Compiler" ) .dependsOn(compiler) lazy val repl = configureAsSubproject(project) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( connectInput in run := true, - publishArtifact := false, run <<= (run in Compile).partialInput(" -usejavacp") // Automatically add this so that `repl/run` works without additional arguments. ) .dependsOn(compiler, interactive) lazy val replJline = configureAsSubproject(Project("repl-jline", file(".") / "src" / "repl-jline")) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( libraryDependencies += jlineDep, - name := "scala-repl-jline", - publishArtifact := false + name := "scala-repl-jline" ) .dependsOn(repl) lazy val replJlineEmbedded = Project("repl-jline-embedded", file(".") / "target" / "repl-jline-embedded-src-dummy") .settings(scalaSubprojectSettings: _*) + .settings(disablePublishing: _*) .settings( name := "scala-repl-jline-embedded", // There is nothing to compile for this project. Instead we use the compile task to create @@ -464,18 +493,17 @@ lazy val replJlineEmbedded = Project("repl-jline-embedded", file(".") / "target" val outdir = (classDirectory in Compile).value JarJar(inputs, outdir, config) }), - publishArtifact := false, connectInput in run := true ) .dependsOn(replJline) lazy val scaladoc = configureAsSubproject(project) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( name := "scala-compiler-doc", description := "Scala Documentation Generator", libraryDependencies ++= Seq(scalaXmlDep, scalaParserCombinatorsDep, partestDep), - publishArtifact := false, includeFilter in unmanagedResources in Compile := "*.html" | "*.css" | "*.gif" | "*.png" | "*.js" | "*.txt" ) .dependsOn(compiler) @@ -497,10 +525,10 @@ lazy val partestExtras = configureAsSubproject(Project("partest-extras", file(". .dependsOn(replJlineEmbedded) .settings(clearSourceAndResourceDirectories: _*) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( name := "scala-partest-extras", description := "Scala Compiler Testing Tool (compiler-specific extras)", - publishArtifact := false, libraryDependencies += partestDep, unmanagedSourceDirectories in Compile := List(baseDirectory.value) ) @@ -510,8 +538,8 @@ lazy val junit = project.in(file("test") / "junit") .settings(clearSourceAndResourceDirectories: _*) .settings(commonSettings: _*) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( - publishArtifact := false, fork in Test := true, libraryDependencies ++= Seq(junitDep, junitIntefaceDep), testOptions += Tests.Argument(TestFrameworks.JUnit, "-a", "-v"), @@ -543,9 +571,9 @@ lazy val test = project .configs(IntegrationTest) .settings(commonSettings: _*) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings(Defaults.itSettings: _*) .settings( - publishArtifact := false, libraryDependencies ++= Seq(asmDep, partestDep, scalaXmlDep, scalacheckDep), unmanagedBase in IntegrationTest := baseDirectory.value / "files" / "lib", unmanagedJars in IntegrationTest <+= (unmanagedBase) (j => Attributed.blank(j)) map(identity), @@ -572,8 +600,8 @@ lazy val test = project lazy val manual = configureAsSubproject(project) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings( - publishArtifact := false, libraryDependencies ++= Seq(scalaXmlDep, antDep), classDirectory in Compile := (target in Compile).value / "classes" ) @@ -643,9 +671,9 @@ lazy val scalaDist = Project("scala-dist", file(".") / "target" / "scala-dist-di lazy val root = (project in file(".")) .settings(disableDocs: _*) + .settings(disablePublishing: _*) .settings(generateBuildCharacterFileSettings: _*) .settings( - publishArtifact := false, publish := {}, publishLocal := {}, commands ++= ScriptCommands.all, diff --git a/project/Osgi.scala b/project/Osgi.scala index 4676119076dc..36803c0e4494 100644 --- a/project/Osgi.scala +++ b/project/Osgi.scala @@ -3,7 +3,7 @@ import aQute.bnd.osgi.Constants._ import java.util.Properties import sbt._ import sbt.Keys._ -import scala.collection.JavaConversions._ +import collection.JavaConverters._ import VersionUtil.versionProperties /** OSGi packaging for the Scala build, distilled from sbt-osgi. We do not use sbt-osgi because it @@ -40,7 +40,6 @@ object Osgi { }, packagedArtifact in (Compile, packageBin) <<= (artifact in (Compile, packageBin), bundle).identityMap, // Also create OSGi source bundles: - artifact in (Compile, packageBin) ~= (_.copy(`type` = "bundle")), packageOptions in (Compile, packageSrc) += Package.ManifestAttributes( "Bundle-Name" -> (description.value + " Sources"), "Bundle-SymbolicName" -> (bundleSymbolicName.value + ".source"), @@ -57,12 +56,12 @@ object Osgi { headers foreach { case (k, v) => builder.setProperty(k, v) } val includeRes = resourceDirectories.filter(_.exists).map(_.getAbsolutePath).mkString(",") if(!includeRes.isEmpty) builder.setProperty(INCLUDERESOURCE, includeRes) - builder.getProperties.foreach { case (k, v) => log.debug(s"bnd: $k: $v") } + builder.getProperties.asScala.foreach { case (k, v) => log.debug(s"bnd: $k: $v") } // builder.build is not thread-safe because it uses a static SimpleDateFormat. This ensures // that all calls to builder.build are serialized. val jar = synchronized { builder.build } - builder.getWarnings.foreach(s => log.warn(s"bnd: $s")) - builder.getErrors.foreach(s => log.error(s"bnd: $s")) + builder.getWarnings.asScala.foreach(s => log.warn(s"bnd: $s")) + builder.getErrors.asScala.foreach(s => log.error(s"bnd: $s")) IO.createDirectory(artifactPath.getParentFile) jar.write(artifactPath) artifactPath diff --git a/src/compiler/scala/tools/cmd/Property.scala b/src/compiler/scala/tools/cmd/Property.scala index e6262a7e40a4..18bedd6f7e17 100644 --- a/src/compiler/scala/tools/cmd/Property.scala +++ b/src/compiler/scala/tools/cmd/Property.scala @@ -65,8 +65,8 @@ trait Property extends Reference { propertiesToOptions(loadProperties(file)) def propertiesToOptions(props: java.util.Properties): List[String] = { - import scala.collection.JavaConversions._ - propertiesToOptions(props.toList) + import scala.collection.JavaConverters._ + propertiesToOptions(props.asScala.toList) } def propertiesToOptions(props: List[(String, String)]) = props flatMap propMapper } diff --git a/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala b/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala index e99cce9186aa..c82ed68da8e4 100644 --- a/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala +++ b/src/compiler/scala/tools/nsc/GenericRunnerSettings.scala @@ -6,10 +6,10 @@ package scala.tools.nsc import java.net.URL -import scala.tools.util.PathResolverFactory +import scala.tools.util.PathResolver class GenericRunnerSettings(error: String => Unit) extends Settings(error) { - lazy val classpathURLs: Seq[URL] = PathResolverFactory.create(this).resultAsURLs + lazy val classpathURLs: Seq[URL] = new PathResolver(this).resultAsURLs val howtorun = ChoiceSetting( diff --git a/src/compiler/scala/tools/nsc/Global.scala b/src/compiler/scala/tools/nsc/Global.scala index 847c4cb2d1a1..7417d9c09ddc 100644 --- a/src/compiler/scala/tools/nsc/Global.scala +++ b/src/compiler/scala/tools/nsc/Global.scala @@ -13,7 +13,7 @@ import java.nio.charset.{Charset, CharsetDecoder, IllegalCharsetNameException, U import scala.collection.{immutable, mutable} import io.{AbstractFile, Path, SourceReader} import reporters.Reporter -import util.{ClassFileLookup, ClassPath, StatisticsInfo, returning} +import util.{ClassPath, StatisticsInfo, returning} import scala.reflect.ClassTag import scala.reflect.internal.util.{BatchSourceFile, NoSourceFile, ScalaClassLoader, ScriptSourceFile, SourceFile} import scala.reflect.internal.pickling.PickleBuffer @@ -30,7 +30,6 @@ import backend.jvm.GenBCode import scala.language.postfixOps import scala.tools.nsc.ast.{TreeGen => AstTreeGen} import scala.tools.nsc.classpath._ -import scala.tools.nsc.settings.ClassPathRepresentationType class Global(var currentSettings: Settings, var reporter: Reporter) extends SymbolTable @@ -54,12 +53,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) class GlobalMirror extends Roots(NoSymbol) { val universe: self.type = self - def rootLoader: LazyType = { - settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Flat => new loaders.PackageLoaderUsingFlatClassPath(FlatClassPath.RootPackage, flatClassPath) - case ClassPathRepresentationType.Recursive => new loaders.PackageLoader(recursiveClassPath) - } - } + def rootLoader: LazyType = new loaders.PackageLoader(ClassPath.RootPackage, classPath) override def toString = "compiler mirror" } implicit val MirrorTag: ClassTag[Mirror] = ClassTag[Mirror](classOf[GlobalMirror]) @@ -102,14 +96,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) type ThisPlatform = JavaPlatform { val global: Global.this.type } lazy val platform: ThisPlatform = new GlobalPlatform - def classPath: ClassFileLookup[AbstractFile] = settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Flat => flatClassPath - case ClassPathRepresentationType.Recursive => recursiveClassPath - } - - private def recursiveClassPath: ClassPath[AbstractFile] = platform.classPath - - private def flatClassPath: FlatClassPath = platform.flatClassPath + def classPath: ClassPath = platform.classPath // sub-components -------------------------------------------------- @@ -394,15 +381,18 @@ class Global(var currentSettings: Settings, var reporter: Reporter) if (settings.debug && (settings.verbose || currentRun.size < 5)) inform("[running phase " + name + " on " + unit + "]") + if (!cancelled(unit)) { + currentRun.informUnitStarting(this, unit) + try withCurrentUnitNoLog(unit)(task) + finally currentRun.advanceUnit() + } + } + final def withCurrentUnitNoLog(unit: CompilationUnit)(task: => Unit) { val unit0 = currentUnit try { currentRun.currentUnit = unit - if (!cancelled(unit)) { - currentRun.informUnitStarting(this, unit) - task - } - currentRun.advanceUnit() + task } finally { //assert(currentUnit == unit) currentRun.currentUnit = unit0 @@ -768,17 +758,10 @@ class Global(var currentSettings: Settings, var reporter: Reporter) /** Extend classpath of `platform` and rescan updated packages. */ def extendCompilerClassPath(urls: URL*): Unit = { - if (settings.YclasspathImpl.value == ClassPathRepresentationType.Flat) { - val urlClasspaths = urls.map(u => FlatClassPathFactory.newClassPath(AbstractFile.getURL(u), settings)) - val newClassPath = AggregateFlatClassPath.createAggregate(platform.flatClassPath +: urlClasspaths : _*) - platform.currentFlatClassPath = Some(newClassPath) - invalidateClassPathEntries(urls.map(_.getPath): _*) - } else { - val newClassPath = platform.classPath.mergeUrlsIntoClassPath(urls: _*) - platform.currentClassPath = Some(newClassPath) - // Reload all specified jars into this compiler instance - invalidateClassPathEntries(urls.map(_.getPath): _*) - } + val urlClasspaths = urls.map(u => ClassPathFactory.newClassPath(AbstractFile.getURL(u), settings)) + val newClassPath = AggregateClassPath.createAggregate(platform.classPath +: urlClasspaths : _*) + platform.currentClassPath = Some(newClassPath) + invalidateClassPathEntries(urls.map(_.getPath): _*) } // ------------ Invalidations --------------------------------- @@ -810,28 +793,26 @@ class Global(var currentSettings: Settings, var reporter: Reporter) * entries on the classpath. */ def invalidateClassPathEntries(paths: String*): Unit = { - implicit object ClassPathOrdering extends Ordering[ClassFileLookup[AbstractFile]] { - def compare(a:ClassFileLookup[AbstractFile], b:ClassFileLookup[AbstractFile]) = a.asClassPathString compare b.asClassPathString + implicit object ClassPathOrdering extends Ordering[ClassPath] { + def compare(a: ClassPath, b: ClassPath): Int = a.asClassPathString compareTo b.asClassPathString } val invalidated, failed = new mutable.ListBuffer[ClassSymbol] - def assoc(path: String): Option[(ClassFileLookup[AbstractFile], ClassFileLookup[AbstractFile])] = { - def origin(lookup: ClassFileLookup[AbstractFile]): Option[String] = lookup match { - case cp: ClassPath[_] => cp.origin + def assoc(path: String): Option[(ClassPath, ClassPath)] = { + def origin(lookup: ClassPath): Option[String] = lookup match { case cp: JFileDirectoryLookup[_] => Some(cp.dir.getPath) case cp: ZipArchiveFileLookup[_] => Some(cp.zipFile.getPath) case _ => None } - def entries(lookup: ClassFileLookup[AbstractFile]): Seq[ClassFileLookup[AbstractFile]] = lookup match { - case cp: ClassPath[_] => cp.entries - case cp: AggregateFlatClassPath => cp.aggregates - case cp: FlatClassPath => Seq(cp) + def entries(lookup: ClassPath): Seq[ClassPath] = lookup match { + case cp: AggregateClassPath => cp.aggregates + case cp: ClassPath => Seq(cp) } val dir = AbstractFile.getDirectory(path) // if path is a `jar`, this is a FileZipArchive (isDirectory is true) val canonical = dir.canonicalPath // this is the canonical path of the .jar - def matchesCanonical(e: ClassFileLookup[AbstractFile]) = origin(e) match { + def matchesCanonical(e: ClassPath) = origin(e) match { case Some(opath) => AbstractFile.getDirectory(opath).canonicalPath == canonical case None => @@ -839,7 +820,7 @@ class Global(var currentSettings: Settings, var reporter: Reporter) } entries(classPath) find matchesCanonical match { case Some(oldEntry) => - Some(oldEntry -> ClassFileLookup.createForFile(dir, classPath, settings)) + Some(oldEntry -> ClassPathFactory.newClassPath(dir, settings)) case None => error(s"Error adding entry to classpath. During invalidation, no entry named $path in classpath $classPath") None @@ -849,19 +830,15 @@ class Global(var currentSettings: Settings, var reporter: Reporter) if (subst.nonEmpty) { platform updateClassPath subst informProgress(s"classpath updated on entries [${subst.keys mkString ","}]") - def mkClassPath(elems: Iterable[ClassFileLookup[AbstractFile]]): ClassFileLookup[AbstractFile] = + def mkClassPath(elems: Iterable[ClassPath]): ClassPath = if (elems.size == 1) elems.head - else ClassFileLookup.createAggregate(elems, classPath) + else AggregateClassPath.createAggregate(elems.toSeq: _*) val oldEntries = mkClassPath(subst.keys) val newEntries = mkClassPath(subst.values) classPath match { - case rcp: ClassPath[_] => mergeNewEntriesRecursive( - newEntries.asInstanceOf[ClassPath[AbstractFile]], RootClass, Some(rcp), Some(oldEntries.asInstanceOf[ClassPath[AbstractFile]]), - invalidated, failed) - - case fcp: FlatClassPath => mergeNewEntriesFlat( + case cp: ClassPath => mergeNewEntries( RootClass, "", - oldEntries.asInstanceOf[FlatClassPath], newEntries.asInstanceOf[FlatClassPath], fcp, + oldEntries, newEntries, cp, invalidated, failed) } } @@ -872,69 +849,6 @@ class Global(var currentSettings: Settings, var reporter: Reporter) show("could not invalidate system packages", failed) } - /** Merges new classpath entries into the symbol table - * - * @param newEntries The new classpath entries - * @param root The root symbol to be resynced (a package class) - * @param allEntries Optionally, the corresponding package in the complete current classpath - * @param oldEntries Optionally, the corresponding package in the old classpath entries - * @param invalidated A listbuffer collecting the invalidated package classes - * @param failed A listbuffer collecting system package classes which could not be invalidated - * - * The merging strategy is determined by the absence or presence of classes and packages. - * - * If either oldEntries or newEntries contains classes, root is invalidated provided that a corresponding package - * exists in allEntries. Otherwise it is removed. - * Otherwise, the action is determined by the following matrix, with columns: - * - * old sym action - * + + recurse into all child packages of newEntries - * - + invalidate root - * - - create and enter root - * - * Here, old means classpath, and sym means symboltable. + is presence of an entry in its column, - is absence. - */ - private def mergeNewEntriesRecursive(newEntries: ClassPath[AbstractFile], root: ClassSymbol, - allEntries: Option[ClassPath[AbstractFile]], oldEntries: Option[ClassPath[AbstractFile]], - invalidated: mutable.ListBuffer[ClassSymbol], failed: mutable.ListBuffer[ClassSymbol]) { - ifDebug(informProgress(s"syncing $root, $oldEntries -> $newEntries")) - - val getPackageName: ClassPath[AbstractFile] => String = _.name - def hasClasses(cp: Option[ClassPath[AbstractFile]]) = cp.isDefined && cp.get.classes.nonEmpty - def invalidateOrRemove(root: ClassSymbol) = { - allEntries match { - case Some(cp) => root setInfo new loaders.PackageLoader(cp) - case None => root.owner.info.decls unlink root.sourceModule - } - invalidated += root - } - def subPackage(cp: ClassPath[AbstractFile], name: String): Option[ClassPath[AbstractFile]] = - cp.packages find (cp1 => getPackageName(cp1) == name) - - val classesFound = hasClasses(oldEntries) || newEntries.classes.nonEmpty - if (classesFound && !isSystemPackageClass(root)) { - invalidateOrRemove(root) - } else { - if (classesFound) { - if (root.isRoot) invalidateOrRemove(EmptyPackageClass) - else failed += root - } - if (oldEntries.isEmpty) invalidateOrRemove(root) - else - for (pstr <- newEntries.packages.map(getPackageName)) { - val pname = newTermName(pstr) - val pkg = (root.info decl pname) orElse { - // package does not exist in symbol table, create symbol to track it - assert(subPackage(oldEntries.get, pstr).isEmpty) - loaders.enterPackage(root, pstr, new loaders.PackageLoader(allEntries.get)) - } - mergeNewEntriesRecursive(subPackage(newEntries, pstr).get, pkg.moduleClass.asClass, - subPackage(allEntries.get, pstr), subPackage(oldEntries.get, pstr), - invalidated, failed) - } - } - } - /** * Merges new classpath entries into the symbol table * @@ -953,20 +867,19 @@ class Global(var currentSettings: Settings, var reporter: Reporter) * Otherwise, sub-packages in newEntries are looked up in the symbol table (created if * non-existent) and the merge function is called recursively. */ - private def mergeNewEntriesFlat( - packageClass: ClassSymbol, fullPackageName: String, - oldEntries: FlatClassPath, newEntries: FlatClassPath, fullClasspath: FlatClassPath, - invalidated: mutable.ListBuffer[ClassSymbol], failed: mutable.ListBuffer[ClassSymbol]): Unit = { + private def mergeNewEntries(packageClass: ClassSymbol, fullPackageName: String, + oldEntries: ClassPath, newEntries: ClassPath, fullClasspath: ClassPath, + invalidated: mutable.ListBuffer[ClassSymbol], failed: mutable.ListBuffer[ClassSymbol]): Unit = { ifDebug(informProgress(s"syncing $packageClass, $oldEntries -> $newEntries")) - def packageExists(cp: FlatClassPath): Boolean = { + def packageExists(cp: ClassPath): Boolean = { val (parent, _) = PackageNameUtils.separatePkgAndClassNames(fullPackageName) cp.packages(parent).exists(_.name == fullPackageName) } def invalidateOrRemove(pkg: ClassSymbol) = { if (packageExists(fullClasspath)) - pkg setInfo new loaders.PackageLoaderUsingFlatClassPath(fullPackageName, fullClasspath) + pkg setInfo new loaders.PackageLoader(fullPackageName, fullClasspath) else pkg.owner.info.decls unlink pkg.sourceModule invalidated += pkg @@ -984,9 +897,9 @@ class Global(var currentSettings: Settings, var reporter: Reporter) val (_, subPackageName) = PackageNameUtils.separatePkgAndClassNames(p.name) val subPackage = packageClass.info.decl(newTermName(subPackageName)) orElse { // package does not exist in symbol table, create a new symbol - loaders.enterPackage(packageClass, subPackageName, new loaders.PackageLoaderUsingFlatClassPath(p.name, fullClasspath)) + loaders.enterPackage(packageClass, subPackageName, new loaders.PackageLoader(p.name, fullClasspath)) } - mergeNewEntriesFlat( + mergeNewEntries( subPackage.moduleClass.asClass, p.name, oldEntries, newEntries, fullClasspath, invalidated, failed) diff --git a/src/compiler/scala/tools/nsc/ScriptRunner.scala b/src/compiler/scala/tools/nsc/ScriptRunner.scala index bf93ad30bc92..1f66657d8dd2 100644 --- a/src/compiler/scala/tools/nsc/ScriptRunner.scala +++ b/src/compiler/scala/tools/nsc/ScriptRunner.scala @@ -8,10 +8,8 @@ package tools.nsc import io.{ AbstractFile, Directory, File, Path } import java.io.IOException -import scala.tools.nsc.classpath.DirectoryFlatClassPath +import scala.tools.nsc.classpath.DirectoryClassPath import scala.tools.nsc.reporters.{Reporter,ConsoleReporter} -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.nsc.util.ClassPath.DefaultJavaContext import util.Exceptional.unwrap /** An object that runs Scala code in script files. @@ -115,10 +113,7 @@ class ScriptRunner extends HasCompileSocket { } def hasClassToRun(d: Directory): Boolean = { - val cp = settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Recursive => DefaultJavaContext.newClassPath(AbstractFile.getDirectory(d)) - case ClassPathRepresentationType.Flat => DirectoryFlatClassPath(d.jfile) - } + val cp = DirectoryClassPath(d.jfile) cp.findClass(mainClass).isDefined } diff --git a/src/compiler/scala/tools/nsc/ast/TreeGen.scala b/src/compiler/scala/tools/nsc/ast/TreeGen.scala index 0786ceb7c246..de1535e96660 100644 --- a/src/compiler/scala/tools/nsc/ast/TreeGen.scala +++ b/src/compiler/scala/tools/nsc/ast/TreeGen.scala @@ -274,8 +274,19 @@ abstract class TreeGen extends scala.reflect.internal.TreeGen with TreeDSL { } // used to create the lifted method that holds a function's body - def mkLiftedFunctionBodyMethod(localTyper: analyzer.Typer)(owner: Symbol, fun: Function) = - mkMethodForFunctionBody(localTyper)(owner, fun, nme.ANON_FUN_NAME)(additionalFlags = ARTIFACT) + def mkLiftedFunctionBodyMethod(localTyper: analyzer.Typer)(owner: Symbol, fun: Function) = { + def nonLocalEnclosingMember(sym: Symbol) = { + if (sym.isLocalDummy) sym.enclClass.primaryConstructor else { + if (sym.isLocalToBlock) sym.originalOwner else sym + } + } + val ownerName = nonLocalEnclosingMember(fun.symbol.originalOwner).name match { + case nme.CONSTRUCTOR => nme.NEWkw // do as javac does for the suffix, prefer "new" to "$lessinit$greater$1" + case x => x + } + val newName = nme.ANON_FUN_NAME.append(nme.NAME_JOIN_STRING).append(ownerName) + mkMethodForFunctionBody(localTyper)(owner, fun, newName)(additionalFlags = ARTIFACT) + } /** @@ -310,6 +321,40 @@ abstract class TreeGen extends scala.reflect.internal.TreeGen with TreeDSL { newDefDef(methSym, moveToMethod(useMethodParams(fun.body)))(tpt = TypeTree(resTp)) } + /** + * Create a new `DefDef` based on `orig` with an explicit self parameter. + * + * Details: + * - Must by runs after erasure + * - If `maybeClone` is the identity function, this runs "in place" + * and mutates the symbol of `orig`. `orig` should be discarded + * - Symbol owners and returns are substituted + * - Recursive calls are not rewritten. This is correct if we assume + * that we either: + * - are in "in-place" mode, but can guarantee that no recursive calls exists + * - are associating the RHS with a cloned symbol, but intend for the original + * method to remain and for recursive calls to target it. + */ + final def mkStatic(orig: DefDef, maybeClone: Symbol => Symbol): DefDef = { + assert(phase.erasedTypes, phase) + val origSym = orig.symbol + val origParams = orig.symbol.info.params + val newSym = maybeClone(orig.symbol) + newSym.setFlag(STATIC) + // Add an explicit self parameter + val selfParamSym = newSym.newSyntheticValueParam(newSym.owner.typeConstructor, nme.SELF) + newSym.updateInfo(newSym.info match { + case mt @ MethodType(params, res) => copyMethodType(mt, selfParamSym :: params, res) + }) + val selfParam = ValDef(selfParamSym) + val oldSyms = origParams + val newSyms = newSym.info.params.drop(1) + + val rhs = orig.rhs.substituteThis(newSym.owner, atPos(newSym.pos)(gen.mkAttributedIdent(selfParamSym))) + .substituteSymbols(oldSyms, newSyms).changeOwner(origSym -> newSym) + treeCopy.DefDef(orig, orig.mods, orig.name, orig.tparams, (selfParam :: orig.vparamss.head) :: Nil, orig.tpt, rhs).setSymbol(newSym) + } + // TODO: the rewrite to AbstractFunction is superfluous once we compile FunctionN to a SAM type (aka functional interface) def functionClassType(fun: Function): Type = if (isFunctionType(fun.tpe)) abstractFunctionType(fun.vparams.map(_.symbol.tpe), fun.body.tpe.deconst) diff --git a/src/compiler/scala/tools/nsc/backend/JavaPlatform.scala b/src/compiler/scala/tools/nsc/backend/JavaPlatform.scala index 0e2f059a3671..dc63b335ccc7 100644 --- a/src/compiler/scala/tools/nsc/backend/JavaPlatform.scala +++ b/src/compiler/scala/tools/nsc/backend/JavaPlatform.scala @@ -7,11 +7,9 @@ package scala.tools.nsc package backend import io.AbstractFile -import scala.tools.nsc.classpath.{AggregateFlatClassPath, FlatClassPath} -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.nsc.util.{ClassFileLookup, ClassPath, MergedClassPath} -import scala.tools.util.FlatClassPathResolver +import scala.tools.nsc.classpath.AggregateClassPath import scala.tools.util.PathResolver +import scala.tools.nsc.util.ClassPath trait JavaPlatform extends Platform { val global: Global @@ -19,38 +17,20 @@ trait JavaPlatform extends Platform { import global._ import definitions._ - private[nsc] var currentClassPath: Option[MergedClassPath[AbstractFile]] = None - - def classPath: ClassPath[AbstractFile] = { - assert(settings.YclasspathImpl.value == ClassPathRepresentationType.Recursive, - "To use recursive classpath representation you must enable it with -YclasspathImpl:recursive compiler option.") + private[nsc] var currentClassPath: Option[ClassPath] = None + private[nsc] def classPath: ClassPath = { if (currentClassPath.isEmpty) currentClassPath = Some(new PathResolver(settings).result) currentClassPath.get } - private[nsc] var currentFlatClassPath: Option[FlatClassPath] = None - - private[nsc] def flatClassPath: FlatClassPath = { - assert(settings.YclasspathImpl.value == ClassPathRepresentationType.Flat, - "To use flat classpath representation you must enable it with -YclasspathImpl:flat compiler option.") - - if (currentFlatClassPath.isEmpty) currentFlatClassPath = Some(new FlatClassPathResolver(settings).result) - currentFlatClassPath.get - } - /** Update classpath with a substituted subentry */ - def updateClassPath(subst: Map[ClassFileLookup[AbstractFile], ClassFileLookup[AbstractFile]]) = global.classPath match { - case cp: ClassPath[AbstractFile] => - val s = subst.asInstanceOf[Map[ClassPath[AbstractFile], ClassPath[AbstractFile]]] - currentClassPath = Some(new MergedClassPath(cp.entries map (e => s.getOrElse(e, e)), cp.context)) - - case AggregateFlatClassPath(entries) => - val s = subst.asInstanceOf[Map[FlatClassPath, FlatClassPath]] - currentFlatClassPath = Some(AggregateFlatClassPath(entries map (e => s.getOrElse(e, e)))) + def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit = global.classPath match { + case AggregateClassPath(entries) => + currentClassPath = Some(AggregateClassPath(entries map (e => subst.getOrElse(e, e)))) - case cp: FlatClassPath => - currentFlatClassPath = Some(subst.getOrElse(cp, cp).asInstanceOf[FlatClassPath]) + case cp: ClassPath => + currentClassPath = Some(subst.getOrElse(cp, cp)) } def platformPhases = List( diff --git a/src/compiler/scala/tools/nsc/backend/Platform.scala b/src/compiler/scala/tools/nsc/backend/Platform.scala index 369bcc44ed5f..e464768bb361 100644 --- a/src/compiler/scala/tools/nsc/backend/Platform.scala +++ b/src/compiler/scala/tools/nsc/backend/Platform.scala @@ -6,9 +6,8 @@ package scala.tools.nsc package backend -import util.{ClassFileLookup, ClassPath} import io.AbstractFile -import scala.tools.nsc.classpath.FlatClassPath +import scala.tools.nsc.util.ClassPath /** The platform dependent pieces of Global. */ @@ -16,14 +15,11 @@ trait Platform { val symbolTable: symtab.SymbolTable import symbolTable._ - /** The old, recursive implementation of compiler classpath. */ - def classPath: ClassPath[AbstractFile] - /** The new implementation of compiler classpath. */ - private[nsc] def flatClassPath: FlatClassPath + private[nsc] def classPath: ClassPath /** Update classpath with a substitution that maps entries to entries */ - def updateClassPath(subst: Map[ClassFileLookup[AbstractFile], ClassFileLookup[AbstractFile]]) + def updateClassPath(subst: Map[ClassPath, ClassPath]) /** Any platform-specific phases. */ def platformPhases: List[SubComponent] diff --git a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala index 32f8c7826f4c..630b2b6c7f3e 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/AsmUtils.scala @@ -9,8 +9,7 @@ import scala.tools.asm.tree.{InsnList, AbstractInsnNode, ClassNode, MethodNode} import java.io.{StringWriter, PrintWriter} import scala.tools.asm.util.{CheckClassAdapter, TraceClassVisitor, TraceMethodVisitor, Textifier} import scala.tools.asm.{ClassReader, ClassWriter, Attribute} -import scala.collection.convert.decorateAsScala._ -import scala.collection.convert.decorateAsJava._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.analysis.InitialProducer import scala.tools.nsc.backend.jvm.opt.InlineInfoAttributePrototype diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala index 22587c68f7a8..8d90874c87d0 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeBodyBuilder.scala @@ -117,15 +117,16 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder { // binary operation case rarg :: Nil => - resKind = tpeTK(larg).maxType(tpeTK(rarg)) - if (scalaPrimitives.isShiftOp(code) || scalaPrimitives.isBitwiseOp(code)) { + val isShiftOp = scalaPrimitives.isShiftOp(code) + resKind = tpeTK(larg).maxType(if (isShiftOp) INT else tpeTK(rarg)) + + if (isShiftOp || scalaPrimitives.isBitwiseOp(code)) { assert(resKind.isIntegralType || (resKind == BOOL), s"$resKind incompatible with arithmetic modulo operation.") } genLoad(larg, resKind) - genLoad(rarg, // check .NET size of shift arguments! - if (scalaPrimitives.isShiftOp(code)) INT else resKind) + genLoad(rarg, if (isShiftOp) INT else resKind) (code: @switch) match { case ADD => bc add resKind @@ -1059,12 +1060,6 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder { receiverClass.info // ensure types the type is up to date; erasure may add lateINTERFACE to traits val receiverName = internalName(receiverClass) - // super calls are only allowed to direct parents - if (style.isSuper && receiverClass.isTraitOrInterface && !cnode.interfaces.contains(receiverName)) { - thisBType.info.get.inlineInfo.lateInterfaces += receiverName - cnode.interfaces.add(receiverName) - } - def needsInterfaceCall(sym: Symbol) = { sym.isTraitOrInterface || sym.isJavaDefined && sym.isNonBottomSubClass(definitions.ClassfileAnnotationClass) @@ -1081,7 +1076,16 @@ abstract class BCodeBodyBuilder extends BCodeSkelBuilder { case Virtual => if (needsInterfaceCall(receiverClass)) bc.invokeinterface(receiverName, jname, mdescr, pos) else bc.invokevirtual (receiverName, jname, mdescr, pos) - case Super => bc.invokespecial (receiverName, jname, mdescr, pos) + case Super => + if (receiverClass.isTrait && method.owner.isTrait && !method.owner.isJavaDefined) { + val staticDesc = MethodBType(typeToBType(method.owner.info) :: method.info.paramTypes.map(typeToBType), typeToBType(method.info.resultType)).descriptor + bc.invokestatic(receiverName, jname, staticDesc, pos) + } else { + if (receiverClass.isTraitOrInterface && !cnode.interfaces.contains(receiverName)) + reporter.error(pos, s"Implementation restruction: unable to emit a super call to ${receiverName}.${method.name} from ${cnode.name}. Add $receiverName as a direct parent of ${cnode.name}") + else + bc.invokespecial (receiverName, jname, mdescr, pos) + } } bmType.returnType diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala index f190c1f2de26..f59f8c2c6dba 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BCodeSkelBuilder.scala @@ -489,7 +489,18 @@ abstract class BCodeSkelBuilder extends BCodeHelpers { case ValDef(mods, name, tpt, rhs) => () // fields are added in `genPlainClass()`, via `addClassFields()` - case dd : DefDef => genDefDef(dd) + case dd : DefDef => + if (dd.symbol.owner.isTrait && dd.rhs != EmptyTree && !dd.symbol.isPrivate && !dd.symbol.hasFlag(Flags.STATIC)) { + // Split concrete methods in traits (including mixin constructors) into a static method + // with an explicit this parameter, and a non-static forwarder method. + val staticDefDef = global.gen.mkStatic(dd, _.cloneSymbol) + val forwarderDefDef = { + val forwarderBody = Apply(global.gen.mkAttributedRef(staticDefDef.symbol), This(dd.symbol.owner).setType(dd.symbol.owner.typeConstructor) :: dd.vparamss.head.map(p => global.gen.mkAttributedIdent(p.symbol))).setType(dd.symbol.info.resultType) + deriveDefDef(dd)(_ => global.atPos(dd.pos)(forwarderBody)) + } + genDefDef(staticDefDef) + genDefDef(forwarderDefDef) + } else genDefDef(dd) case Template(_, _, body) => body foreach gen diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala index 49353afef4d9..9d217d8712d4 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypes.scala @@ -17,7 +17,7 @@ import scala.tools.nsc.backend.jvm.BTypes.{InlineInfo, MethodInlineInfo} import scala.tools.nsc.backend.jvm.BackendReporting._ import scala.tools.nsc.backend.jvm.analysis.BackendUtils import scala.tools.nsc.backend.jvm.opt._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.collection.mutable.ListBuffer import scala.tools.nsc.settings.ScalaSettings @@ -225,8 +225,7 @@ abstract class BTypes { val inlineInfo = inlineInfoFromClassfile(classNode) - val classfileInterfaces: List[ClassBType] = classNode.interfaces.asScala.map(classBTypeFromParsedClassfile)(collection.breakOut) - val interfaces = classfileInterfaces.filterNot(i => inlineInfo.lateInterfaces.contains(i.internalName)) + val interfaces: List[ClassBType] = classNode.interfaces.asScala.map(classBTypeFromParsedClassfile)(collection.breakOut) classBType.info = Right(ClassInfo(superClass, interfaces, flags, nestedClasses, nestedInfo, inlineInfo)) classBType @@ -1147,25 +1146,6 @@ object BTypes { sam: Option[String], methodInfos: Map[String, MethodInlineInfo], warning: Option[ClassInlineInfoWarning]) { - /** - * A super call (invokespecial) to a default method T.m is only allowed if the interface T is - * a direct parent of the class. Super calls are introduced for example in Mixin when generating - * forwarder methods: - * - * trait T { override def clone(): Object = "hi" } - * trait U extends T - * class C extends U - * - * The class C gets a forwarder that invokes T.clone(). During code generation the interface T - * is added as direct parent to class C. Note that T is not a (direct) parent in the frontend - * type of class C. - * - * All interfaces that are added to a class during code generation are added to this buffer and - * stored in the InlineInfo classfile attribute. This ensures that the ClassBTypes for a - * specific class is the same no matter if it's constructed from a Symbol or from a classfile. - * This is tested in BTypesFromClassfileTest. - */ - val lateInterfaces: ListBuffer[InternalName] = ListBuffer.empty } val EmptyInlineInfo = InlineInfo(false, None, Map.empty, None) diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala index d10b6c8dbaf6..40b0fe8339ba 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BTypesFromSymbols.scala @@ -234,18 +234,10 @@ class BTypesFromSymbols[G <: Global](val global: G) extends BTypes { val allParents = classParents ++ classSym.annotations.flatMap(newParentForAnnotation) - // We keep the superClass when computing minimizeParents to eliminate more interfaces. - // Example: T can be eliminated from D - // trait T - // class C extends T - // class D extends C with T - val interfaces = erasure.minimizeParents(allParents) match { + val interfaces = allParents match { case superClass :: ifs if !isInterfaceOrTrait(superClass.typeSymbol) => ifs case ifs => - // minimizeParents removes the superclass if it's redundant, for example: - // trait A - // class C extends Object with A // minimizeParents removes Object ifs } interfaces.map(_.typeSymbol) diff --git a/src/compiler/scala/tools/nsc/backend/jvm/BackendReporting.scala b/src/compiler/scala/tools/nsc/backend/jvm/BackendReporting.scala index 01206aa6ebb2..4287c24dc806 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/BackendReporting.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/BackendReporting.scala @@ -42,15 +42,15 @@ object BackendReporting { def assertionError(message: String): Nothing = throw new AssertionError(message) implicit class RightBiasedEither[A, B](val v: Either[A, B]) extends AnyVal { - def map[U](f: B => U) = v.right.map(f) - def flatMap[BB](f: B => Either[A, BB]) = v.right.flatMap(f) + def map[C](f: B => C): Either[A, C] = v.right.map(f) + def flatMap[C](f: B => Either[A, C]): Either[A, C] = v.right.flatMap(f) def withFilter(f: B => Boolean)(implicit empty: A): Either[A, B] = v match { case Left(_) => v case Right(e) => if (f(e)) v else Left(empty) // scalaz.\/ requires an implicit Monoid m to get m.empty } - def foreach[U](f: B => U) = v.right.foreach(f) + def foreach[U](f: B => U): Unit = v.right.foreach(f) - def getOrElse[BB >: B](alt: => BB): BB = v.right.getOrElse(alt) + def getOrElse[C >: B](alt: => C): C = v.right.getOrElse(alt) /** * Get the value, fail with an assertion if this is an error. @@ -101,11 +101,14 @@ object BackendReporting { else "" } - case MethodNotFound(name, descriptor, ownerInternalName, missingClasses) => - val (javaDef, others) = missingClasses.partition(_.definedInJavaSource) - s"The method $name$descriptor could not be found in the class $ownerInternalName or any of its parents." + - (if (others.isEmpty) "" else others.map(_.internalName).mkString("\nNote that the following parent classes could not be found on the classpath: ", ", ", "")) + - (if (javaDef.isEmpty) "" else javaDef.map(_.internalName).mkString("\nNote that the following parent classes are defined in Java sources (mixed compilation), no bytecode is available: ", ",", "")) + case MethodNotFound(name, descriptor, ownerInternalName, missingClass) => + val missingClassWarning = missingClass match { + case None => "" + case Some(c) => + if (c.definedInJavaSource) s"\nNote that the parent class ${c.internalName} is defined in a Java source (mixed compilation), no bytecode is available." + else s"\nNote that the parent class ${c.internalName} could not be found on the classpath." + } + s"The method $name$descriptor could not be found in the class $ownerInternalName or any of its parents." + missingClassWarning case FieldNotFound(name, descriptor, ownerInternalName, missingClass) => s"The field node $name$descriptor could not be found because the classfile $ownerInternalName cannot be found on the classpath." + @@ -127,7 +130,7 @@ object BackendReporting { } case class ClassNotFound(internalName: InternalName, definedInJavaSource: Boolean) extends MissingBytecodeWarning - case class MethodNotFound(name: String, descriptor: String, ownerInternalNameOrArrayDescriptor: InternalName, missingClasses: List[ClassNotFound]) extends MissingBytecodeWarning { + case class MethodNotFound(name: String, descriptor: String, ownerInternalNameOrArrayDescriptor: InternalName, missingClass: Option[ClassNotFound]) extends MissingBytecodeWarning { def isArrayMethod = ownerInternalNameOrArrayDescriptor.charAt(0) == '[' } case class FieldNotFound(name: String, descriptor: String, ownerInternalName: InternalName, missingClass: Option[ClassNotFound]) extends MissingBytecodeWarning diff --git a/src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala b/src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala index 0b53ea2fb1d0..1feca56923f4 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/CoreBTypes.scala @@ -107,6 +107,7 @@ class CoreBTypes[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes: BTFS) { lazy val juHashMapRef : ClassBType = classBTypeFromSymbol(JavaUtilHashMap) // java/util/HashMap lazy val sbScalaBeanInfoRef : ClassBType = classBTypeFromSymbol(requiredClass[scala.beans.ScalaBeanInfo]) lazy val jliSerializedLambdaRef : ClassBType = classBTypeFromSymbol(requiredClass[java.lang.invoke.SerializedLambda]) + lazy val jliMethodHandleRef : ClassBType = classBTypeFromSymbol(requiredClass[java.lang.invoke.MethodHandle]) lazy val jliMethodHandlesRef : ClassBType = classBTypeFromSymbol(requiredClass[java.lang.invoke.MethodHandles]) lazy val jliMethodHandlesLookupRef : ClassBType = classBTypeFromSymbol(exitingPickler(getRequiredClass("java.lang.invoke.MethodHandles.Lookup"))) // didn't find a reliable non-stringly-typed way that works for inner classes in the backend lazy val jliMethodTypeRef : ClassBType = classBTypeFromSymbol(requiredClass[java.lang.invoke.MethodType]) @@ -320,6 +321,7 @@ trait CoreBTypesProxyGlobalIndependent[BTS <: BTypes] { def jliCallSiteRef : ClassBType def jliMethodTypeRef : ClassBType def jliSerializedLambdaRef : ClassBType + def jliMethodHandleRef : ClassBType def jliMethodHandlesLookupRef : ClassBType def srBoxesRunTimeRef : ClassBType def srBoxedUnitRef : ClassBType @@ -383,6 +385,7 @@ final class CoreBTypesProxy[BTFS <: BTypesFromSymbols[_ <: Global]](val bTypes: def juHashMapRef : ClassBType = _coreBTypes.juHashMapRef def sbScalaBeanInfoRef : ClassBType = _coreBTypes.sbScalaBeanInfoRef def jliSerializedLambdaRef : ClassBType = _coreBTypes.jliSerializedLambdaRef + def jliMethodHandleRef : ClassBType = _coreBTypes.jliMethodHandleRef def jliMethodHandlesRef : ClassBType = _coreBTypes.jliMethodHandlesRef def jliMethodHandlesLookupRef : ClassBType = _coreBTypes.jliMethodHandlesLookupRef def jliMethodTypeRef : ClassBType = _coreBTypes.jliMethodTypeRef diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala index 65bd62a413cb..3520d5759941 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenBCode.scala @@ -135,7 +135,7 @@ abstract class GenBCode extends BCodeSyncAndTry { return } else { - try { withCurrentUnit(item.cunit)(visit(item)) } + try { withCurrentUnitNoLog(item.cunit)(visit(item)) } catch { case ex: Throwable => ex.printStackTrace() @@ -221,7 +221,7 @@ abstract class GenBCode extends BCodeSyncAndTry { */ class Worker2 { def runGlobalOptimizations(): Unit = { - import scala.collection.convert.decorateAsScala._ + import scala.collection.JavaConverters._ // add classes to the bytecode repo before building the call graph: the latter needs to // look up classes and methods in the code repo. diff --git a/src/compiler/scala/tools/nsc/backend/jvm/analysis/BackendUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/analysis/BackendUtils.scala index f1facce173ce..919ff68d2d33 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/analysis/BackendUtils.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/analysis/BackendUtils.scala @@ -6,14 +6,14 @@ import scala.annotation.switch import scala.tools.asm.{Handle, Type} import scala.tools.asm.Opcodes._ import scala.tools.asm.tree._ -import scala.tools.asm.tree.analysis.{Frame, BasicInterpreter, Analyzer, Value} +import scala.tools.asm.tree.analysis._ import GenBCode._ import scala.tools.nsc.backend.jvm.BTypes._ import scala.tools.nsc.backend.jvm.opt.BytecodeUtils._ import java.lang.invoke.LambdaMetafactory + import scala.collection.mutable -import scala.collection.convert.decorateAsJava._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ /** * This component hosts tools and utilities used in the backend that require access to a `BTypes` @@ -33,8 +33,13 @@ class BackendUtils[BT <: BTypes](val btypes: BT) { */ class AsmAnalyzer[V <: Value](methodNode: MethodNode, classInternalName: InternalName, val analyzer: Analyzer[V] = new Analyzer(new BasicInterpreter)) { computeMaxLocalsMaxStack(methodNode) - analyzer.analyze(classInternalName, methodNode) - def frameAt(instruction: AbstractInsnNode): Frame[V] = analyzer.frameAt(instruction, methodNode) + try { + analyzer.analyze(classInternalName, methodNode) + } catch { + case ae: AnalyzerException => + throw new AnalyzerException(null, "While processing " + classInternalName + "." + methodNode.name, ae) + } + def frameAt(instruction: AbstractInsnNode): Frame[V] = analyzer.frameAt(instruction, methodNode) } /** diff --git a/src/compiler/scala/tools/nsc/backend/jvm/analysis/ProdConsAnalyzerImpl.scala b/src/compiler/scala/tools/nsc/backend/jvm/analysis/ProdConsAnalyzerImpl.scala index 6b645cb80352..8af4bd4d5d62 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/analysis/ProdConsAnalyzerImpl.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/analysis/ProdConsAnalyzerImpl.scala @@ -18,7 +18,7 @@ import scala.tools.asm.tree.analysis._ import opt.BytecodeUtils._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ /** * This class provides additional queries over ASM's built-in `SourceValue` analysis. diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/BoxUnbox.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/BoxUnbox.scala index 16fe2e5cff27..a4cd8fce1e7b 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/BoxUnbox.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/BoxUnbox.scala @@ -12,7 +12,7 @@ import scala.tools.asm.Type import scala.tools.asm.Opcodes._ import scala.tools.asm.tree._ import scala.collection.mutable -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.BTypes.InternalName import scala.tools.nsc.backend.jvm.opt.BytecodeUtils._ diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala index 4492d0baf5ab..16590ec75c58 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/ByteCodeRepository.scala @@ -9,12 +9,11 @@ package opt import scala.tools.asm import asm.tree._ -import scala.collection.convert.decorateAsScala._ -import scala.collection.concurrent +import scala.collection.JavaConverters._ +import scala.collection.{concurrent, mutable} import scala.tools.asm.Attribute import scala.tools.nsc.backend.jvm.BackendReporting._ -import scala.tools.nsc.io.AbstractFile -import scala.tools.nsc.util.ClassFileLookup +import scala.tools.nsc.util.ClassPath import BytecodeUtils._ import ByteCodeRepository._ import BTypes.InternalName @@ -26,7 +25,7 @@ import java.util.concurrent.atomic.AtomicLong * * @param classPath The compiler classpath where classfiles are searched and read from. */ -class ByteCodeRepository[BT <: BTypes](val classPath: ClassFileLookup[AbstractFile], val btypes: BT) { +class ByteCodeRepository[BT <: BTypes](val classPath: ClassPath, val btypes: BT) { import btypes._ /** @@ -132,38 +131,135 @@ class ByteCodeRepository[BT <: BTypes](val classPath: ClassFileLookup[AbstractFi * The method node for a method matching `name` and `descriptor`, accessed in class `ownerInternalNameOrArrayDescriptor`. * The declaration of the method may be in one of the parents. * - * TODO: make sure we always return the right method, the one being invoked. write tests. - * - if there's an abstract and a concrete one. could possibly somehow the abstract be returned? - * - with traits and default methods, if there is more than one default method inherited and - * no override: what should be returned? We should not just inline one of the two. + * Note that the JVM spec performs method lookup in two steps: resolution and selection. + * + * Method resolution, defined in jvms-5.4.3.3 and jvms-5.4.3.4, is the first step and is identical + * for all invocation styles (virtual, interface, special, static). If C is the receiver class + * in the invocation instruction: + * 1 find a matching method (name and descriptor) in C + * 2 then in C's superclasses + * 3 then find the maximally-specific matching superinterface methods, succeed if there's a + * single non-abstract one. static and private methods in superinterfaces are not considered. + * 4 then pick a random non-static, non-private superinterface method. + * 5 then fail. + * + * Note that for an `invokestatic` instruction, a method reference `B.m` may resolve to `A.m`, if + * class `B` doesn't specify a matching method `m`, but the parent `A` does. + * + * Selection depends on the invocation style and is defined in jvms-6.5. + * - invokestatic: invokes the resolved method + * - invokevirtual / invokeinterface: searches for an override of the resolved method starting + * at the dynamic receiver type. the search procedure is basically the same as in resolution, + * but it fails at 4 instead of picking a superinterface method at random. + * - invokespecial: if C is the receiver in the invocation instruction, searches for an override + * of the resolved method starting at + * - the superclass of the current class, if C is a superclass of the current class + * - C otherwise + * again, the search procedure is the same. + * + * In the method here we implement method *resolution*. Whether or not the returned method is + * actually invoked at runtime depends on the invocation instruction and the class hierarchy, so + * the users (e.g. the inliner) have to be aware of method selection. + * + * Note that the returned method may be abstract (ACC_ABSTRACT), native (ACC_NATIVE) or signature + * polymorphic (methods `invoke` and `invokeExact` in class `MehtodHandles`). * * @return The [[MethodNode]] of the requested method and the [[InternalName]] of its declaring - * class, or an error message if the method could not be found. + * class, or an error message if the method could not be found. An error message is also + * returned if method resolution results in multiple default methods. */ def methodNode(ownerInternalNameOrArrayDescriptor: String, name: String, descriptor: String): Either[MethodNotFound, (MethodNode, InternalName)] = { - // on failure, returns a list of class names that could not be found on the classpath - def methodNodeImpl(ownerInternalName: InternalName): Either[List[ClassNotFound], (MethodNode, InternalName)] = { - classNode(ownerInternalName) match { - case Left(e) => Left(List(e)) - case Right(c) => - c.methods.asScala.find(m => m.name == name && m.desc == descriptor) match { - case Some(m) => Right((m, ownerInternalName)) - case None => findInParents(Option(c.superName) ++: c.interfaces.asScala.toList, Nil) - } + def findMethod(c: ClassNode): Option[MethodNode] = c.methods.asScala.find(m => m.name == name && m.desc == descriptor) + + // https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.9: "In Java SE 8, the only + // signature polymorphic methods are the invoke and invokeExact methods of the class MethodHandle. + def isSignaturePolymorphic(owner: InternalName) = owner == coreBTypes.jliMethodHandleRef.internalName && (name == "invoke" || name == "invokeExact") + + // Note: if `owner` is an interface, in the first iteration we search for a matching member in the interface itself. + // If that fails, the recursive invocation checks in the superclass (which is Object) with `publicInstanceOnly == true`. + // This is specified in jvms-5.4.3.4: interface method resolution only returns public, non-static methods of Object. + def findInSuperClasses(owner: ClassNode, publicInstanceOnly: Boolean = false): Either[ClassNotFound, Option[(MethodNode, InternalName)]] = { + findMethod(owner) match { + case Some(m) if !publicInstanceOnly || (isPublicMethod(m) && !isStaticMethod(m)) => Right(Some((m, owner.name))) + case None => + if (isSignaturePolymorphic(owner.name)) Right(Some((owner.methods.asScala.find(_.name == name).get, owner.name))) + else if (owner.superName == null) Right(None) + else classNode(owner.superName).flatMap(findInSuperClasses(_, isInterface(owner))) } } - // find the MethodNode in one of the parent classes - def findInParents(parents: List[InternalName], failedClasses: List[ClassNotFound]): Either[List[ClassNotFound], (MethodNode, InternalName)] = parents match { - case x :: xs => methodNodeImpl(x).left.flatMap(failed => findInParents(xs, failed ::: failedClasses)) - case Nil => Left(failedClasses) + def findInInterfaces(initialOwner: ClassNode): Either[ClassNotFound, Option[(MethodNode, InternalName)]] = { + val visited = mutable.Set.empty[InternalName] + val found = mutable.ListBuffer.empty[(MethodNode, ClassNode)] + + def findIn(owner: ClassNode): Option[ClassNotFound] = { + for (i <- owner.interfaces.asScala if !visited(i)) classNode(i) match { + case Left(e) => return Some(e) + case Right(c) => + visited += i + // abstract and static methods are excluded, see jvms-5.4.3.3 + for (m <- findMethod(c) if !isPrivateMethod(m) && !isStaticMethod(m)) found += ((m, c)) + val recusionResult = findIn(c) + if (recusionResult.isDefined) return recusionResult + } + None + } + + findIn(initialOwner) + + val result = + if (found.size <= 1) found.headOption + else { + val maxSpecific = found.filterNot({ + case (method, owner) => + isAbstractMethod(method) || { + val ownerTp = classBTypeFromClassNode(owner) + found exists { + case (other, otherOwner) => + (other ne method) && { + val otherTp = classBTypeFromClassNode(otherOwner) + otherTp.isSubtypeOf(ownerTp).get + } + } + } + }) + // (*) note that if there's no single, non-abstract, maximally-specific method, the jvm + // method resolution (jvms-5.4.3.3) returns any of the non-private, non-static parent + // methods at random (abstract or concrete). + // we chose not to do this here, to prevent the inliner from potentially inlining the + // wrong method. in other words, we guarantee that a concrete method is only returned if + // it resolves deterministically. + // however, there may be multiple abstract methods inherited. in this case we *do* want + // to return a result to allow performing accessibility checks in the inliner. note that + // for accessibility it does not matter which of these methods is return, as they are all + // non-private (i.e., public, protected is not possible, jvms-4.1). + // the remaining case (when there's no max-specific method, but some non-abstract one) + // does not occur in bytecode generated by scalac or javac. we return no result in this + // case. this may at worst prevent some optimizations from happening. + if (maxSpecific.size == 1) maxSpecific.headOption + else if (found.forall(p => isAbstractMethod(p._1))) found.headOption // (*) + else None + } + Right(result.map(p => (p._1, p._2.name))) } // In a MethodInsnNode, the `owner` field may be an array descriptor, for example when invoking `clone`. We don't have a method node to return in this case. - if (ownerInternalNameOrArrayDescriptor.charAt(0) == '[') - Left(MethodNotFound(name, descriptor, ownerInternalNameOrArrayDescriptor, Nil)) - else - methodNodeImpl(ownerInternalNameOrArrayDescriptor).left.map(MethodNotFound(name, descriptor, ownerInternalNameOrArrayDescriptor, _)) + if (ownerInternalNameOrArrayDescriptor.charAt(0) == '[') { + Left(MethodNotFound(name, descriptor, ownerInternalNameOrArrayDescriptor, None)) + } else { + def notFound(cnf: Option[ClassNotFound]) = Left(MethodNotFound(name, descriptor, ownerInternalNameOrArrayDescriptor, cnf)) + val res: Either[ClassNotFound, Option[(MethodNode, InternalName)]] = classNode(ownerInternalNameOrArrayDescriptor).flatMap(c => + findInSuperClasses(c) flatMap { + case None => findInInterfaces(c) + case res => Right(res) + } + ) + res match { + case Left(e) => notFound(Some(e)) + case Right(None) => notFound(None) + case Right(Some(res)) => Right(res) + } + } } private def parseClass(internalName: InternalName): Either[ClassNotFound, ClassNode] = { diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala index 2afc095af617..63906d80e50b 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/BytecodeUtils.scala @@ -17,7 +17,7 @@ import scala.tools.asm.{Label, Type} import scala.tools.asm.Opcodes._ import scala.tools.asm.tree._ import GenBCode._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.analysis.InstructionStackEffect object BytecodeUtils { @@ -99,6 +99,10 @@ object BytecodeUtils { methodNode.name == INSTANCE_CONSTRUCTOR_NAME || methodNode.name == CLASS_CONSTRUCTOR_NAME } + def isPublicMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_PUBLIC) != 0 + + def isPrivateMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_PRIVATE) != 0 + def isStaticMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_STATIC) != 0 def isAbstractMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_ABSTRACT) != 0 @@ -107,10 +111,12 @@ object BytecodeUtils { def isNativeMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_NATIVE) != 0 - def hasCallerSensitiveAnnotation(methodNode: MethodNode) = methodNode.visibleAnnotations != null && methodNode.visibleAnnotations.asScala.exists(_.desc == "Lsun/reflect/CallerSensitive;") + def hasCallerSensitiveAnnotation(methodNode: MethodNode): Boolean = methodNode.visibleAnnotations != null && methodNode.visibleAnnotations.asScala.exists(_.desc == "Lsun/reflect/CallerSensitive;") def isFinalClass(classNode: ClassNode): Boolean = (classNode.access & ACC_FINAL) != 0 + def isInterface(classNode: ClassNode): Boolean = (classNode.access & ACC_INTERFACE) != 0 + def isFinalMethod(methodNode: MethodNode): Boolean = (methodNode.access & (ACC_FINAL | ACC_PRIVATE | ACC_STATIC)) != 0 def isStrictfpMethod(methodNode: MethodNode): Boolean = (methodNode.access & ACC_STRICT) != 0 diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/CallGraph.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/CallGraph.scala index 5f06e1356052..d241acf7b1cc 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/CallGraph.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/CallGraph.scala @@ -12,7 +12,7 @@ import scala.reflect.internal.util.{NoPosition, Position} import scala.tools.asm.{Opcodes, Type, Handle} import scala.tools.asm.tree._ import scala.collection.{concurrent, mutable} -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.BTypes.InternalName import scala.tools.nsc.backend.jvm.BackendReporting._ import scala.tools.nsc.backend.jvm.analysis._ @@ -131,19 +131,19 @@ class CallGraph[BT <: BTypes](val btypes: BT) { (method, declarationClass) <- byteCodeRepository.methodNode(call.owner, call.name, call.desc): Either[OptimizerWarning, (MethodNode, InternalName)] (declarationClassNode, source) <- byteCodeRepository.classNodeAndSource(declarationClass): Either[OptimizerWarning, (ClassNode, Source)] } yield { - val declarationClassBType = classBTypeFromClassNode(declarationClassNode) - val info = analyzeCallsite(method, declarationClassBType, call, source) - import info._ - Callee( - callee = method, - calleeDeclarationClass = declarationClassBType, - safeToInline = safeToInline, - canInlineFromSource = canInlineFromSource, - annotatedInline = annotatedInline, - annotatedNoInline = annotatedNoInline, - samParamTypes = info.samParamTypes, - calleeInfoWarning = warning) - } + val declarationClassBType = classBTypeFromClassNode(declarationClassNode) + val info = analyzeCallsite(method, declarationClassBType, call, source) + import info._ + Callee( + callee = method, + calleeDeclarationClass = declarationClassBType, + safeToInline = safeToInline, + canInlineFromSource = canInlineFromSource, + annotatedInline = annotatedInline, + annotatedNoInline = annotatedNoInline, + samParamTypes = info.samParamTypes, + calleeInfoWarning = warning) + } val argInfos = computeArgInfos(callee, call, prodCons) @@ -388,12 +388,11 @@ class CallGraph[BT <: BTypes](val btypes: BT) { * @param calleeInfoWarning An inliner warning if some information was not available while * gathering the information about this callee. */ - final case class Callee( - callee: MethodNode, calleeDeclarationClass: btypes.ClassBType, - safeToInline: Boolean, canInlineFromSource: Boolean, - annotatedInline: Boolean, annotatedNoInline: Boolean, - samParamTypes: IntMap[btypes.ClassBType], - calleeInfoWarning: Option[CalleeInfoWarning]) { + final case class Callee(callee: MethodNode, calleeDeclarationClass: btypes.ClassBType, + safeToInline: Boolean, canInlineFromSource: Boolean, + annotatedInline: Boolean, annotatedNoInline: Boolean, + samParamTypes: IntMap[btypes.ClassBType], + calleeInfoWarning: Option[CalleeInfoWarning]) { override def toString = s"Callee($calleeDeclarationClass.${callee.name})" } diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/ClosureOptimizer.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/ClosureOptimizer.scala index dcfa9ef70516..93dc40f318c0 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/ClosureOptimizer.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/ClosureOptimizer.scala @@ -18,7 +18,7 @@ import BytecodeUtils._ import BackendReporting._ import Opcodes._ import scala.tools.nsc.backend.jvm.opt.ByteCodeRepository.CompilationUnit -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ class ClosureOptimizer[BT <: BTypes](val btypes: BT) { import btypes._ diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala index d28565b9bc89..4163d62df77f 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/CopyProp.scala @@ -13,7 +13,7 @@ import scala.tools.asm.Type import scala.tools.asm.Opcodes._ import scala.tools.asm.tree._ import scala.collection.mutable -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.BTypes.InternalName import scala.tools.nsc.backend.jvm.analysis._ import scala.tools.nsc.backend.jvm.opt.BytecodeUtils._ diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala index 79d26b0b4eee..5ce7072c60c0 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlineInfoAttribute.scala @@ -51,7 +51,6 @@ case class InlineInfoAttribute(inlineInfo: InlineInfo) extends Attribute(InlineI if (inlineInfo.isEffectivelyFinal) flags |= 1 // flags |= 2 // no longer written if (inlineInfo.sam.isDefined) flags |= 4 - if (inlineInfo.lateInterfaces.nonEmpty) flags |= 8 result.putByte(flags) for (samNameDesc <- inlineInfo.sam) { @@ -79,9 +78,6 @@ case class InlineInfoAttribute(inlineInfo: InlineInfo) extends Attribute(InlineI result.putByte(inlineInfo) } - result.putShort(inlineInfo.lateInterfaces.length) - for (i <- inlineInfo.lateInterfaces) result.putShort(cw.newUTF8(i)) - result } @@ -105,7 +101,6 @@ case class InlineInfoAttribute(inlineInfo: InlineInfo) extends Attribute(InlineI val isFinal = (flags & 1) != 0 val hasSelf = (flags & 2) != 0 val hasSam = (flags & 4) != 0 - val hasLateInterfaces = (flags & 8) != 0 if (hasSelf) nextUTF8() // no longer used @@ -128,13 +123,7 @@ case class InlineInfoAttribute(inlineInfo: InlineInfo) extends Attribute(InlineI (name + desc, MethodInlineInfo(isFinal, isInline, isNoInline)) }).toMap - val lateInterfaces = if (!hasLateInterfaces) Nil else { - val numLateInterfaces = nextShort() - (0 until numLateInterfaces).map(_ => nextUTF8()) - } - val info = InlineInfo(isFinal, sam, infos, None) - info.lateInterfaces ++= lateInterfaces InlineInfoAttribute(info) } else { val msg = UnknownScalaInlineInfoVersion(cr.getClassName, version) @@ -161,8 +150,6 @@ object InlineInfoAttribute { * [u2] name (reference) * [u2] descriptor (reference) * [u1] isFinal (<< 0), traitMethodWithStaticImplementation (<< 1), hasInlineAnnotation (<< 2), hasNoInlineAnnotation (<< 3) - * [u2]? numLateInterfaces - * [u2] lateInterface (reference) */ final val VERSION: Byte = 1 diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala index a0ef74b46a6b..f35eaa45e927 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/Inliner.scala @@ -11,8 +11,7 @@ import scala.annotation.tailrec import scala.tools.asm import asm.Opcodes._ import asm.tree._ -import scala.collection.convert.decorateAsScala._ -import scala.collection.convert.decorateAsJava._ +import scala.collection.JavaConverters._ import AsmUtils._ import BytecodeUtils._ import collection.mutable diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/InlinerHeuristics.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlinerHeuristics.scala index e132ae179212..6aaf9734d381 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/InlinerHeuristics.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/InlinerHeuristics.scala @@ -9,7 +9,7 @@ package opt import scala.tools.asm.tree.MethodNode import scala.tools.nsc.backend.jvm.BTypes.InternalName -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.BackendReporting.OptimizerWarning class InlinerHeuristics[BT <: BTypes](val bTypes: BT) { diff --git a/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala b/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala index f120357c63f7..4e1349257e85 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/opt/LocalOpt.scala @@ -14,7 +14,7 @@ import scala.tools.asm.tree.analysis.Frame import scala.tools.asm.Opcodes._ import scala.tools.asm.tree._ import scala.collection.mutable -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.nsc.backend.jvm.BTypes.InternalName import scala.tools.nsc.backend.jvm.analysis._ import scala.tools.nsc.backend.jvm.opt.BytecodeUtils._ diff --git a/src/compiler/scala/tools/nsc/classpath/AggregateFlatClassPath.scala b/src/compiler/scala/tools/nsc/classpath/AggregateClassPath.scala similarity index 66% rename from src/compiler/scala/tools/nsc/classpath/AggregateFlatClassPath.scala rename to src/compiler/scala/tools/nsc/classpath/AggregateClassPath.scala index f97d97548e2f..6b435542a309 100644 --- a/src/compiler/scala/tools/nsc/classpath/AggregateFlatClassPath.scala +++ b/src/compiler/scala/tools/nsc/classpath/AggregateClassPath.scala @@ -12,15 +12,16 @@ import scala.tools.nsc.util.ClassRepresentation /** * A classpath unifying multiple class- and sourcepath entries. - * Flat classpath can obtain entries for classes and sources independently + * The Classpath can obtain entries for classes and sources independently * so it tries to do operations quite optimally - iterating only these collections * which are needed in the given moment and only as far as it's necessary. + * * @param aggregates classpath instances containing entries which this class processes */ -case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatClassPath { +case class AggregateClassPath(aggregates: Seq[ClassPath]) extends ClassPath { override def findClassFile(className: String): Option[AbstractFile] = { @tailrec - def find(aggregates: Seq[FlatClassPath]): Option[AbstractFile] = + def find(aggregates: Seq[ClassPath]): Option[AbstractFile] = if (aggregates.nonEmpty) { val classFile = aggregates.head.findClassFile(className) if (classFile.isDefined) classFile @@ -30,22 +31,24 @@ case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatCl find(aggregates) } - override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = { - val (pkg, simpleClassName) = PackageNameUtils.separatePkgAndClassNames(className) - + override def findClass(className: String): Option[ClassRepresentation] = { @tailrec - def findEntry[T <: ClassRepClassPathEntry](aggregates: Seq[FlatClassPath], getEntries: FlatClassPath => Seq[T]): Option[T] = + def findEntry(aggregates: Seq[ClassPath], isSource: Boolean): Option[ClassRepresentation] = if (aggregates.nonEmpty) { - val entry = getEntries(aggregates.head).find(_.name == simpleClassName) + val entry = aggregates.head.findClass(className) match { + case s @ Some(_: SourceFileEntry) if isSource => s + case s @ Some(_: ClassFileEntry) if !isSource => s + case _ => None + } if (entry.isDefined) entry - else findEntry(aggregates.tail, getEntries) + else findEntry(aggregates.tail, isSource) } else None - val classEntry = findEntry(aggregates, classesGetter(pkg)) - val sourceEntry = findEntry(aggregates, sourcesGetter(pkg)) + val classEntry = findEntry(aggregates, isSource = false) + val sourceEntry = findEntry(aggregates, isSource = true) (classEntry, sourceEntry) match { - case (Some(c), Some(s)) => Some(ClassAndSourceFilesEntry(c.file, s.file)) + case (Some(c: ClassFileEntry), Some(s: SourceFileEntry)) => Some(ClassAndSourceFilesEntry(c.file, s.file)) case (c @ Some(_), _) => c case (_, s) => s } @@ -63,16 +66,16 @@ case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatCl } override private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] = - getDistinctEntries(classesGetter(inPackage)) + getDistinctEntries(_.classes(inPackage)) override private[nsc] def sources(inPackage: String): Seq[SourceFileEntry] = - getDistinctEntries(sourcesGetter(inPackage)) + getDistinctEntries(_.sources(inPackage)) - override private[nsc] def list(inPackage: String): FlatClassPathEntries = { + override private[nsc] def list(inPackage: String): ClassPathEntries = { val (packages, classesAndSources) = aggregates.map(_.list(inPackage)).unzip val distinctPackages = packages.flatten.distinct val distinctClassesAndSources = mergeClassesAndSources(classesAndSources: _*) - FlatClassPathEntries(distinctPackages, distinctClassesAndSources) + ClassPathEntries(distinctPackages, distinctClassesAndSources) } /** @@ -80,11 +83,11 @@ case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatCl * creates an entry containing both of them. If there would be more than one class or source * entries for the same class it always would use the first entry of each type found on a classpath. */ - private def mergeClassesAndSources(entries: Seq[ClassRepClassPathEntry]*): Seq[ClassRepClassPathEntry] = { + private def mergeClassesAndSources(entries: Seq[ClassRepresentation]*): Seq[ClassRepresentation] = { // based on the implementation from MergedClassPath var count = 0 val indices = collection.mutable.HashMap[String, Int]() - val mergedEntries = new ArrayBuffer[ClassRepClassPathEntry](1024) + val mergedEntries = new ArrayBuffer[ClassRepresentation](1024) for { partOfEntries <- entries @@ -109,7 +112,7 @@ case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatCl mergedEntries.toIndexedSeq } - private def getDistinctEntries[EntryType <: ClassRepClassPathEntry](getEntries: FlatClassPath => Seq[EntryType]): Seq[EntryType] = { + private def getDistinctEntries[EntryType <: ClassRepresentation](getEntries: ClassPath => Seq[EntryType]): Seq[EntryType] = { val seenNames = collection.mutable.HashSet[String]() val entriesBuffer = new ArrayBuffer[EntryType](1024) for { @@ -121,19 +124,16 @@ case class AggregateFlatClassPath(aggregates: Seq[FlatClassPath]) extends FlatCl } entriesBuffer.toIndexedSeq } - - private def classesGetter(pkg: String) = (cp: FlatClassPath) => cp.classes(pkg) - private def sourcesGetter(pkg: String) = (cp: FlatClassPath) => cp.sources(pkg) } -object AggregateFlatClassPath { - def createAggregate(parts: FlatClassPath*): FlatClassPath = { - val elems = new ArrayBuffer[FlatClassPath]() +object AggregateClassPath { + def createAggregate(parts: ClassPath*): ClassPath = { + val elems = new ArrayBuffer[ClassPath]() parts foreach { - case AggregateFlatClassPath(ps) => elems ++= ps + case AggregateClassPath(ps) => elems ++= ps case p => elems += p } if (elems.size == 1) elems.head - else AggregateFlatClassPath(elems.toIndexedSeq) + else AggregateClassPath(elems.toIndexedSeq) } } diff --git a/src/compiler/scala/tools/nsc/classpath/ClassPath.scala b/src/compiler/scala/tools/nsc/classpath/ClassPath.scala new file mode 100644 index 000000000000..08bd98b1d8de --- /dev/null +++ b/src/compiler/scala/tools/nsc/classpath/ClassPath.scala @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2014 Contributor. All rights reserved. + */ +package scala.tools.nsc.classpath + +import scala.reflect.io.AbstractFile +import scala.tools.nsc.util.ClassRepresentation + +case class ClassPathEntries(packages: Seq[PackageEntry], classesAndSources: Seq[ClassRepresentation]) + +object ClassPathEntries { + import scala.language.implicitConversions + // to have working unzip method + implicit def entry2Tuple(entry: ClassPathEntries): (Seq[PackageEntry], Seq[ClassRepresentation]) = (entry.packages, entry.classesAndSources) +} + +trait ClassFileEntry extends ClassRepresentation { + def file: AbstractFile +} + +trait SourceFileEntry extends ClassRepresentation { + def file: AbstractFile +} + +trait PackageEntry { + def name: String +} + +private[nsc] case class ClassFileEntryImpl(file: AbstractFile) extends ClassFileEntry { + override def name = FileUtils.stripClassExtension(file.name) // class name + + override def binary: Option[AbstractFile] = Some(file) + override def source: Option[AbstractFile] = None +} + +private[nsc] case class SourceFileEntryImpl(file: AbstractFile) extends SourceFileEntry { + override def name = FileUtils.stripSourceExtension(file.name) + + override def binary: Option[AbstractFile] = None + override def source: Option[AbstractFile] = Some(file) +} + +private[nsc] case class ClassAndSourceFilesEntry(classFile: AbstractFile, srcFile: AbstractFile) extends ClassRepresentation { + override def name = FileUtils.stripClassExtension(classFile.name) + + override def binary: Option[AbstractFile] = Some(classFile) + override def source: Option[AbstractFile] = Some(srcFile) +} + +private[nsc] case class PackageEntryImpl(name: String) extends PackageEntry + +private[nsc] trait NoSourcePaths { + def asSourcePathString: String = "" + private[nsc] def sources(inPackage: String): Seq[SourceFileEntry] = Seq.empty +} + +private[nsc] trait NoClassPaths { + def findClassFile(className: String): Option[AbstractFile] = None + private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] = Seq.empty +} diff --git a/src/compiler/scala/tools/nsc/classpath/ClassPathFactory.scala b/src/compiler/scala/tools/nsc/classpath/ClassPathFactory.scala index 9bf4e3f77908..3a29f1ba11ef 100644 --- a/src/compiler/scala/tools/nsc/classpath/ClassPathFactory.scala +++ b/src/compiler/scala/tools/nsc/classpath/ClassPathFactory.scala @@ -3,47 +3,49 @@ */ package scala.tools.nsc.classpath -import scala.reflect.io.AbstractFile +import scala.reflect.io.{AbstractFile, VirtualDirectory} +import scala.tools.nsc.Settings +import FileUtils.AbstractFileOps import scala.tools.nsc.util.ClassPath /** - * A trait that contains factory methods for classpath elements of type T. - * - * The logic has been abstracted from ClassPath#ClassPathContext so it's possible - * to have common trait that supports both recursive and flat classpath representations. - * - * Therefore, we expect that T will be either ClassPath[U] or FlatClassPath. + * Provides factory methods for classpath. When creating classpath instances for a given path, + * it uses proper type of classpath depending on a types of particular files containing sources or classes. */ -trait ClassPathFactory[T] { - +class ClassPathFactory(settings: Settings) { /** - * Create a new classpath based on the abstract file. - */ - def newClassPath(file: AbstractFile): T + * Create a new classpath based on the abstract file. + */ + def newClassPath(file: AbstractFile): ClassPath = ClassPathFactory.newClassPath(file, settings) /** - * Creators for sub classpaths which preserve this context. - */ - def sourcesInPath(path: String): List[T] + * Creators for sub classpaths which preserve this context. + */ + def sourcesInPath(path: String): List[ClassPath] = + for { + file <- expandPath(path, expandStar = false) + dir <- Option(AbstractFile getDirectory file) + } yield createSourcePath(dir) + - def expandPath(path: String, expandStar: Boolean = true): List[String] = ClassPath.expandPath(path, expandStar) + def expandPath(path: String, expandStar: Boolean = true): List[String] = scala.tools.nsc.util.ClassPath.expandPath(path, expandStar) - def expandDir(extdir: String): List[String] = ClassPath.expandDir(extdir) + def expandDir(extdir: String): List[String] = scala.tools.nsc.util.ClassPath.expandDir(extdir) - def contentsOfDirsInPath(path: String): List[T] = + def contentsOfDirsInPath(path: String): List[ClassPath] = for { dir <- expandPath(path, expandStar = false) name <- expandDir(dir) entry <- Option(AbstractFile.getDirectory(name)) } yield newClassPath(entry) - def classesInExpandedPath(path: String): IndexedSeq[T] = + def classesInExpandedPath(path: String): IndexedSeq[ClassPath] = classesInPathImpl(path, expand = true).toIndexedSeq def classesInPath(path: String) = classesInPathImpl(path, expand = false) def classesInManifest(useManifestClassPath: Boolean) = - if (useManifestClassPath) ClassPath.manifests.map(url => newClassPath(AbstractFile getResources url)) + if (useManifestClassPath) scala.tools.nsc.util.ClassPath.manifests.map(url => newClassPath(AbstractFile getResources url)) else Nil // Internal @@ -52,4 +54,25 @@ trait ClassPathFactory[T] { file <- expandPath(path, expand) dir <- Option(AbstractFile.getDirectory(file)) } yield newClassPath(dir) + + private def createSourcePath(file: AbstractFile): ClassPath = + if (file.isJarOrZip) + ZipAndJarSourcePathFactory.create(file, settings) + else if (file.isDirectory) + new DirectorySourcePath(file.file) + else + sys.error(s"Unsupported sourcepath element: $file") +} + +object ClassPathFactory { + def newClassPath(file: AbstractFile, settings: Settings): ClassPath = file match { + case vd: VirtualDirectory => VirtualDirectoryClassPath(vd) + case _ => + if (file.isJarOrZip) + ZipAndJarClassPathFactory.create(file, settings) + else if (file.isDirectory) + new DirectoryClassPath(file.file) + else + sys.error(s"Unsupported classpath element: $file") + } } diff --git a/src/compiler/scala/tools/nsc/classpath/DirectoryFlatClassPath.scala b/src/compiler/scala/tools/nsc/classpath/DirectoryClassPath.scala similarity index 85% rename from src/compiler/scala/tools/nsc/classpath/DirectoryFlatClassPath.scala rename to src/compiler/scala/tools/nsc/classpath/DirectoryClassPath.scala index e3964dfa7847..aba941e043e5 100644 --- a/src/compiler/scala/tools/nsc/classpath/DirectoryFlatClassPath.scala +++ b/src/compiler/scala/tools/nsc/classpath/DirectoryClassPath.scala @@ -5,9 +5,8 @@ package scala.tools.nsc.classpath import java.io.File import java.net.URL -import scala.reflect.io.AbstractFile -import scala.reflect.io.PlainFile -import scala.tools.nsc.util.ClassRepresentation +import scala.reflect.io.{AbstractFile, PlainFile} +import scala.tools.nsc.util.{ClassPath, ClassRepresentation} import FileUtils._ /** @@ -17,7 +16,7 @@ import FileUtils._ * when we have a name of a package. * It abstracts over the file representation to work with both JFile and AbstractFile. */ -trait DirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends FlatClassPath { +trait DirectoryLookup[FileEntryType <: ClassRepresentation] extends ClassPath { type F val dir: F @@ -33,7 +32,7 @@ trait DirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends FlatClass protected def isMatchingFile(f: F): Boolean private def getDirectory(forPackage: String): Option[F] = { - if (forPackage == FlatClassPath.RootPackage) { + if (forPackage == ClassPath.RootPackage) { Some(dir) } else { val packageDirName = FileUtils.dirPath(forPackage) @@ -60,7 +59,7 @@ trait DirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends FlatClass files.map(f => createFileEntry(toAbstractFile(f))) } - private[nsc] def list(inPackage: String): FlatClassPathEntries = { + private[nsc] def list(inPackage: String): ClassPathEntries = { val dirForPackage = getDirectory(inPackage) val files: Array[F] = dirForPackage match { case None => emptyFiles @@ -75,11 +74,11 @@ trait DirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends FlatClass else if (isMatchingFile(file)) fileBuf += createFileEntry(toAbstractFile(file)) } - FlatClassPathEntries(packageBuf, fileBuf) + ClassPathEntries(packageBuf, fileBuf) } } -trait JFileDirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends DirectoryLookup[FileEntryType] { +trait JFileDirectoryLookup[FileEntryType <: ClassRepresentation] extends DirectoryLookup[FileEntryType] { type F = File protected def emptyFiles: Array[File] = Array.empty @@ -102,8 +101,8 @@ trait JFileDirectoryLookup[FileEntryType <: ClassRepClassPathEntry] extends Dire def asClassPathStrings: Seq[String] = Seq(dir.getPath) } -case class DirectoryFlatClassPath(dir: File) extends JFileDirectoryLookup[ClassFileEntryImpl] with NoSourcePaths { - override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = findClassFile(className) map ClassFileEntryImpl +case class DirectoryClassPath(dir: File) extends JFileDirectoryLookup[ClassFileEntryImpl] with NoSourcePaths { + override def findClass(className: String): Option[ClassRepresentation] = findClassFile(className) map ClassFileEntryImpl def findClassFile(className: String): Option[AbstractFile] = { val relativePath = FileUtils.dirPath(className) @@ -121,13 +120,13 @@ case class DirectoryFlatClassPath(dir: File) extends JFileDirectoryLookup[ClassF private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] = files(inPackage) } -case class DirectoryFlatSourcePath(dir: File) extends JFileDirectoryLookup[SourceFileEntryImpl] with NoClassPaths { +case class DirectorySourcePath(dir: File) extends JFileDirectoryLookup[SourceFileEntryImpl] with NoClassPaths { def asSourcePathString: String = asClassPathString protected def createFileEntry(file: AbstractFile): SourceFileEntryImpl = SourceFileEntryImpl(file) protected def isMatchingFile(f: File): Boolean = endsScalaOrJava(f.getName) - override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = findSourceFile(className) map SourceFileEntryImpl + override def findClass(className: String): Option[ClassRepresentation] = findSourceFile(className) map SourceFileEntryImpl private def findSourceFile(className: String): Option[AbstractFile] = { val relativePath = FileUtils.dirPath(className) diff --git a/src/compiler/scala/tools/nsc/classpath/FlatClassPath.scala b/src/compiler/scala/tools/nsc/classpath/FlatClassPath.scala deleted file mode 100644 index e95ffe02e378..000000000000 --- a/src/compiler/scala/tools/nsc/classpath/FlatClassPath.scala +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2014 Contributor. All rights reserved. - */ -package scala.tools.nsc.classpath - -import scala.reflect.io.AbstractFile -import scala.tools.nsc.util.{ ClassFileLookup, ClassPath, ClassRepresentation } - -/** - * A base trait for the particular flat classpath representation implementations. - * - * We call this variant of a classpath representation flat because it's possible to - * query the whole classpath using just single instance extending this trait. - * - * This is an alternative design compared to scala.tools.nsc.util.ClassPath - */ -trait FlatClassPath extends ClassFileLookup[AbstractFile] { - /** Empty string represents root package */ - private[nsc] def packages(inPackage: String): Seq[PackageEntry] - private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] - private[nsc] def sources(inPackage: String): Seq[SourceFileEntry] - - /** Allows to get entries for packages and classes merged with sources possibly in one pass. */ - private[nsc] def list(inPackage: String): FlatClassPathEntries - - // A default implementation which should be overridden, if we can create the more efficient - // solution for a given type of FlatClassPath - override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = { - val (pkg, simpleClassName) = PackageNameUtils.separatePkgAndClassNames(className) - - val foundClassFromClassFiles = classes(pkg).find(_.name == simpleClassName) - def findClassInSources = sources(pkg).find(_.name == simpleClassName) - - foundClassFromClassFiles orElse findClassInSources - } - - override def asClassPathString: String = ClassPath.join(asClassPathStrings: _*) - def asClassPathStrings: Seq[String] -} - -object FlatClassPath { - val RootPackage = "" -} - -case class FlatClassPathEntries(packages: Seq[PackageEntry], classesAndSources: Seq[ClassRepClassPathEntry]) - -object FlatClassPathEntries { - import scala.language.implicitConversions - // to have working unzip method - implicit def entry2Tuple(entry: FlatClassPathEntries): (Seq[PackageEntry], Seq[ClassRepClassPathEntry]) = (entry.packages, entry.classesAndSources) -} - -sealed trait ClassRepClassPathEntry extends ClassRepresentation[AbstractFile] - -trait ClassFileEntry extends ClassRepClassPathEntry { - def file: AbstractFile -} - -trait SourceFileEntry extends ClassRepClassPathEntry { - def file: AbstractFile -} - -trait PackageEntry { - def name: String -} - -private[nsc] case class ClassFileEntryImpl(file: AbstractFile) extends ClassFileEntry { - override def name = FileUtils.stripClassExtension(file.name) // class name - - override def binary: Option[AbstractFile] = Some(file) - override def source: Option[AbstractFile] = None -} - -private[nsc] case class SourceFileEntryImpl(file: AbstractFile) extends SourceFileEntry { - override def name = FileUtils.stripSourceExtension(file.name) - - override def binary: Option[AbstractFile] = None - override def source: Option[AbstractFile] = Some(file) -} - -private[nsc] case class ClassAndSourceFilesEntry(classFile: AbstractFile, srcFile: AbstractFile) extends ClassRepClassPathEntry { - override def name = FileUtils.stripClassExtension(classFile.name) - - override def binary: Option[AbstractFile] = Some(classFile) - override def source: Option[AbstractFile] = Some(srcFile) -} - -private[nsc] case class PackageEntryImpl(name: String) extends PackageEntry - -private[nsc] trait NoSourcePaths { - def asSourcePathString: String = "" - private[nsc] def sources(inPackage: String): Seq[SourceFileEntry] = Seq.empty -} - -private[nsc] trait NoClassPaths { - def findClassFile(className: String): Option[AbstractFile] = None - private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] = Seq.empty -} diff --git a/src/compiler/scala/tools/nsc/classpath/FlatClassPathFactory.scala b/src/compiler/scala/tools/nsc/classpath/FlatClassPathFactory.scala deleted file mode 100644 index 463301696e18..000000000000 --- a/src/compiler/scala/tools/nsc/classpath/FlatClassPathFactory.scala +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2014 Contributor. All rights reserved. - */ -package scala.tools.nsc.classpath - -import scala.reflect.io.VirtualDirectory -import scala.tools.nsc.Settings -import scala.tools.nsc.io.AbstractFile -import FileUtils.AbstractFileOps - -/** - * Provides factory methods for flat classpath. When creating classpath instances for a given path, - * it uses proper type of classpath depending on a types of particular files containing sources or classes. - */ -class FlatClassPathFactory(settings: Settings) extends ClassPathFactory[FlatClassPath] { - def newClassPath(file: AbstractFile): FlatClassPath = FlatClassPathFactory.newClassPath(file, settings) - - def sourcesInPath(path: String): List[FlatClassPath] = - for { - file <- expandPath(path, expandStar = false) - dir <- Option(AbstractFile getDirectory file) - } yield createSourcePath(dir) - - private def createSourcePath(file: AbstractFile): FlatClassPath = - if (file.isJarOrZip) - ZipAndJarFlatSourcePathFactory.create(file, settings) - else if (file.isDirectory) - new DirectoryFlatSourcePath(file.file) - else - sys.error(s"Unsupported sourcepath element: $file") -} - -object FlatClassPathFactory { - def newClassPath(file: AbstractFile, settings: Settings): FlatClassPath = file match { - case vd: VirtualDirectory => VirtualDirectoryFlatClassPath(vd) - case _ => - if (file.isJarOrZip) - ZipAndJarFlatClassPathFactory.create(file, settings) - else if (file.isDirectory) - new DirectoryFlatClassPath(file.file) - else - sys.error(s"Unsupported classpath element: $file") - } -} diff --git a/src/compiler/scala/tools/nsc/classpath/PackageNameUtils.scala b/src/compiler/scala/tools/nsc/classpath/PackageNameUtils.scala index c907d565d2c5..39b0d7813559 100644 --- a/src/compiler/scala/tools/nsc/classpath/PackageNameUtils.scala +++ b/src/compiler/scala/tools/nsc/classpath/PackageNameUtils.scala @@ -3,7 +3,7 @@ */ package scala.tools.nsc.classpath -import scala.tools.nsc.classpath.FlatClassPath.RootPackage +import scala.tools.nsc.util.ClassPath.RootPackage /** * Common methods related to package names represented as String diff --git a/src/compiler/scala/tools/nsc/classpath/VirtualDirectoryFlatClassPath.scala b/src/compiler/scala/tools/nsc/classpath/VirtualDirectoryClassPath.scala similarity index 86% rename from src/compiler/scala/tools/nsc/classpath/VirtualDirectoryFlatClassPath.scala rename to src/compiler/scala/tools/nsc/classpath/VirtualDirectoryClassPath.scala index 06cdab583ccf..8df0c3743de7 100644 --- a/src/compiler/scala/tools/nsc/classpath/VirtualDirectoryFlatClassPath.scala +++ b/src/compiler/scala/tools/nsc/classpath/VirtualDirectoryClassPath.scala @@ -4,8 +4,9 @@ import scala.tools.nsc.util.ClassRepresentation import scala.reflect.io.{Path, PlainFile, VirtualDirectory, AbstractFile} import FileUtils._ import java.net.URL +import scala.tools.nsc.util.ClassPath -case class VirtualDirectoryFlatClassPath(dir: VirtualDirectory) extends FlatClassPath with DirectoryLookup[ClassFileEntryImpl] with NoSourcePaths { +case class VirtualDirectoryClassPath(dir: VirtualDirectory) extends ClassPath with DirectoryLookup[ClassFileEntryImpl] with NoSourcePaths { type F = AbstractFile protected def emptyFiles: Array[AbstractFile] = Array.empty @@ -23,7 +24,7 @@ case class VirtualDirectoryFlatClassPath(dir: VirtualDirectory) extends FlatClas def asURLs: Seq[URL] = Seq(new URL(dir.name)) def asClassPathStrings: Seq[String] = Seq(dir.path) - override def findClass(className: String): Option[ClassRepresentation[AbstractFile]] = findClassFile(className) map ClassFileEntryImpl + override def findClass(className: String): Option[ClassRepresentation] = findClassFile(className) map ClassFileEntryImpl def findClassFile(className: String): Option[AbstractFile] = { val relativePath = FileUtils.dirPath(className) diff --git a/src/compiler/scala/tools/nsc/classpath/ZipAndJarFileLookupFactory.scala b/src/compiler/scala/tools/nsc/classpath/ZipAndJarFileLookupFactory.scala index 6ec3805d8b2a..fe74e5f8747b 100644 --- a/src/compiler/scala/tools/nsc/classpath/ZipAndJarFileLookupFactory.scala +++ b/src/compiler/scala/tools/nsc/classpath/ZipAndJarFileLookupFactory.scala @@ -6,7 +6,8 @@ package scala.tools.nsc.classpath import java.io.File import java.net.URL import scala.annotation.tailrec -import scala.reflect.io.{ AbstractFile, FileZipArchive, ManifestResources } +import scala.reflect.io.{AbstractFile, FileZipArchive, ManifestResources} +import scala.tools.nsc.util.ClassPath import scala.tools.nsc.Settings import FileUtils._ @@ -19,16 +20,16 @@ import FileUtils._ * when there are a lot of projects having a lot of common dependencies. */ sealed trait ZipAndJarFileLookupFactory { - private val cache = collection.mutable.Map.empty[AbstractFile, FlatClassPath] + private val cache = collection.mutable.Map.empty[AbstractFile, ClassPath] - def create(zipFile: AbstractFile, settings: Settings): FlatClassPath = { + def create(zipFile: AbstractFile, settings: Settings): ClassPath = { if (settings.YdisableFlatCpCaching) createForZipFile(zipFile) else createUsingCache(zipFile, settings) } - protected def createForZipFile(zipFile: AbstractFile): FlatClassPath + protected def createForZipFile(zipFile: AbstractFile): ClassPath - private def createUsingCache(zipFile: AbstractFile, settings: Settings): FlatClassPath = cache.synchronized { + private def createUsingCache(zipFile: AbstractFile, settings: Settings): ClassPath = cache.synchronized { def newClassPathInstance = { if (settings.verbose || settings.Ylogcp) println(s"$zipFile is not yet in the classpath cache") @@ -39,11 +40,11 @@ sealed trait ZipAndJarFileLookupFactory { } /** - * Manages creation of flat classpath for class files placed in zip and jar files. + * Manages creation of classpath for class files placed in zip and jar files. * It should be the only way of creating them as it provides caching. */ -object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { - private case class ZipArchiveFlatClassPath(zipFile: File) +object ZipAndJarClassPathFactory extends ZipAndJarFileLookupFactory { + private case class ZipArchiveClassPath(zipFile: File) extends ZipArchiveFileLookup[ClassFileEntryImpl] with NoSourcePaths { @@ -65,7 +66,7 @@ object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { * with a particularly prepared scala-library.jar. It should have all classes listed in the manifest like e.g. this entry: * Name: scala/Function2$mcFJD$sp.class */ - private case class ManifestResourcesFlatClassPath(file: ManifestResources) extends FlatClassPath with NoSourcePaths { + private case class ManifestResourcesClassPath(file: ManifestResources) extends ClassPath with NoSourcePaths { override def findClassFile(className: String): Option[AbstractFile] = { val (pkg, simpleClassName) = PackageNameUtils.separatePkgAndClassNames(className) classes(pkg).find(_.name == simpleClassName).map(_.file) @@ -75,8 +76,8 @@ object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { override def asURLs: Seq[URL] = file.toURLs() - import ManifestResourcesFlatClassPath.PackageFileInfo - import ManifestResourcesFlatClassPath.PackageInfo + import ManifestResourcesClassPath.PackageFileInfo + import ManifestResourcesClassPath.PackageInfo /** * A cache mapping package name to abstract file for package directory and subpackages of given package. @@ -114,8 +115,8 @@ object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { } val subpackages = getSubpackages(file) - packages.put(FlatClassPath.RootPackage, PackageFileInfo(file, subpackages)) - traverse(FlatClassPath.RootPackage, subpackages, collection.mutable.Queue()) + packages.put(ClassPath.RootPackage, PackageFileInfo(file, subpackages)) + traverse(ClassPath.RootPackage, subpackages, collection.mutable.Queue()) packages } @@ -132,21 +133,21 @@ object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { (for (file <- pkg if file.isClass) yield ClassFileEntryImpl(file))(collection.breakOut) } - override private[nsc] def list(inPackage: String): FlatClassPathEntries = FlatClassPathEntries(packages(inPackage), classes(inPackage)) + override private[nsc] def list(inPackage: String): ClassPathEntries = ClassPathEntries(packages(inPackage), classes(inPackage)) } - private object ManifestResourcesFlatClassPath { + private object ManifestResourcesClassPath { case class PackageFileInfo(packageFile: AbstractFile, subpackages: Seq[AbstractFile]) case class PackageInfo(packageName: String, subpackages: List[AbstractFile]) } - override protected def createForZipFile(zipFile: AbstractFile): FlatClassPath = + override protected def createForZipFile(zipFile: AbstractFile): ClassPath = if (zipFile.file == null) createWithoutUnderlyingFile(zipFile) - else ZipArchiveFlatClassPath(zipFile.file) + else ZipArchiveClassPath(zipFile.file) private def createWithoutUnderlyingFile(zipFile: AbstractFile) = zipFile match { case manifestRes: ManifestResources => - ManifestResourcesFlatClassPath(manifestRes) + ManifestResourcesClassPath(manifestRes) case _ => val errorMsg = s"Abstract files which don't have an underlying file and are not ManifestResources are not supported. There was $zipFile" throw new IllegalArgumentException(errorMsg) @@ -154,11 +155,11 @@ object ZipAndJarFlatClassPathFactory extends ZipAndJarFileLookupFactory { } /** - * Manages creation of flat classpath for source files placed in zip and jar files. + * Manages creation of classpath for source files placed in zip and jar files. * It should be the only way of creating them as it provides caching. */ -object ZipAndJarFlatSourcePathFactory extends ZipAndJarFileLookupFactory { - private case class ZipArchiveFlatSourcePath(zipFile: File) +object ZipAndJarSourcePathFactory extends ZipAndJarFileLookupFactory { + private case class ZipArchiveSourcePath(zipFile: File) extends ZipArchiveFileLookup[SourceFileEntryImpl] with NoClassPaths { @@ -170,5 +171,5 @@ object ZipAndJarFlatSourcePathFactory extends ZipAndJarFileLookupFactory { override protected def isRequiredFileType(file: AbstractFile): Boolean = file.isScalaOrJavaSource } - override protected def createForZipFile(zipFile: AbstractFile): FlatClassPath = ZipArchiveFlatSourcePath(zipFile.file) + override protected def createForZipFile(zipFile: AbstractFile): ClassPath = ZipArchiveSourcePath(zipFile.file) } diff --git a/src/compiler/scala/tools/nsc/classpath/ZipArchiveFileLookup.scala b/src/compiler/scala/tools/nsc/classpath/ZipArchiveFileLookup.scala index a24d989306a1..9c147cf8cc69 100644 --- a/src/compiler/scala/tools/nsc/classpath/ZipArchiveFileLookup.scala +++ b/src/compiler/scala/tools/nsc/classpath/ZipArchiveFileLookup.scala @@ -9,13 +9,14 @@ import scala.collection.Seq import scala.reflect.io.AbstractFile import scala.reflect.io.FileZipArchive import FileUtils.AbstractFileOps +import scala.tools.nsc.util.{ClassPath, ClassRepresentation} /** * A trait allowing to look for classpath entries of given type in zip and jar files. * It provides common logic for classes handling class and source files. * It's aware of things like e.g. META-INF directory which is correctly skipped. */ -trait ZipArchiveFileLookup[FileEntryType <: ClassRepClassPathEntry] extends FlatClassPath { +trait ZipArchiveFileLookup[FileEntryType <: ClassRepresentation] extends ClassPath { val zipFile: File assert(zipFile != null, "Zip file in ZipArchiveFileLookup cannot be null") @@ -39,7 +40,7 @@ trait ZipArchiveFileLookup[FileEntryType <: ClassRepClassPathEntry] extends Flat entry <- dirEntry.iterator if isRequiredFileType(entry) } yield createFileEntry(entry) - override private[nsc] def list(inPackage: String): FlatClassPathEntries = { + override private[nsc] def list(inPackage: String): ClassPathEntries = { val foundDirEntry = findDirEntry(inPackage) foundDirEntry map { dirEntry => @@ -53,8 +54,8 @@ trait ZipArchiveFileLookup[FileEntryType <: ClassRepClassPathEntry] extends Flat else if (isRequiredFileType(entry)) fileBuf += createFileEntry(entry) } - FlatClassPathEntries(pkgBuf, fileBuf) - } getOrElse FlatClassPathEntries(Seq.empty, Seq.empty) + ClassPathEntries(pkgBuf, fileBuf) + } getOrElse ClassPathEntries(Seq.empty, Seq.empty) } private def findDirEntry(pkg: String): Option[archive.DirEntry] = { diff --git a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala index 7b9801175955..9a0d86a94df1 100644 --- a/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala +++ b/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala @@ -200,7 +200,6 @@ trait ScalaSettings extends AbsScalaSettings val YmethodInfer = BooleanSetting ("-Yinfer-argument-types", "Infer types for arguments of overridden methods.") val etaExpandKeepsStar = BooleanSetting ("-Yeta-expand-keeps-star", "Eta-expand varargs methods to T* rather than Seq[T]. This is a temporary option to ease transition.").withDeprecationMessage(removalIn212) val inferByName = BooleanSetting ("-Yinfer-by-name", "Allow inference of by-name types. This is a temporary option to ease transition. See SI-7899.").withDeprecationMessage(removalIn212) - val YclasspathImpl = ChoiceSetting ("-YclasspathImpl", "implementation", "Choose classpath scanning method.", List(ClassPathRepresentationType.Recursive, ClassPathRepresentationType.Flat), ClassPathRepresentationType.Flat) val YdisableFlatCpCaching = BooleanSetting ("-YdisableFlatCpCaching", "Do not cache flat classpath representation of classpath elements from jars across compiler instances.") val exposeEmptyPackage = BooleanSetting ("-Yexpose-empty-package", "Internal only: expose the empty package.").internalOnly() @@ -389,8 +388,3 @@ trait ScalaSettings extends AbsScalaSettings None } } - -object ClassPathRepresentationType { - val Flat = "flat" - val Recursive = "recursive" -} diff --git a/src/compiler/scala/tools/nsc/settings/Warnings.scala b/src/compiler/scala/tools/nsc/settings/Warnings.scala index 79795bcc17a0..7ef606b6ef83 100644 --- a/src/compiler/scala/tools/nsc/settings/Warnings.scala +++ b/src/compiler/scala/tools/nsc/settings/Warnings.scala @@ -25,6 +25,8 @@ trait Warnings { // currently considered too noisy for general use val warnUnusedImport = BooleanSetting("-Ywarn-unused-import", "Warn when imports are unused.") + val nowarnDefaultJunitMethods = BooleanSetting("-Ynowarn-default-junit-methods", "Don't warn when a JUnit @Test method is generated as a default method (not supported in JUnit 4).") + // Experimental lint warnings that are turned off, but which could be turned on programmatically. // They are not activated by -Xlint and can't be enabled on the command line because they are not // created using the standard factory methods. @@ -57,6 +59,7 @@ trait Warnings { val PackageObjectClasses = LintWarning("package-object-classes", "Class or object defined in package object.") val UnsoundMatch = LintWarning("unsound-match", "Pattern match may not be typesafe.") val StarsAlign = LintWarning("stars-align", "Pattern sequence wildcard must align with sequence component.") + val Constant = LintWarning("constant", "Evaluation of a constant arithmetic expression results in an error.") def allLintWarnings = values.toSeq.asInstanceOf[Seq[LintWarning]] } @@ -78,6 +81,7 @@ trait Warnings { def warnPackageObjectClasses = lint contains PackageObjectClasses def warnUnsoundMatch = lint contains UnsoundMatch def warnStarsAlign = lint contains StarsAlign + def warnConstant = lint contains Constant // Lint warnings that are currently -Y, but deprecated in that usage @deprecated("Use warnAdaptedArgs", since="2.11.2") diff --git a/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala b/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala index 4f5589fd7c72..b36d5d4ef144 100644 --- a/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala +++ b/src/compiler/scala/tools/nsc/symtab/SymbolLoaders.scala @@ -10,10 +10,8 @@ import classfile.ClassfileParser import java.io.IOException import scala.reflect.internal.MissingRequirementError import scala.reflect.internal.util.Statistics -import scala.reflect.io.{ AbstractFile, NoAbstractFile } -import scala.tools.nsc.classpath.FlatClassPath -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.nsc.util.{ ClassPath, ClassRepresentation } +import scala.reflect.io.{AbstractFile, NoAbstractFile} +import scala.tools.nsc.util.{ClassPath, ClassRepresentation} /** This class ... * @@ -154,7 +152,7 @@ abstract class SymbolLoaders { /** Initialize toplevel class and module symbols in `owner` from class path representation `classRep` */ - def initializeFromClassPath(owner: Symbol, classRep: ClassRepresentation[AbstractFile]) { + def initializeFromClassPath(owner: Symbol, classRep: ClassRepresentation) { ((classRep.binary, classRep.source) : @unchecked) match { case (Some(bin), Some(src)) if platform.needCompile(bin, src) && !binaryOnly(owner, classRep.name) => @@ -246,42 +244,12 @@ abstract class SymbolLoaders { resPhase } - /** - * Load contents of a package - */ - class PackageLoader(classpath: ClassPath[AbstractFile]) extends SymbolLoader with FlagAgnosticCompleter { - protected def description = s"package loader ${classpath.name}" - - protected def doComplete(root: Symbol) { - assert(root.isPackageClass, root) - // Time travel to a phase before refchecks avoids an initialization issue. `openPackageModule` - // creates a module symbol and invokes invokes `companionModule` while the `infos` field is - // still null. This calls `isModuleNotMethod`, which forces the `info` if run after refchecks. - enteringPhase(phaseBeforeRefchecks) { - root.setInfo(new PackageClassInfoType(newScope, root)) - - if (!root.isRoot) { - for (classRep <- classpath.classes) { - initializeFromClassPath(root, classRep) - } - } - if (!root.isEmptyPackageClass) { - for (pkg <- classpath.packages) { - enterPackage(root, pkg.name, new PackageLoader(pkg)) - } - - openPackageModule(root) - } - } - } - } - /** * Loads contents of a package */ - class PackageLoaderUsingFlatClassPath(packageName: String, classPath: FlatClassPath) extends SymbolLoader with FlagAgnosticCompleter { + class PackageLoader(packageName: String, classPath: ClassPath) extends SymbolLoader with FlagAgnosticCompleter { protected def description = { - val shownPackageName = if (packageName == FlatClassPath.RootPackage) "" else packageName + val shownPackageName = if (packageName == ClassPath.RootPackage) "" else packageName s"package loader $shownPackageName" } @@ -298,9 +266,9 @@ abstract class SymbolLoaders { val fullName = pkg.name val name = - if (packageName == FlatClassPath.RootPackage) fullName + if (packageName == ClassPath.RootPackage) fullName else fullName.substring(packageName.length + 1) - val packageLoader = new PackageLoaderUsingFlatClassPath(fullName, classPath) + val packageLoader = new PackageLoader(fullName, classPath) enterPackage(root, name, packageLoader) } @@ -329,10 +297,7 @@ abstract class SymbolLoaders { val loaders = SymbolLoaders.this.asInstanceOf[SymbolLoadersRefined] - override def classFileLookup: util.ClassFileLookup[AbstractFile] = settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Recursive => platform.classPath - case ClassPathRepresentationType.Flat => platform.flatClassPath - } + override def classPath: ClassPath = platform.classPath } protected def description = "class file "+ classfile.toString diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala index fffd48d145be..0533d420cdb4 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala @@ -8,16 +8,16 @@ package tools.nsc package symtab package classfile -import java.io.{ File, IOException } +import java.io.{File, IOException} import java.lang.Integer.toHexString -import scala.collection.{ mutable, immutable } -import scala.collection.mutable.{ ListBuffer, ArrayBuffer } +import scala.collection.{immutable, mutable} +import scala.collection.mutable.{ArrayBuffer, ListBuffer} import scala.annotation.switch -import scala.reflect.internal.{ JavaAccFlags } -import scala.reflect.internal.pickling.{PickleBuffer, ByteCodecs} +import scala.reflect.internal.JavaAccFlags +import scala.reflect.internal.pickling.{ByteCodecs, PickleBuffer} import scala.reflect.io.NoAbstractFile +import scala.tools.nsc.util.ClassPath import scala.tools.nsc.io.AbstractFile -import scala.tools.nsc.util.ClassFileLookup /** This abstract class implements a class file parser. * @@ -43,8 +43,8 @@ abstract class ClassfileParser { */ protected def lookupMemberAtTyperPhaseIfPossible(sym: Symbol, name: Name): Symbol - /** The way of the class file lookup used by the compiler. */ - def classFileLookup: ClassFileLookup[AbstractFile] + /** The compiler classpath. */ + def classPath: ClassPath import definitions._ import scala.reflect.internal.ClassfileConstants._ @@ -357,7 +357,7 @@ abstract class ClassfileParser { } private def loadClassSymbol(name: Name): Symbol = { - val file = classFileLookup findClassFile name.toString getOrElse { + val file = classPath findClassFile name.toString getOrElse { // SI-5593 Scaladoc's current strategy is to visit all packages in search of user code that can be documented // therefore, it will rummage through the classpath triggering errors whenever it encounters package objects // that are not in their correct place (see bug for details) @@ -1079,7 +1079,7 @@ abstract class ClassfileParser { for (entry <- innerClasses.entries) { // create a new class member for immediate inner classes if (entry.outerName == currentClass) { - val file = classFileLookup.findClassFile(entry.externalName.toString) + val file = classPath.findClassFile(entry.externalName.toString) enterClassAndModule(entry, file.getOrElse(NoAbstractFile)) } } diff --git a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala index d350ca8e1757..1dfc1330c6a5 100644 --- a/src/compiler/scala/tools/nsc/transform/Delambdafy.scala +++ b/src/compiler/scala/tools/nsc/transform/Delambdafy.scala @@ -61,6 +61,9 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre private def mkLambdaMetaFactoryCall(fun: Function, target: Symbol, functionalInterface: Symbol, samUserDefined: Symbol, isSpecialized: Boolean): Tree = { val pos = fun.pos + def isSelfParam(p: Symbol) = p.isSynthetic && p.name == nme.SELF + val hasSelfParam = isSelfParam(target.firstParam) + val allCapturedArgRefs = { // find which variables are free in the lambda because those are captures that need to be // passed into the constructor of the anonymous function class @@ -68,7 +71,8 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre gen.mkAttributedRef(capture) setPos pos ).toList - if (target hasFlag STATIC) captureArgs // no `this` reference needed + if (!hasSelfParam) captureArgs.filterNot(arg => isSelfParam(arg.symbol)) + else if (currentMethod.hasFlag(Flags.STATIC)) captureArgs else (gen.mkAttributedThis(fun.symbol.enclClass) setPos pos) :: captureArgs } @@ -179,7 +183,7 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre val numCaptures = targetParams.length - functionParamTypes.length val (targetCapturedParams, targetFunctionParams) = targetParams.splitAt(numCaptures) - val methSym = oldClass.newMethod(target.name.append("$adapted").toTermName, target.pos, target.flags | FINAL | ARTIFACT) + val methSym = oldClass.newMethod(target.name.append("$adapted").toTermName, target.pos, target.flags | FINAL | ARTIFACT | STATIC) val bridgeCapturedParams = targetCapturedParams.map(param => methSym.newSyntheticValueParam(param.tpe, param.name.toTermName)) val bridgeFunctionParams = map2(targetFunctionParams, bridgeParamTypes)((param, tp) => methSym.newSyntheticValueParam(tp, param.name.toTermName)) @@ -223,10 +227,8 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre private def transformFunction(originalFunction: Function): Tree = { val target = targetMethod(originalFunction) - target.makeNotPrivate(target.owner) - - // must be done before calling createBoxingBridgeMethod and mkLambdaMetaFactoryCall - if (!(target hasFlag STATIC) && !methodReferencesThis(target)) target setFlag STATIC + assert(target.hasFlag(Flags.STATIC)) + target.setFlag(notPRIVATE) val funSym = originalFunction.tpe.typeSymbolDirect // The functional interface that can be used to adapt the lambda target method `target` to the given function type. @@ -252,11 +254,22 @@ abstract class Delambdafy extends Transform with TypingTransformers with ast.Tre // here's the main entry point of the transform override def transform(tree: Tree): Tree = tree match { // the main thing we care about is lambdas - case fun: Function => super.transform(transformFunction(fun)) + case fun: Function => + super.transform(transformFunction(fun)) case Template(_, _, _) => + def pretransform(tree: Tree): Tree = tree match { + case dd: DefDef if dd.symbol.isDelambdafyTarget => + if (!dd.symbol.hasFlag(STATIC) && methodReferencesThis(dd.symbol)) { + gen.mkStatic(dd, sym => sym) + } else { + dd.symbol.setFlag(STATIC) + dd + } + case t => t + } try { // during this call boxingBridgeMethods will be populated from the Function case - val Template(parents, self, body) = super.transform(tree) + val Template(parents, self, body) = super.transform(deriveTemplate(tree)(_.mapConserve(pretransform))) Template(parents, self, body ++ boxingBridgeMethods) } finally boxingBridgeMethods.clear() case _ => super.transform(tree) diff --git a/src/compiler/scala/tools/nsc/transform/Erasure.scala b/src/compiler/scala/tools/nsc/transform/Erasure.scala index 0301e06c87a3..f49e5dbc1b5c 100644 --- a/src/compiler/scala/tools/nsc/transform/Erasure.scala +++ b/src/compiler/scala/tools/nsc/transform/Erasure.scala @@ -187,26 +187,6 @@ abstract class Erasure extends AddInterfaces private def isErasedValueType(tpe: Type) = tpe.isInstanceOf[ErasedValueType] - /* Drop redundant types (ones which are implemented by some other parent) from the immediate parents. - * This is important on Android because there is otherwise an interface explosion. - */ - def minimizeParents(parents: List[Type]): List[Type] = if (parents.isEmpty) parents else { - def isInterfaceOrTrait(sym: Symbol) = sym.isInterface || sym.isTrait - - var rest = parents.tail - var leaves = collection.mutable.ListBuffer.empty[Type] += parents.head - while(rest.nonEmpty) { - val candidate = rest.head - val nonLeaf = leaves exists { t => t.typeSymbol isSubClass candidate.typeSymbol } - if(!nonLeaf) { - leaves = leaves filterNot { t => isInterfaceOrTrait(t.typeSymbol) && (candidate.typeSymbol isSubClass t.typeSymbol) } - leaves += candidate - } - rest = rest.tail - } - leaves.toList - } - /** The Java signature of type 'info', for symbol sym. The symbol is used to give the right return * type for constructors. @@ -224,12 +204,11 @@ abstract class Erasure extends AddInterfaces case _ => tps } - val minParents = minimizeParents(parents) val validParents = if (isTraitSignature) // java is unthrilled about seeing interfaces inherit from classes - minParents filter (p => isInterfaceOrTrait(p.typeSymbol)) - else minParents + parents filter (p => isInterfaceOrTrait(p.typeSymbol)) + else parents val ps = ensureClassAsFirstParent(validParents) diff --git a/src/compiler/scala/tools/nsc/transform/Mixin.scala b/src/compiler/scala/tools/nsc/transform/Mixin.scala index 03935c3d6723..19ba9345faea 100644 --- a/src/compiler/scala/tools/nsc/transform/Mixin.scala +++ b/src/compiler/scala/tools/nsc/transform/Mixin.scala @@ -8,6 +8,7 @@ package transform import symtab._ import Flags._ +import scala.annotation.tailrec import scala.collection.mutable abstract class Mixin extends InfoTransform with ast.TreeDSL { @@ -241,22 +242,16 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { case NoSymbol => val isMemberOfClazz = clazz.info.findMember(member.name, 0, 0L, stableOnly = false).alternatives.contains(member) if (isMemberOfClazz) { - val competingMethods = clazz.baseClasses.iterator - .filter(_ ne mixinClass) - .map(member.overriddenSymbol) - .filter(_.exists) - .toList - - // `member` is a concrete `method` defined in `mixinClass`, which is a base class of + // `member` is a concrete method defined in `mixinClass`, which is a base class of // `clazz`, and the method is not overridden in `clazz`. A forwarder is needed if: // - // - A non-trait base class defines matching method. Example: + // - A non-trait base class of `clazz` defines a matching method. Example: // class C {def f: Int}; trait T extends C {def f = 1}; class D extends T // Even if C.f is abstract, the forwarder in D is needed, otherwise the JVM would // resolve `D.f` to `C.f`, see jvms-6.5.invokevirtual. // - // - There exists another concrete, matching method in any of the base classes, and - // the `mixinClass` does not itself extend that base class. In this case the + // - There exists another concrete, matching method in a parent interface `p` of + // `clazz`, and the `mixinClass` does not itself extend `p`. In this case the // forwarder is needed to disambiguate. Example: // trait T1 {def f = 1}; trait T2 extends T1 {override def f = 2}; class C extends T2 // In C we don't need a forwarder for f because T2 extends T1, so the JVM resolves @@ -265,13 +260,25 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { // In D the forwarder is needed, the interfaces U1 and U2 are unrelated at the JVM // level. - lazy val mixinSuperInterfaces = mixinClass.ancestors.filter(_.isTraitOrInterface) - val needsForwarder = competingMethods.exists(m => { - !m.owner.isTraitOrInterface || - (!m.isDeferred && !mixinSuperInterfaces.contains(m.owner)) - }) - if (needsForwarder) + @tailrec + def existsCompetingMethod(baseClasses: List[Symbol]): Boolean = baseClasses match { + case baseClass :: rest => + if (baseClass ne mixinClass) { + val m = member.overriddenSymbol(baseClass) + val isCompeting = m.exists && { + !m.owner.isTraitOrInterface || + (!m.isDeferred && !mixinClass.isNonBottomSubClass(m.owner)) + } + isCompeting || existsCompetingMethod(rest) + } else existsCompetingMethod(rest) + + case _ => false + } + + if (existsCompetingMethod(clazz.baseClasses)) cloneAndAddMixinMember(mixinClass, member).asInstanceOf[TermSymbol] setAlias member + else if (!settings.nowarnDefaultJunitMethods && JUnitTestClass.exists && member.hasAnnotation(JUnitTestClass)) + warning(member.pos, "JUnit tests in traits that are compiled as default methods are not executed by JUnit 4. JUnit 5 will fix this issue.") } case _ => diff --git a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala index 2f4771e9d47e..2cd4785fbf0e 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala @@ -40,9 +40,10 @@ abstract class ConstantFolder { if ((x ne null) && x.tag != UnitTag) tree setType ConstantType(x) else tree } catch { - case _: ArithmeticException => tree // the code will crash at runtime, - // but that is better than the - // compiler itself crashing + case e: ArithmeticException => + if (settings.warnConstant) + warning(tree.pos, s"Evaluation of a constant expression results in an arithmetic error: ${e.getMessage}") + tree } private def foldUnop(op: Name, x: Constant): Constant = (op, x.tag) match { @@ -102,13 +103,13 @@ abstract class ConstantFolder { case nme.XOR => Constant(x.longValue ^ y.longValue) case nme.AND => Constant(x.longValue & y.longValue) case nme.LSL if x.tag <= IntTag - => Constant(x.intValue << y.longValue) + => Constant(x.intValue << y.longValue.toInt) // TODO: remove .toInt once starr includes the fix for SI-9516 (2.12.0-M5) case nme.LSL => Constant(x.longValue << y.longValue) case nme.LSR if x.tag <= IntTag - => Constant(x.intValue >>> y.longValue) + => Constant(x.intValue >>> y.longValue.toInt) // TODO: remove .toInt once starr includes the fix for SI-9516 (2.12.0-M5) case nme.LSR => Constant(x.longValue >>> y.longValue) case nme.ASR if x.tag <= IntTag - => Constant(x.intValue >> y.longValue) + => Constant(x.intValue >> y.longValue.toInt) // TODO: remove .toInt once starr includes the fix for SI-9516 (2.12.0-M5) case nme.ASR => Constant(x.longValue >> y.longValue) case nme.EQ => Constant(x.longValue == y.longValue) case nme.NE => Constant(x.longValue != y.longValue) @@ -158,7 +159,7 @@ abstract class ConstantFolder { else if (x.isNumeric && y.isNumeric) math.max(x.tag, y.tag) else NoTag - try optag match { + optag match { case BooleanTag => foldBooleanOp(op, x, y) case ByteTag | ShortTag | CharTag | IntTag => foldSubrangeOp(op, x, y) case LongTag => foldLongOp(op, x, y) @@ -167,8 +168,5 @@ abstract class ConstantFolder { case StringTag if op == nme.ADD => Constant(x.stringValue + y.stringValue) case _ => null } - catch { - case _: ArithmeticException => null - } } } diff --git a/src/compiler/scala/tools/nsc/util/ClassFileLookup.scala b/src/compiler/scala/tools/nsc/util/ClassFileLookup.scala deleted file mode 100644 index 5d8831a607be..000000000000 --- a/src/compiler/scala/tools/nsc/util/ClassFileLookup.scala +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) 2014 Contributor. All rights reserved. - */ -package scala.tools.nsc.util - -import scala.tools.nsc.Settings -import scala.tools.nsc.classpath.{AggregateFlatClassPath, FlatClassPath, FlatClassPathFactory} -import scala.tools.nsc.io.AbstractFile -import java.net.URL - -/** - * Simple interface that allows us to abstract over how class file lookup is performed - * in different classpath representations. - */ -// TODO at the end, after the possible removal of the old classpath representation, this class shouldn't be generic -// T should be just changed to AbstractFile -trait ClassFileLookup[T] { - def findClassFile(name: String): Option[AbstractFile] - - /** - * It returns both classes from class file and source files (as our base ClassRepresentation). - * So note that it's not so strictly related to findClassFile. - */ - def findClass(name: String): Option[ClassRepresentation[T]] - - /** - * A sequence of URLs representing this classpath. - */ - def asURLs: Seq[URL] - - /** The whole classpath in the form of one String. - */ - def asClassPathString: String - - // for compatibility purposes - @deprecated("Use asClassPathString instead of this one", "2.11.5") - def asClasspathString: String = asClassPathString - - /** The whole sourcepath in the form of one String. - */ - def asSourcePathString: String -} - -object ClassFileLookup { - def createForFile(f: AbstractFile, current: ClassFileLookup[AbstractFile], settings: Settings): ClassFileLookup[AbstractFile] = current match { - case cp: ClassPath[_] => cp.context.newClassPath(f) - case _: FlatClassPath => FlatClassPathFactory.newClassPath(f, settings) - } - - def createAggregate(elems: Iterable[ClassFileLookup[AbstractFile]], current: ClassFileLookup[AbstractFile]): ClassFileLookup[AbstractFile] = { - assert(elems.nonEmpty) - if (elems.size == 1) elems.head - else current match { - case cp: ClassPath[_] => - new MergedClassPath(elems.asInstanceOf[Iterable[ClassPath[AbstractFile]]], cp.context) - - case _: FlatClassPath => - AggregateFlatClassPath.createAggregate(elems.asInstanceOf[Iterable[FlatClassPath]].toSeq : _*) - } - } -} - -/** - * Represents classes which can be loaded with a ClassfileLoader and/or SourcefileLoader. - */ -// TODO at the end, after the possible removal of the old classpath implementation, this class shouldn't be generic -// T should be just changed to AbstractFile -trait ClassRepresentation[T] { - def binary: Option[T] - def source: Option[AbstractFile] - - def name: String -} - -object ClassRepresentation { - def unapply[T](classRep: ClassRepresentation[T]): Option[(Option[T], Option[AbstractFile])] = - Some((classRep.binary, classRep.source)) -} diff --git a/src/compiler/scala/tools/nsc/util/ClassPath.scala b/src/compiler/scala/tools/nsc/util/ClassPath.scala index 6cdc3856cd60..cef2fc4bbf50 100644 --- a/src/compiler/scala/tools/nsc/util/ClassPath.scala +++ b/src/compiler/scala/tools/nsc/util/ClassPath.scala @@ -7,28 +7,61 @@ package scala.tools.nsc package util -import io.{ AbstractFile, Directory, File, Jar } +import io.{AbstractFile, Directory, File, Jar} import java.net.MalformedURLException import java.net.URL import java.util.regex.PatternSyntaxException -import scala.collection.{ mutable, immutable } -import scala.reflect.internal.util.StringOps.splitWhere -import scala.tools.nsc.classpath.FileUtils import File.pathSeparator -import FileUtils.endsClass -import FileUtils.endsScalaOrJava import Jar.isJarOrZip -/**

- * This module provides star expansion of '-classpath' option arguments, behaves the same as - * java, see [[http://docs.oracle.com/javase/6/docs/technotes/tools/windows/classpath.html]] - *

- * - * @author Stepan Koltsov - */ +/** + * A representation of the compiler's class- or sourcepath. + */ +trait ClassPath { + import scala.tools.nsc.classpath._ + def asURLs: Seq[URL] + + /** Empty string represents root package */ + private[nsc] def packages(inPackage: String): Seq[PackageEntry] + private[nsc] def classes(inPackage: String): Seq[ClassFileEntry] + private[nsc] def sources(inPackage: String): Seq[SourceFileEntry] + + /** Allows to get entries for packages and classes merged with sources possibly in one pass. */ + private[nsc] def list(inPackage: String): ClassPathEntries + + /** + * It returns both classes from class file and source files (as our base ClassRepresentation). + * So note that it's not so strictly related to findClassFile. + */ + def findClass(className: String): Option[ClassRepresentation] = { + // A default implementation which should be overridden, if we can create the more efficient + // solution for a given type of ClassPath + val (pkg, simpleClassName) = PackageNameUtils.separatePkgAndClassNames(className) + + val foundClassFromClassFiles = classes(pkg).find(_.name == simpleClassName) + def findClassInSources = sources(pkg).find(_.name == simpleClassName) + + foundClassFromClassFiles orElse findClassInSources + } + def findClassFile(className: String): Option[AbstractFile] + + def asClassPathStrings: Seq[String] + + /** The whole classpath in the form of one String. + */ + def asClassPathString: String = ClassPath.join(asClassPathStrings: _*) + // for compatibility purposes + @deprecated("Use asClassPathString instead of this one", "2.11.5") + def asClasspathString: String = asClassPathString + + /** The whole sourcepath in the form of one String. + */ + def asSourcePathString: String +} + object ClassPath { - import scala.language.postfixOps + val RootPackage = "" /** Expand single path entry */ private def expandS(pattern: String): List[String] = { @@ -36,14 +69,14 @@ object ClassPath { /* Get all subdirectories, jars, zips out of a directory. */ def lsDir(dir: Directory, filt: String => Boolean = _ => true) = - dir.list filter (x => filt(x.name) && (x.isDirectory || isJarOrZip(x))) map (_.path) toList + dir.list.filter(x => filt(x.name) && (x.isDirectory || isJarOrZip(x))).map(_.path).toList if (pattern == "*") lsDir(Directory(".")) else if (pattern endsWith wildSuffix) lsDir(Directory(pattern dropRight 2)) else if (pattern contains '*') { try { val regexp = ("^" + pattern.replaceAllLiterally("""\*""", """.*""") + "$").r - lsDir(Directory(pattern).parent, regexp findFirstIn _ isDefined) + lsDir(Directory(pattern).parent, regexp.findFirstIn(_).isDefined) } catch { case _: PatternSyntaxException => List(pattern) } } @@ -51,7 +84,7 @@ object ClassPath { } /** Split classpath using platform-dependent path separator */ - def split(path: String): List[String] = (path split pathSeparator).toList filterNot (_ == "") distinct + def split(path: String): List[String] = (path split pathSeparator).toList.filterNot(_ == "").distinct /** Join classpath using platform-dependent path separator */ def join(paths: String*): String = paths filterNot (_ == "") mkString pathSeparator @@ -68,9 +101,10 @@ object ClassPath { def expandDir(extdir: String): List[String] = { AbstractFile getDirectory extdir match { case null => Nil - case dir => dir filter (_.isClassContainer) map (x => new java.io.File(dir.file, x.name) getPath) toList + case dir => dir.filter(_.isClassContainer).map(x => new java.io.File(dir.file, x.name).getPath).toList } } + /** Expand manifest jar classpath entries: these are either urls, or paths * relative to the location of the jar. */ @@ -88,302 +122,30 @@ object ClassPath { try Some(new URL(spec)) catch { case _: MalformedURLException => None } - /** A class modeling aspects of a ClassPath which should be - * propagated to any classpaths it creates. - */ - abstract class ClassPathContext[T] extends classpath.ClassPathFactory[ClassPath[T]] { - /** A filter which can be used to exclude entities from the classpath - * based on their name. - */ - def isValidName(name: String): Boolean = true - - /** Filters for assessing validity of various entities. - */ - def validClassFile(name: String) = endsClass(name) && isValidName(name) - def validPackage(name: String) = (name != "META-INF") && (name != "") && (name.charAt(0) != '.') - def validSourceFile(name: String) = endsScalaOrJava(name) - - /** From the representation to its identifier. - */ - def toBinaryName(rep: T): String - - def sourcesInPath(path: String): List[ClassPath[T]] = - for (file <- expandPath(path, expandStar = false) ; dir <- Option(AbstractFile getDirectory file)) yield - new SourcePath[T](dir, this) - } - def manifests: List[java.net.URL] = { - import scala.collection.convert.WrapAsScala.enumerationAsScalaIterator - Thread.currentThread().getContextClassLoader() - .getResources("META-INF/MANIFEST.MF") - .filter(_.getProtocol == "jar").toList + import scala.collection.JavaConverters._ + val resources = Thread.currentThread().getContextClassLoader().getResources("META-INF/MANIFEST.MF") + resources.asScala.filter(_.getProtocol == "jar").toList } - class JavaContext extends ClassPathContext[AbstractFile] { - def toBinaryName(rep: AbstractFile) = { - val name = rep.name - assert(endsClass(name), name) - FileUtils.stripClassExtension(name) - } + @deprecated("Shim for sbt's compiler interface", since = "2.12") + sealed abstract class ClassPathContext - def newClassPath(dir: AbstractFile) = new DirectoryClassPath(dir, this) - } - - object DefaultJavaContext extends JavaContext - - /** From the source file to its identifier. - */ - def toSourceName(f: AbstractFile): String = FileUtils.stripSourceExtension(f.name) + @deprecated("Shim for sbt's compiler interface", since = "2.12") + sealed abstract class JavaContext } -import ClassPath._ - -/** - * Represents a package which contains classes and other packages - */ -abstract class ClassPath[T] extends ClassFileLookup[T] { - /** - * The short name of the package (without prefix) - */ +trait ClassRepresentation { def name: String - - /** - * A String representing the origin of this classpath element, if known. - * For example, the path of the directory or jar. - */ - def origin: Option[String] = None - - /** Info which should be propagated to any sub-classpaths. - */ - def context: ClassPathContext[T] - - /** Lists of entities. - */ - def classes: IndexedSeq[ClassRepresentation[T]] - def packages: IndexedSeq[ClassPath[T]] - def sourcepaths: IndexedSeq[AbstractFile] - - /** The entries this classpath is composed of. In class `ClassPath` it's just the singleton list containing `this`. - * Subclasses such as `MergedClassPath` typically return lists with more elements. - */ - def entries: IndexedSeq[ClassPath[T]] = IndexedSeq(this) - - /** Merge classpath of `platform` and `urls` into merged classpath */ - def mergeUrlsIntoClassPath(urls: URL*): MergedClassPath[T] = { - // Collect our new jars/directories and add them to the existing set of classpaths - val allEntries = - (entries ++ - urls.map(url => context.newClassPath(io.AbstractFile.getURL(url))) - ).distinct - - // Combine all of our classpaths (old and new) into one merged classpath - new MergedClassPath(allEntries, context) - } - - /** - * Represents classes which can be loaded with a ClassfileLoader and/or SourcefileLoader. - */ - case class ClassRep(binary: Option[T], source: Option[AbstractFile]) extends ClassRepresentation[T] { - def name: String = binary match { - case Some(x) => context.toBinaryName(x) - case _ => - assert(source.isDefined) - toSourceName(source.get) - } - } - - /** Filters for assessing validity of various entities. - */ - def validClassFile(name: String) = context.validClassFile(name) - def validPackage(name: String) = context.validPackage(name) - def validSourceFile(name: String) = context.validSourceFile(name) - - /** - * Find a ClassRep given a class name of the form "package.subpackage.ClassName". - * Does not support nested classes on .NET - */ - override def findClass(name: String): Option[ClassRepresentation[T]] = - splitWhere(name, _ == '.', doDropIndex = true) match { - case Some((pkg, rest)) => - val rep = packages find (_.name == pkg) flatMap (_ findClass rest) - rep map { - case x: ClassRepresentation[T] => x - case x => throw new FatalError("Unexpected ClassRep '%s' found searching for name '%s'".format(x, name)) - } - case _ => - classes find (_.name == name) - } - - override def findClassFile(name: String): Option[AbstractFile] = - findClass(name) match { - case Some(ClassRepresentation(Some(x: AbstractFile), _)) => Some(x) - case _ => None - } - - override def asSourcePathString: String = sourcepaths.mkString(pathSeparator) - - def sortString = join(split(asClassPathString).sorted: _*) - override def equals(that: Any) = that match { - case x: ClassPath[_] => this.sortString == x.sortString - case _ => false - } - override def hashCode = sortString.hashCode() -} - -/** - * A Classpath containing source files - */ -class SourcePath[T](dir: AbstractFile, val context: ClassPathContext[T]) extends ClassPath[T] { - import FileUtils.AbstractFileOps - - def name = dir.name - override def origin = dir.underlyingSource map (_.path) - def asURLs = dir.toURLs() - def asClassPathString = dir.path - val sourcepaths: IndexedSeq[AbstractFile] = IndexedSeq(dir) - - private def traverse() = { - val classBuf = immutable.Vector.newBuilder[ClassRep] - val packageBuf = immutable.Vector.newBuilder[SourcePath[T]] - dir foreach { f => - if (!f.isDirectory && validSourceFile(f.name)) - classBuf += ClassRep(None, Some(f)) - else if (f.isDirectory && validPackage(f.name)) - packageBuf += new SourcePath[T](f, context) - } - (packageBuf.result(), classBuf.result()) - } - - lazy val (packages, classes) = traverse() - override def toString() = "sourcepath: "+ dir.toString() + def binary: Option[AbstractFile] + def source: Option[AbstractFile] } -/** - * A directory (or a .jar file) containing classfiles and packages - */ -class DirectoryClassPath(val dir: AbstractFile, val context: ClassPathContext[AbstractFile]) extends ClassPath[AbstractFile] { - import FileUtils.AbstractFileOps - - def name = dir.name - override def origin = dir.underlyingSource map (_.path) - def asURLs = dir.toURLs(default = Seq(new URL(name))) - def asClassPathString = dir.path - val sourcepaths: IndexedSeq[AbstractFile] = IndexedSeq() - - // calculates (packages, classes) in one traversal. - private def traverse() = { - val classBuf = immutable.Vector.newBuilder[ClassRep] - val packageBuf = immutable.Vector.newBuilder[DirectoryClassPath] - dir foreach { - f => - // Optimization: We assume the file was not changed since `dir` called - // `Path.apply` and categorized existent files as `Directory` - // or `File` (avoids IO operation JFile.isDirectory()). - val isDirectory = f match { - case pf: io.PlainFile => pf.givenPath match { - case _: io.Directory => true - case _: io.File => false - case _ => f.isDirectory - } - case _ => - f.isDirectory - } - if (!isDirectory && validClassFile(f.name)) - classBuf += ClassRep(Some(f), None) - else if (isDirectory && validPackage(f.name)) - packageBuf += new DirectoryClassPath(f, context) - } - (packageBuf.result(), classBuf.result()) - } +@deprecated("Shim for sbt's compiler interface", since = "2.12") +sealed abstract class DirectoryClassPath - lazy val (packages, classes) = traverse() - override def toString() = "directory classpath: "+ origin.getOrElse("?") -} +@deprecated("Shim for sbt's compiler interface", since = "2.12") +sealed abstract class MergedClassPath -/** - * A classpath unifying multiple class- and sourcepath entries. - */ -class MergedClassPath[T]( - override val entries: IndexedSeq[ClassPath[T]], - val context: ClassPathContext[T]) -extends ClassPath[T] { - - def this(entries: TraversableOnce[ClassPath[T]], context: ClassPathContext[T]) = - this(entries.toIndexedSeq, context) - - def name = entries.head.name - def asURLs = (entries flatMap (_.asURLs)).toList - lazy val sourcepaths: IndexedSeq[AbstractFile] = entries flatMap (_.sourcepaths) - - override def origin = Some(entries map (x => x.origin getOrElse x.name) mkString ("Merged(", ", ", ")")) - override def asClassPathString: String = join(entries map (_.asClassPathString) : _*) - - lazy val classes: IndexedSeq[ClassRepresentation[T]] = { - var count = 0 - val indices = mutable.HashMap[String, Int]() - val cls = new mutable.ArrayBuffer[ClassRepresentation[T]](1024) - - for (e <- entries; c <- e.classes) { - val name = c.name - if (indices contains name) { - val idx = indices(name) - val existing = cls(idx) - - if (existing.binary.isEmpty && c.binary.isDefined) - cls(idx) = ClassRep(binary = c.binary, source = existing.source) - if (existing.source.isEmpty && c.source.isDefined) - cls(idx) = ClassRep(binary = existing.binary, source = c.source) - } - else { - indices(name) = count - cls += c - count += 1 - } - } - cls.toIndexedSeq - } - - lazy val packages: IndexedSeq[ClassPath[T]] = { - var count = 0 - val indices = mutable.HashMap[String, Int]() - val pkg = new mutable.ArrayBuffer[ClassPath[T]](256) - - for (e <- entries; p <- e.packages) { - val name = p.name - if (indices contains name) { - val idx = indices(name) - pkg(idx) = addPackage(pkg(idx), p) - } - else { - indices(name) = count - pkg += p - count += 1 - } - } - pkg.toIndexedSeq - } - - private def addPackage(to: ClassPath[T], pkg: ClassPath[T]) = { - val newEntries: IndexedSeq[ClassPath[T]] = to match { - case cp: MergedClassPath[_] => cp.entries :+ pkg - case _ => IndexedSeq(to, pkg) - } - new MergedClassPath[T](newEntries, context) - } - - def show() { - println("ClassPath %s has %d entries and results in:\n".format(name, entries.size)) - asClassPathString split ':' foreach (x => println(" " + x)) - } - - override def toString() = "merged classpath "+ entries.mkString("(", "\n", ")") -} - -/** - * The classpath when compiling with target:jvm. Binary files (classfiles) are represented - * as AbstractFile. nsc.io.ZipArchive is used to view zip/jar archives as directories. - */ -class JavaClassPath( - containers: IndexedSeq[ClassPath[AbstractFile]], - context: JavaContext) -extends MergedClassPath[AbstractFile](containers, context) { } +@deprecated("Shim for sbt's compiler interface", since = "2.12") +sealed abstract class JavaClassPath diff --git a/src/compiler/scala/tools/reflect/ReflectMain.scala b/src/compiler/scala/tools/reflect/ReflectMain.scala index 8d8418945a4e..7d8291069928 100644 --- a/src/compiler/scala/tools/reflect/ReflectMain.scala +++ b/src/compiler/scala/tools/reflect/ReflectMain.scala @@ -5,12 +5,12 @@ import scala.reflect.internal.util.ScalaClassLoader import scala.tools.nsc.Driver import scala.tools.nsc.Global import scala.tools.nsc.Settings -import scala.tools.util.PathResolverFactory +import scala.tools.util.PathResolver object ReflectMain extends Driver { private def classloaderFromSettings(settings: Settings) = { - val classPathURLs = PathResolverFactory.create(settings).resultAsURLs + val classPathURLs = new PathResolver(settings).resultAsURLs ScalaClassLoader.fromURLs(classPathURLs, getClass.getClassLoader) } diff --git a/src/compiler/scala/tools/util/PathResolver.scala b/src/compiler/scala/tools/util/PathResolver.scala index 9decc99c8dc5..c351b6ace1bb 100644 --- a/src/compiler/scala/tools/util/PathResolver.scala +++ b/src/compiler/scala/tools/util/PathResolver.scala @@ -10,12 +10,10 @@ package util import java.net.URL import scala.tools.reflect.WrappedProperties.AccessControl import scala.tools.nsc.Settings -import scala.tools.nsc.util.{ ClassFileLookup, ClassPath, JavaClassPath } -import scala.reflect.io.{ File, Directory, Path, AbstractFile } -import ClassPath.{ JavaContext, DefaultJavaContext, split } +import scala.tools.nsc.util.ClassPath +import scala.reflect.io.{Directory, File, Path} import PartialFunction.condOpt -import scala.tools.nsc.classpath.{ AggregateFlatClassPath, ClassPathFactory, FlatClassPath, FlatClassPathFactory } -import scala.tools.nsc.settings.ClassPathRepresentationType +import scala.tools.nsc.classpath._ // Loosely based on the draft specification at: // https://wiki.scala-lang.org/display/SIW/Classpath @@ -40,7 +38,7 @@ object PathResolver { } /** pretty print class path */ - def ppcp(s: String) = split(s) match { + def ppcp(s: String) = ClassPath.split(s) match { case Nil => "" case Seq(x) => x case xs => xs.mkString(EOL, EOL, "") @@ -164,13 +162,6 @@ object PathResolver { |}""".asLines } - @deprecated("This method is no longer used be scalap and will be deleted", "2.11.5") - def fromPathString(path: String, context: JavaContext = DefaultJavaContext): JavaClassPath = { - val s = new Settings() - s.classpath.value = path - new PathResolver(s, context).result - } - /** With no arguments, show the interesting values in Environment and Defaults. * If there are arguments, show those in Calculated as if those options had been * given to a scala runner. @@ -182,28 +173,19 @@ object PathResolver { } else { val settings = new Settings() val rest = settings.processArguments(args.toList, processAll = false)._2 - val pr = PathResolverFactory.create(settings) + val pr = new PathResolver(settings) println("COMMAND: 'scala %s'".format(args.mkString(" "))) println("RESIDUAL: 'scala %s'\n".format(rest.mkString(" "))) pr.result match { - case cp: JavaClassPath => - cp.show() - case cp: AggregateFlatClassPath => + case cp: AggregateClassPath => println(s"ClassPath has ${cp.aggregates.size} entries and results in:\n${cp.asClassPathStrings}") } } } -trait PathResolverResult { - def result: ClassFileLookup[AbstractFile] - - def resultAsURLs: Seq[URL] = result.asURLs -} - -abstract class PathResolverBase[BaseClassPathType <: ClassFileLookup[AbstractFile], ResultClassPathType <: BaseClassPathType] -(settings: Settings, classPathFactory: ClassPathFactory[BaseClassPathType]) - extends PathResolverResult { +final class PathResolver(settings: Settings) { + private val classPathFactory = new ClassPathFactory(settings) import PathResolver.{ AsLines, Defaults, ppcp } @@ -251,7 +233,7 @@ abstract class PathResolverBase[BaseClassPathType <: ClassFileLookup[AbstractFil import classPathFactory._ // Assemble the elements! - def basis = List[Traversable[BaseClassPathType]]( + def basis = List[Traversable[ClassPath]]( classesInPath(javaBootClassPath), // 1. The Java bootstrap class path. contentsOfDirsInPath(javaExtDirs), // 2. The Java extension class path. classesInExpandedPath(javaUserClassPath), // 3. The Java application class path. @@ -282,7 +264,7 @@ abstract class PathResolverBase[BaseClassPathType <: ClassFileLookup[AbstractFil import PathResolver.MkLines - def result: ResultClassPathType = { + def result: ClassPath = { val cp = computeResult() if (settings.Ylogcp) { Console print f"Classpath built from ${settings.toConciseString} %n" @@ -295,34 +277,11 @@ abstract class PathResolverBase[BaseClassPathType <: ClassFileLookup[AbstractFil cp } + def resultAsURLs: Seq[URL] = result.asURLs + @deprecated("Use resultAsURLs instead of this one", "2.11.5") def asURLs: List[URL] = resultAsURLs.toList - protected def computeResult(): ResultClassPathType + private def computeResult(): ClassPath = AggregateClassPath(containers.toIndexedSeq) } -class PathResolver(settings: Settings, context: JavaContext) - extends PathResolverBase[ClassPath[AbstractFile], JavaClassPath](settings, context) { - - def this(settings: Settings) = this(settings, DefaultJavaContext) - - override protected def computeResult(): JavaClassPath = - new JavaClassPath(containers.toIndexedSeq, context) -} - -class FlatClassPathResolver(settings: Settings, flatClassPathFactory: ClassPathFactory[FlatClassPath]) - extends PathResolverBase[FlatClassPath, AggregateFlatClassPath](settings, flatClassPathFactory) { - - def this(settings: Settings) = this(settings, new FlatClassPathFactory(settings)) - - override protected def computeResult(): AggregateFlatClassPath = AggregateFlatClassPath(containers.toIndexedSeq) -} - -object PathResolverFactory { - - def create(settings: Settings): PathResolverResult = - settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Flat => new FlatClassPathResolver(settings) - case ClassPathRepresentationType.Recursive => new PathResolver(settings) - } -} diff --git a/src/eclipse/README.md b/src/eclipse/README.md index 3df7fbed7d7e..f67fa26e5edf 100644 --- a/src/eclipse/README.md +++ b/src/eclipse/README.md @@ -7,21 +7,20 @@ The following points describe how to build Scala using Eclipse. 0. Download the [Scala IDE bundle](http://scala-ide.org/download/sdk.html). It comes preconfigured for optimal performance. -0. Run `ant init` to download some necessary jars. - -0. Import the project (in `src/eclipse`) via `File` → `Import Existing Projects` and navigate to `scala/src/eclipse`. Check all projects and click ok. - -0. You need to define a `path variable` inside Eclipse. Define `SCALA_BASEDIR` in -`Preferences/General/Workspace/Linked Resources`. The value should be the absolute -path to your Scala checkout. All paths in the project files are relative to this one, -so nothing will work before you do so. - - The same `SCALA_BASEDIR` variable needs to be defined as a `classpath variable` in +0. Run `ant build` to download some necessary jars and see a successful build. + +0. You need to define a `path variable` and a `classpath variable` inside Eclipse, both pointing to the Scala checkout directory: + - (experimental): run `./update-workspace.sh scala_checkout_dir [workspace_dir]`. This should update your workspace settings + (restart Eclipse if it was running). For example: + ``` + ./update-workspace.sh $HOME/git/scala ~/Documents/workspace-scalac + ``` + - If the above didn't work, you can perform these steps manually: Define `SCALA_BASEDIR` in `Preferences/General/Workspace/Linked Resources`. The value should be the absolute +path to your Scala checkout. All paths in the project files are relative to this one, so nothing will work before you do so. +The same `SCALA_BASEDIR` variable needs to be defined **also** as a `classpath variable` in `Java/Build Path/Classpath Variables`. - Additionally, we start using Maven dependencies (e.g. `JUnit`) so you need to define another -`classpath variable` inside Eclipse. Define `M2_REPO` in `Java/Build Path/Classpath Variables` -to point to your local Maven repository (e.g. `$HOME/.m2/repository`). +0. Import the project (in `src/eclipse`) via `File` → `Import Existing Projects` and navigate to `scala/src/eclipse`. Check all projects and click ok. Lastly, the JRE used by Eclipse needs to know the path to the `JLine` library, which is used by the REPL. To set the JAR file, navigate to `Java/Installed JREs`, select the default JRE, press `Edit/Add External JARs...` diff --git a/src/eclipse/interactive/.classpath b/src/eclipse/interactive/.classpath index 929ce65f2a73..721351a20775 100644 --- a/src/eclipse/interactive/.classpath +++ b/src/eclipse/interactive/.classpath @@ -1,10 +1,10 @@ + - diff --git a/src/eclipse/partest/.classpath b/src/eclipse/partest/.classpath index 63f46f46cd13..ab9ede5a1921 100644 --- a/src/eclipse/partest/.classpath +++ b/src/eclipse/partest/.classpath @@ -1,14 +1,14 @@ + - - - + + - + diff --git a/src/eclipse/reflect/.classpath b/src/eclipse/reflect/.classpath index 3f14621da79f..ee6bcd47da1a 100644 --- a/src/eclipse/reflect/.classpath +++ b/src/eclipse/reflect/.classpath @@ -3,5 +3,6 @@ + diff --git a/src/eclipse/repl/.classpath b/src/eclipse/repl/.classpath index 085b58f66872..682377adc98b 100644 --- a/src/eclipse/repl/.classpath +++ b/src/eclipse/repl/.classpath @@ -1,10 +1,12 @@ - + + - + + diff --git a/src/eclipse/scala-compiler/.classpath b/src/eclipse/scala-compiler/.classpath index bbed3324c4cc..625b9b2e4b8f 100644 --- a/src/eclipse/scala-compiler/.classpath +++ b/src/eclipse/scala-compiler/.classpath @@ -4,7 +4,8 @@ - + + diff --git a/src/eclipse/scaladoc/.classpath b/src/eclipse/scaladoc/.classpath index 3a3ebf7799f0..40387983b004 100644 --- a/src/eclipse/scaladoc/.classpath +++ b/src/eclipse/scaladoc/.classpath @@ -2,13 +2,12 @@ + - - - - - + + + diff --git a/src/eclipse/test-junit/.classpath b/src/eclipse/test-junit/.classpath index 995d94aa91fe..3635c8511272 100644 --- a/src/eclipse/test-junit/.classpath +++ b/src/eclipse/test-junit/.classpath @@ -1,16 +1,16 @@ + - - - + + diff --git a/src/eclipse/update-workspace.sh b/src/eclipse/update-workspace.sh new file mode 100755 index 000000000000..24382d1445ba --- /dev/null +++ b/src/eclipse/update-workspace.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +function usage() { + echo "$0 scala_checkout_dir [workspace_dir]" + echo "\n Add necessary path variables to Eclipse workspace settings for Scalac to build" +} + +METADATA_DIR=`pwd`/.metadata + +if [ $# -lt 1 ]; then + echo "Need the Scala directory checkout as argument" + exit 1 +fi + +SCALA_DIR=$1 + +if [ ! -z $2 ]; then + METADATA_DIR=$2/.metadata +fi + +if [ ! -d $METADATA_DIR ]; then + echo "$METADATA_DIR is not a directory" + exit 1 +fi + +echo "Using metadata directory $METADATA_DIR and Scala checkout $SCALA_DIR" + +CORE_PREFS=$METADATA_DIR/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs +if [ ! -f $CORE_PREFS ]; then + echo "Couldn't find $CORE_PREFS. Is $METADATA_DIR an Eclipse workspace?" + exit 1 +fi +echo -e "Workspace preferences:\t$CORE_PREFS" + +JDT_PREFS=$METADATA_DIR/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs +if [ ! -f $JDT_PREFS ]; then + echo "Couldn't find $JDT_PREFS. Creating fresh file." + touch $JDT_PREFS +fi +echo -e "JDT preferences:\t$JDT_PREFS" + +# $1 - preference file (will be backed-up before writing) +# $2 - preference key +# $3 - preference value +function updatePref() { + mv $1 ${1}_backup + + awk -v key=$2 -v value=$3 ' + BEGIN { + FS="="; + OFS="="; + prev="" + } + { + if ($1 == key) { + prev=$2 + $2=value + } + print + } + END { + if (prev) { + printf "Updated existing value from %s to %s\n", prev, value > "/dev/stderr" + } else { + print key,value + } + } + ' ${1}_backup >$1 +} + +updatePref $CORE_PREFS "pathvariable.SCALA_BASEDIR" $SCALA_DIR +updatePref $JDT_PREFS "org.eclipse.jdt.core.classpathVariable.SCALA_BASEDIR" $SCALA_DIR diff --git a/src/intellij/scala.ipr.SAMPLE b/src/intellij/scala.ipr.SAMPLE index 8184b0e45b62..79ad2808f692 100644 --- a/src/intellij/scala.ipr.SAMPLE +++ b/src/intellij/scala.ipr.SAMPLE @@ -77,7 +77,7 @@ - + @@ -100,7 +100,7 @@ - + @@ -126,7 +126,7 @@ - + @@ -159,7 +159,7 @@ - + @@ -303,7 +303,7 @@ - + @@ -312,4 +312,4 @@ - \ No newline at end of file + diff --git a/src/library/scala/collection/JavaConversions.scala b/src/library/scala/collection/JavaConversions.scala index f9a34f78d50d..960e452cdfed 100644 --- a/src/library/scala/collection/JavaConversions.scala +++ b/src/library/scala/collection/JavaConversions.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -11,10 +11,10 @@ package collection import convert._ -/** A collection of implicit conversions supporting interoperability between - * Scala and Java collections. +/** A variety of implicit conversions supporting interoperability between + * Scala and Java collections. * - * The following conversions are supported: + * The following conversions are supported: *{{{ * scala.collection.Iterable <=> java.lang.Iterable * scala.collection.Iterable <=> java.util.Collection @@ -24,8 +24,8 @@ import convert._ * scala.collection.mutable.Map <=> java.util.{ Map, Dictionary } * scala.collection.concurrent.Map <=> java.util.concurrent.ConcurrentMap *}}} - * In all cases, converting from a source type to a target type and back - * again will return the original source object, eg. + * In all cases, converting from a source type to a target type and back + * again will return the original source object: * *{{{ * import scala.collection.JavaConversions._ @@ -45,8 +45,16 @@ import convert._ * java.util.Properties => scala.collection.mutable.Map[String, String] *}}} * + * The transparent conversions provided here are considered + * fragile because they can result in unexpected behavior and performance. + * + * Therefore, this API has been deprecated and `JavaConverters` should be + * used instead. `JavaConverters` provides the same conversions, but through + * extension methods. + * * @author Miles Sabin * @author Martin Odersky * @since 2.8 */ +@deprecated("Use JavaConverters", since="2.12") object JavaConversions extends WrapAsScala with WrapAsJava diff --git a/src/library/scala/collection/JavaConverters.scala b/src/library/scala/collection/JavaConverters.scala index 58fbc5afa5fa..d48a1764e9cd 100644 --- a/src/library/scala/collection/JavaConverters.scala +++ b/src/library/scala/collection/JavaConverters.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -11,14 +11,12 @@ package collection import convert._ -// TODO: I cleaned all this documentation up in JavaConversions, but the -// documentation in here is basically the pre-cleaned-up version with minor -// additions. Would be nice to have in one place. - -/** A collection of decorators that allow converting between - * Scala and Java collections using `asScala` and `asJava` methods. +/** A variety of decorators that enable converting between + * Scala and Java collections using extension methods, `asScala` and `asJava`. + * + * The extension methods return adapters for the corresponding API. * - * The following conversions are supported via `asJava`, `asScala` + * The following conversions are supported via `asScala` and `asJava`: *{{{ * scala.collection.Iterable <=> java.lang.Iterable * scala.collection.Iterator <=> java.util.Iterator @@ -27,25 +25,14 @@ import convert._ * scala.collection.mutable.Map <=> java.util.Map * scala.collection.mutable.concurrent.Map <=> java.util.concurrent.ConcurrentMap *}}} - * In all cases, converting from a source type to a target type and back - * again will return the original source object, e.g. - * {{{ - * import scala.collection.JavaConverters._ - * - * val sl = new scala.collection.mutable.ListBuffer[Int] - * val jl : java.util.List[Int] = sl.asJava - * val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala - * assert(sl eq sl2) - * }}} - * The following conversions are also supported, but the - * direction from Scala to Java is done by the more specifically named methods: - * `asJavaCollection`, `asJavaEnumeration`, `asJavaDictionary`. + * The following conversions are supported via `asScala` and through + * specially-named extension methods to convert to Java collections, as shown: *{{{ - * scala.collection.Iterable <=> java.util.Collection - * scala.collection.Iterator <=> java.util.Enumeration - * scala.collection.mutable.Map <=> java.util.Dictionary + * scala.collection.Iterable <=> java.util.Collection (via asJavaCollection) + * scala.collection.Iterator <=> java.util.Enumeration (via asJavaEnumeration) + * scala.collection.mutable.Map <=> java.util.Dictionary (via asJavaDictionary) *}}} - * In addition, the following one way conversions are provided via `asJava`: + * In addition, the following one-way conversions are provided via `asJava`: *{{{ * scala.collection.Seq => java.util.List * scala.collection.mutable.Seq => java.util.List @@ -56,6 +43,31 @@ import convert._ *{{{ * java.util.Properties => scala.collection.mutable.Map *}}} + * In all cases, converting from a source type to a target type and back + * again will return the original source object. For example: + * {{{ + * import scala.collection.JavaConverters._ + * + * val source = new scala.collection.mutable.ListBuffer[Int] + * val target: java.util.List[Int] = source.asJava + * val other: scala.collection.mutable.Buffer[Int] = target.asScala + * assert(source eq other) + * }}} + * Alternatively, the conversion methods have descriptive names and can be invoked explicitly. + * {{{ + * scala> val vs = java.util.Arrays.asList("hi", "bye") + * vs: java.util.List[String] = [hi, bye] + * + * scala> val ss = asScalaIterator(vs.iterator) + * ss: Iterator[String] = non-empty iterator + * + * scala> .toList + * res0: List[String] = List(hi, bye) + * + * scala> val ss = asScalaBuffer(vs) + * ss: scala.collection.mutable.Buffer[String] = Buffer(hi, bye) + * }}} + * * @since 2.8.1 */ object JavaConverters extends DecorateAsJava with DecorateAsScala diff --git a/src/library/scala/collection/concurrent/TrieMap.scala b/src/library/scala/collection/concurrent/TrieMap.scala index d113cbfcdfe5..5dc01547e6c9 100644 --- a/src/library/scala/collection/concurrent/TrieMap.scala +++ b/src/library/scala/collection/concurrent/TrieMap.scala @@ -1106,14 +1106,14 @@ private[concurrent] case object TrieMapSerializationEnd private[concurrent] object Debug { - import scala.collection._ + import JavaConverters._ lazy val logbuffer = new java.util.concurrent.ConcurrentLinkedQueue[AnyRef] def log(s: AnyRef) = logbuffer.add(s) def flush() { - for (s <- JavaConversions.asScalaIterator(logbuffer.iterator())) Console.out.println(s.toString) + for (s <- logbuffer.iterator().asScala) Console.out.println(s.toString) logbuffer.clear() } diff --git a/src/library/scala/collection/convert/AsJavaConverters.scala b/src/library/scala/collection/convert/AsJavaConverters.scala new file mode 100644 index 000000000000..c7c1fb9c745c --- /dev/null +++ b/src/library/scala/collection/convert/AsJavaConverters.scala @@ -0,0 +1,262 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala +package collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } + +/** Defines converter methods from Scala to Java collections. */ +trait AsJavaConverters { + import Wrappers._ + + /** + * Converts a Scala `Iterator` to a Java `Iterator`. + * + * The returned Java `Iterator` is backed by the provided Scala `Iterator` and any side-effects of + * using it via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Iterator` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaIterator]](java.util.Iterator)` then the original Java `Iterator` will + * be returned. + * + * @param i The Scala `Iterator` to be converted. + * @return A Java `Iterator` view of the argument. + */ + def asJavaIterator[A](i: Iterator[A]): ju.Iterator[A] = i match { + case null => null + case JIteratorWrapper(wrapped) => wrapped.asInstanceOf[ju.Iterator[A]] + case _ => IteratorWrapper(i) + } + + /** + * Converts a Scala `Iterator` to a Java `Enumeration`. + * + * The returned Java `Enumeration` is backed by the provided Scala `Iterator` and any side-effects + * of using it via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Iterator` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.enumerationAsScalaIterator]](java.util.Enumeration)` then the original Java + * `Enumeration` will be returned. + * + * @param i The Scala `Iterator` to be converted. + * @return A Java `Enumeration` view of the argument. + */ + def asJavaEnumeration[A](i: Iterator[A]): ju.Enumeration[A] = i match { + case null => null + case JEnumerationWrapper(wrapped) => wrapped.asInstanceOf[ju.Enumeration[A]] + case _ => IteratorWrapper(i) + } + + /** + * Converts a Scala `Iterable` to a Java `Iterable`. + * + * The returned Java `Iterable` is backed by the provided Scala `Iterable` and any side-effects of + * using it via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Iterable` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.iterableAsScalaIterable]](java.lang.Iterable)` then the original Java + * `Iterable` will be returned. + * + * @param i The Scala `Iterable` to be converted. + * @return A Java `Iterable` view of the argument. + */ + def asJavaIterable[A](i: Iterable[A]): jl.Iterable[A] = i match { + case null => null + case JIterableWrapper(wrapped) => wrapped.asInstanceOf[jl.Iterable[A]] + case _ => IterableWrapper(i) + } + + /** + * Converts a Scala `Iterable` to an immutable Java `Collection`. + * + * If the Scala `Iterable` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.collectionAsScalaIterable]](java.util.Collection)` then the original Java + * `Collection` will be returned. + * + * @param i The Scala `Iterable` to be converted. + * @return A Java `Collection` view of the argument. + */ + def asJavaCollection[A](i: Iterable[A]): ju.Collection[A] = i match { + case null => null + case JCollectionWrapper(wrapped) => wrapped.asInstanceOf[ju.Collection[A]] + case _ => new IterableWrapper(i) + } + + /** + * Converts a Scala mutable `Buffer` to a Java List. + * + * The returned Java List is backed by the provided Scala `Buffer` and any side-effects of using + * it via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Buffer` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaBuffer]](java.util.List)` then the original Java `List` will be + * returned. + * + * @param b The Scala `Buffer` to be converted. + * @return A Java `List` view of the argument. + */ + def bufferAsJavaList[A](b: mutable.Buffer[A]): ju.List[A] = b match { + case null => null + case JListWrapper(wrapped) => wrapped + case _ => new MutableBufferWrapper(b) + } + + /** + * Converts a Scala mutable `Seq` to a Java `List`. + * + * The returned Java `List` is backed by the provided Scala `Seq` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Seq` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaBuffer]](java.util.List)` then the original Java `List` will be + * returned. + * + * @param s The Scala `Seq` to be converted. + * @return A Java `List` view of the argument. + */ + def mutableSeqAsJavaList[A](s: mutable.Seq[A]): ju.List[A] = s match { + case null => null + case JListWrapper(wrapped) => wrapped + case _ => new MutableSeqWrapper(s) + } + + /** + * Converts a Scala `Seq` to a Java `List`. + * + * The returned Java `List` is backed by the provided Scala `Seq` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Seq` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaBuffer]](java.util.List)` then the original Java `List` will be + * returned. + * + * @param s The Scala `Seq` to be converted. + * @return A Java `List` view of the argument. + */ + def seqAsJavaList[A](s: Seq[A]): ju.List[A] = s match { + case null => null + case JListWrapper(wrapped) => wrapped.asInstanceOf[ju.List[A]] + case _ => new SeqWrapper(s) + } + + /** + * Converts a Scala mutable `Set` to a Java `Set`. + * + * The returned Java `Set` is backed by the provided Scala `Set` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Set` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaSet]](java.util.Set)` then the original Java `Set` will be returned. + * + * @param s The Scala mutable `Set` to be converted. + * @return A Java `Set` view of the argument. + */ + def mutableSetAsJavaSet[A](s: mutable.Set[A]): ju.Set[A] = s match { + case null => null + case JSetWrapper(wrapped) => wrapped + case _ => new MutableSetWrapper(s) + } + + /** + * Converts a Scala `Set` to a Java `Set`. + * + * The returned Java `Set` is backed by the provided Scala `Set` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Set` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asScalaSet]](java.util.Set)` then the original Java `Set` will be returned. + * + * @param s The Scala `Set` to be converted. + * @return A Java `Set` view of the argument. + */ + def setAsJavaSet[A](s: Set[A]): ju.Set[A] = s match { + case null => null + case JSetWrapper(wrapped) => wrapped + case _ => new SetWrapper(s) + } + + /** + * Converts a Scala mutable `Map` to a Java `Map`. + * + * The returned Java `Map` is backed by the provided Scala `Map` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Map` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mapAsScalaMap]](java.util.Map)` then the original Java `Map` will be + * returned. + * + * @param m The Scala mutable `Map` to be converted. + * @return A Java `Map` view of the argument. + */ + def mutableMapAsJavaMap[A, B](m: mutable.Map[A, B]): ju.Map[A, B] = m match { + case null => null + case JMapWrapper(wrapped) => wrapped + case _ => new MutableMapWrapper(m) + } + + /** + * Converts a Scala mutable `Map` to a Java `Dictionary`. + * + * The returned Java `Dictionary` is backed by the provided Scala `Dictionary` and any + * side-effects of using it via the Java interface will be visible via the Scala interface and + * vice versa. + * + * If the Scala `Dictionary` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.dictionaryAsScalaMap]](java.util.Dictionary)` then the original Java + * `Dictionary` will be returned. + * + * @param m The Scala `Map` to be converted. + * @return A Java `Dictionary` view of the argument. + */ + def asJavaDictionary[A, B](m: mutable.Map[A, B]): ju.Dictionary[A, B] = m match { + case null => null + case JDictionaryWrapper(wrapped) => wrapped + case _ => new DictionaryWrapper(m) + } + + /** + * Converts a Scala `Map` to a Java `Map`. + * + * The returned Java `Map` is backed by the provided Scala `Map` and any side-effects of using it + * via the Java interface will be visible via the Scala interface and vice versa. + * + * If the Scala `Map` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mapAsScalaMap]](java.util.Map)` then the original Java `Map` will be + * returned. + * + * @param m The Scala `Map` to be converted. + * @return A Java `Map` view of the argument. + */ + def mapAsJavaMap[A, B](m: Map[A, B]): ju.Map[A, B] = m match { + case null => null + case JMapWrapper(wrapped) => wrapped.asInstanceOf[ju.Map[A, B]] + case _ => new MapWrapper(m) + } + + /** + * Converts a Scala mutable `concurrent.Map` to a Java `ConcurrentMap`. + * + * The returned Java `ConcurrentMap` is backed by the provided Scala `concurrent.Map` and any + * side-effects of using it via the Java interface will be visible via the Scala interface and + * vice versa. + * + * If the Scala `concurrent.Map` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mapAsScalaConcurrentMap]](java.util.concurrent.ConcurrentMap)` then the + * original Java `ConcurrentMap` will be returned. + * + * @param m The Scala `concurrent.Map` to be converted. + * @return A Java `ConcurrentMap` view of the argument. + */ + def mapAsJavaConcurrentMap[A, B](m: concurrent.Map[A, B]): juc.ConcurrentMap[A, B] = m match { + case null => null + case JConcurrentMapWrapper(wrapped) => wrapped + case _ => new ConcurrentMapWrapper(m) + } +} diff --git a/src/library/scala/collection/convert/AsScalaConverters.scala b/src/library/scala/collection/convert/AsScalaConverters.scala new file mode 100644 index 000000000000..f9e38797e1f2 --- /dev/null +++ b/src/library/scala/collection/convert/AsScalaConverters.scala @@ -0,0 +1,207 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala +package collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } + +/** Defines converter methods from Java to Scala collections. */ +trait AsScalaConverters { + import Wrappers._ + + /** + * Converts a Java `Iterator` to a Scala `Iterator`. + * + * The returned Scala `Iterator` is backed by the provided Java `Iterator` and any side-effects of + * using it via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Iterator` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asJavaIterator]](scala.collection.Iterator)` then the original Scala + * `Iterator` will be returned. + * + * @param i The Java `Iterator` to be converted. + * @return A Scala `Iterator` view of the argument. + */ + def asScalaIterator[A](i: ju.Iterator[A]): Iterator[A] = i match { + case null => null + case IteratorWrapper(wrapped) => wrapped + case _ => JIteratorWrapper(i) + } + + /** + * Converts a Java `Enumeration` to a Scala `Iterator`. + * + * The returned Scala `Iterator` is backed by the provided Java `Enumeration` and any side-effects + * of using it via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Enumeration` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asJavaEnumeration]](scala.collection.Iterator)` then the original Scala + * `Iterator` will be returned. + * + * @param i The Java `Enumeration` to be converted. + * @return A Scala `Iterator` view of the argument. + */ + def enumerationAsScalaIterator[A](i: ju.Enumeration[A]): Iterator[A] = i match { + case null => null + case IteratorWrapper(wrapped) => wrapped + case _ => JEnumerationWrapper(i) + } + + /** + * Converts a Java `Iterable` to a Scala `Iterable`. + * + * The returned Scala `Iterable` is backed by the provided Java `Iterable` and any side-effects of + * using it via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Iterable` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asJavaIterable]](scala.collection.Iterable) then the original Scala + * `Iterable` will be returned. + * + * @param i The Java `Iterable` to be converted. + * @return A Scala `Iterable` view of the argument. + */ + def iterableAsScalaIterable[A](i: jl.Iterable[A]): Iterable[A] = i match { + case null => null + case IterableWrapper(wrapped) => wrapped + case _ => JIterableWrapper(i) + } + + /** + * Converts a Java `Collection` to an Scala `Iterable`. + * + * If the Java `Collection` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asJavaCollection]](scala.collection.Iterable)` then the original Scala + * `Iterable` will be returned. + * + * @param i The Java `Collection` to be converted. + * @return A Scala `Iterable` view of the argument. + */ + def collectionAsScalaIterable[A](i: ju.Collection[A]): Iterable[A] = i match { + case null => null + case IterableWrapper(wrapped) => wrapped + case _ => JCollectionWrapper(i) + } + + /** + * Converts a Java `List` to a Scala mutable `Buffer`. + * + * The returned Scala `Buffer` is backed by the provided Java `List` and any side-effects of using + * it via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `List` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.bufferAsJavaList]](scala.collection.mutable.Buffer)` then the original Scala + * `Buffer` will be returned. + * + * @param l The Java `List` to be converted. + * @return A Scala mutable `Buffer` view of the argument. + */ + def asScalaBuffer[A](l: ju.List[A]): mutable.Buffer[A] = l match { + case null => null + case MutableBufferWrapper(wrapped) => wrapped + case _ => new JListWrapper(l) + } + + /** + * Converts a Java `Set` to a Scala mutable `Set`. + * + * The returned Scala `Set` is backed by the provided Java `Set` and any side-effects of using it + * via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Set` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mutableSetAsJavaSet]](scala.collection.mutable.Set)` then the original Scala + * `Set` will be returned. + * + * @param s The Java `Set` to be converted. + * @return A Scala mutable `Set` view of the argument. + */ + def asScalaSet[A](s: ju.Set[A]): mutable.Set[A] = s match { + case null => null + case MutableSetWrapper(wrapped) => wrapped + case _ => new JSetWrapper(s) + } + + /** + * Converts a Java `Map` to a Scala mutable `Map`. + * + * The returned Scala `Map` is backed by the provided Java `Map` and any side-effects of using it + * via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Map` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mutableMapAsJavaMap]](scala.collection.mutable.Map)` then the original Scala + * `Map` will be returned. + * + * If the wrapped map is synchronized (e.g. from `java.util.Collections.synchronizedMap`), it is + * your responsibility to wrap all non-atomic operations with `underlying.synchronized`. + * This includes `get`, as `java.util.Map`'s API does not allow for an atomic `get` when `null` + * values may be present. + * + * @param m The Java `Map` to be converted. + * @return A Scala mutable `Map` view of the argument. + */ + def mapAsScalaMap[A, B](m: ju.Map[A, B]): mutable.Map[A, B] = m match { + case null => null + case MutableMapWrapper(wrapped) => wrapped + case _ => new JMapWrapper(m) + } + + /** + * Converts a Java `ConcurrentMap` to a Scala mutable `ConcurrentMap`. + * + * The returned Scala `ConcurrentMap` is backed by the provided Java `ConcurrentMap` and any + * side-effects of using it via the Scala interface will be visible via the Java interface and + * vice versa. + * + * If the Java `ConcurrentMap` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.mapAsJavaConcurrentMap]](scala.collection.mutable.ConcurrentMap)` + * then the original Scala `ConcurrentMap` will be returned. + * + * @param m The Java `ConcurrentMap` to be converted. + * @return A Scala mutable `ConcurrentMap` view of the argument. + */ + def mapAsScalaConcurrentMap[A, B](m: juc.ConcurrentMap[A, B]): concurrent.Map[A, B] = m match { + case null => null + case cmw: ConcurrentMapWrapper[_, _] => cmw.underlying + case _ => new JConcurrentMapWrapper(m) + } + + /** + * Converts a Java `Dictionary` to a Scala mutable `Map`. + * + * The returned Scala `Map` is backed by the provided Java `Dictionary` and any side-effects of + * using it via the Scala interface will be visible via the Java interface and vice versa. + * + * If the Java `Dictionary` was previously obtained from an implicit or explicit call of + * `[[JavaConverters.asJavaDictionary]](scala.collection.mutable.Map)` then the original + * Scala `Map` will be returned. + * + * @param p The Java `Dictionary` to be converted. + * @return A Scala mutable `Map` view of the argument. + */ + def dictionaryAsScalaMap[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = p match { + case null => null + case DictionaryWrapper(wrapped) => wrapped + case _ => new JDictionaryWrapper(p) + } + + /** + * Converts a Java `Properties` to a Scala mutable `Map[String, String]`. + * + * The returned Scala `Map[String, String]` is backed by the provided Java `Properties` and any + * side-effects of using it via the Scala interface will be visible via the Java interface and + * vice versa. + * + * @param p The Java `Properties` to be converted. + * @return A Scala mutable `Map[String, String]` view of the argument. + */ + def propertiesAsScalaMap(p: ju.Properties): mutable.Map[String, String] = p match { + case null => null + case _ => new JPropertiesWrapper(p) + } +} diff --git a/src/library/scala/collection/convert/DecorateAsJava.scala b/src/library/scala/collection/convert/DecorateAsJava.scala index 0ed67a86d714..8371804b914d 100644 --- a/src/library/scala/collection/convert/DecorateAsJava.scala +++ b/src/library/scala/collection/convert/DecorateAsJava.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -12,289 +12,97 @@ package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } import Decorators._ -import WrapAsJava._ import scala.language.implicitConversions - -/** A collection of decorators that allow converting between - * Scala and Java collections using `asScala` and `asJava` methods. - * - * The following conversions are supported via `asJava`, `asScala` - *{{{ - * scala.collection.Iterable <=> java.lang.Iterable - * scala.collection.Iterator <=> java.util.Iterator - * scala.collection.mutable.Buffer <=> java.util.List - * scala.collection.mutable.Set <=> java.util.Set - * scala.collection.mutable.Map <=> java.util.Map - * scala.collection.mutable.concurrent.Map <=> java.util.concurrent.ConcurrentMap - *}}} - * In all cases, converting from a source type to a target type and back - * again will return the original source object, e.g. - * {{{ - * import scala.collection.JavaConverters._ - * - * val sl = new scala.collection.mutable.ListBuffer[Int] - * val jl : java.util.List[Int] = sl.asJava - * val sl2 : scala.collection.mutable.Buffer[Int] = jl.asScala - * assert(sl eq sl2) - * }}} - * The following conversions are also supported, but the - * direction from Scala to Java is done by the more specifically named methods: - * `asJavaCollection`, `asJavaEnumeration`, `asJavaDictionary`. - *{{{ - * scala.collection.Iterable <=> java.util.Collection - * scala.collection.Iterator <=> java.util.Enumeration - * scala.collection.mutable.Map <=> java.util.Dictionary - *}}} - * In addition, the following one way conversions are provided via `asJava`: - *{{{ - * scala.collection.Seq => java.util.List - * scala.collection.mutable.Seq => java.util.List - * scala.collection.Set => java.util.Set - * scala.collection.Map => java.util.Map - *}}} - * The following one way conversion is provided via `asScala`: - *{{{ - * java.util.Properties => scala.collection.mutable.Map - *}}} - * @since 2.8.1 - */ -trait DecorateAsJava { +/** Defines `asJava` extension methods for [[JavaConverters]]. */ +trait DecorateAsJava extends AsJavaConverters { /** - * Adds an `asJava` method that implicitly converts a Scala `Iterator` to a - * Java `Iterator`. The returned Java `Iterator` is backed by the provided Scala - * `Iterator` and any side-effects of using it via the Java interface will - * be visible via the Scala interface and vice versa. - * - * If the Scala `Iterator` was previously obtained from an implicit or explicit - * call of `asIterator(java.util.Iterator)` then the original Java `Iterator` - * will be returned by the `asJava` method. - * - * @param i The `Iterator` to be converted. - * @return An object with an `asJava` method that returns a Java `Iterator` view of the argument. + * Adds an `asJava` method that implicitly converts a Scala `Iterator` to a Java `Iterator`. + * See [[asJavaIterator]]. */ implicit def asJavaIteratorConverter[A](i : Iterator[A]): AsJava[ju.Iterator[A]] = new AsJava(asJavaIterator(i)) /** - * Adds an `asJavaEnumeration` method that implicitly converts a Scala - * `Iterator` to a Java `Enumeration`. The returned Java `Enumeration` is - * backed by the provided Scala `Iterator` and any side-effects of using - * it via the Java interface will be visible via the Scala interface and - * vice versa. - * - * If the Scala `Iterator` was previously obtained from an implicit or - * explicit call of `asIterator(java.util.Enumeration)` then the - * original Java `Enumeration` will be returned. - * - * @param i The `Iterator` to be converted. - * @return An object with an `asJavaEnumeration` method that returns a Java - * `Enumeration` view of the argument. + * Adds an `asJavaEnumeration` method that implicitly converts a Scala `Iterator` to a Java + * `Enumeration`. See [[asJavaEnumeration]]. */ implicit def asJavaEnumerationConverter[A](i : Iterator[A]): AsJavaEnumeration[A] = new AsJavaEnumeration(i) /** - * Adds an `asJava` method that implicitly converts a Scala `Iterable` to - * a Java `Iterable`. - * - * The returned Java `Iterable` is backed by the provided Scala `Iterable` - * and any side-effects of using it via the Java interface will be visible - * via the Scala interface and vice versa. - * - * If the Scala `Iterable` was previously obtained from an implicit or - * explicit call of `asIterable(java.lang.Iterable)` then the original - * Java `Iterable` will be returned. - * - * @param i The `Iterable` to be converted. - * @return An object with an `asJavaCollection` method that returns a Java - * `Iterable` view of the argument. + * Adds an `asJava` method that implicitly converts a Scala `Iterable` to a Java `Iterable`. + * See [[asJavaIterable]]. */ implicit def asJavaIterableConverter[A](i : Iterable[A]): AsJava[jl.Iterable[A]] = new AsJava(asJavaIterable(i)) /** - * Adds an `asJavaCollection` method that implicitly converts a Scala - * `Iterable` to an immutable Java `Collection`. - * - * If the Scala `Iterable` was previously obtained from an implicit or - * explicit call of `asSizedIterable(java.util.Collection)` then the - * original Java `Collection` will be returned. - * - * @param i The `SizedIterable` to be converted. - * @return An object with an `asJava` method that returns a Java - * `Collection` view of the argument. + * Adds an `asJavaCollection` method that implicitly converts a Scala `Iterable` to an immutable + * Java `Collection`. See [[asJavaCollection]]. */ implicit def asJavaCollectionConverter[A](i : Iterable[A]): AsJavaCollection[A] = new AsJavaCollection(i) /** - * Adds an `asJava` method that implicitly converts a Scala mutable `Buffer` - * to a Java `List`. - * - * The returned Java `List` is backed by the provided Scala `Buffer` and any - * side-effects of using it via the Java interface will be visible via the - * Scala interface and vice versa. - * - * If the Scala `Buffer` was previously obtained from an implicit or explicit - * call of `asBuffer(java.util.List)` then the original Java `List` will be - * returned. - * - * @param b The `Buffer` to be converted. - * @return An object with an `asJava` method that returns a Java `List` view - * of the argument. + * Adds an `asJava` method that implicitly converts a Scala mutable `Buffer` to a Java `List`. + * See [[bufferAsJavaList]]. */ implicit def bufferAsJavaListConverter[A](b : mutable.Buffer[A]): AsJava[ju.List[A]] = new AsJava(bufferAsJavaList(b)) /** - * Adds an `asJava` method that implicitly converts a Scala mutable `Seq` - * to a Java `List`. - * - * The returned Java `List` is backed by the provided Scala `Seq` and any - * side-effects of using it via the Java interface will be visible via the - * Scala interface and vice versa. - * - * If the Scala `Seq` was previously obtained from an implicit or explicit - * call of `asSeq(java.util.List)` then the original Java `List` will be - * returned. - * - * @param b The `Seq` to be converted. - * @return An object with an `asJava` method that returns a Java `List` - * view of the argument. + * Adds an `asJava` method that implicitly converts a Scala mutable `Seq` to a Java `List`. + * See [[mutableSeqAsJavaList]]. */ implicit def mutableSeqAsJavaListConverter[A](b : mutable.Seq[A]): AsJava[ju.List[A]] = new AsJava(mutableSeqAsJavaList(b)) /** - * Adds an `asJava` method that implicitly converts a Scala `Seq` to a - * Java `List`. - * - * The returned Java `List` is backed by the provided Scala `Seq` and any - * side-effects of using it via the Java interface will be visible via the - * Scala interface and vice versa. - * - * If the Scala `Seq` was previously obtained from an implicit or explicit - * call of `asSeq(java.util.List)` then the original Java `List` will be - * returned. - * - * @param b The `Seq` to be converted. - * @return An object with an `asJava` method that returns a Java `List` - * view of the argument. + * Adds an `asJava` method that implicitly converts a Scala `Seq` to a Java `List`. + * See [[seqAsJavaList]]. */ implicit def seqAsJavaListConverter[A](b : Seq[A]): AsJava[ju.List[A]] = new AsJava(seqAsJavaList(b)) /** - * Adds an `asJava` method that implicitly converts a Scala mutable `Set`> - * to a Java `Set`. - * - * The returned Java `Set` is backed by the provided Scala `Set` and any - * side-effects of using it via the Java interface will be visible via - * the Scala interface and vice versa. - * - * If the Scala `Set` was previously obtained from an implicit or explicit - * call of `asSet(java.util.Set)` then the original Java `Set` will be - * returned. - * - * @param s The `Set` to be converted. - * @return An object with an `asJava` method that returns a Java `Set` view - * of the argument. + * Adds an `asJava` method that implicitly converts a Scala mutable `Set` to a Java `Set`. + * See [[mutableSetAsJavaSet]]. */ implicit def mutableSetAsJavaSetConverter[A](s : mutable.Set[A]): AsJava[ju.Set[A]] = new AsJava(mutableSetAsJavaSet(s)) /** - * Adds an `asJava` method that implicitly converts a Scala `Set` to a - * Java `Set`. - * - * The returned Java `Set` is backed by the provided Scala `Set` and any - * side-effects of using it via the Java interface will be visible via - * the Scala interface and vice versa. - * - * If the Scala `Set` was previously obtained from an implicit or explicit - * call of `asSet(java.util.Set)` then the original Java `Set` will be - * returned. - * - * @param s The `Set` to be converted. - * @return An object with an `asJava` method that returns a Java `Set` view - * of the argument. + * Adds an `asJava` method that implicitly converts a Scala `Set` to a Java `Set`. + * See [[setAsJavaSet]]. */ implicit def setAsJavaSetConverter[A](s : Set[A]): AsJava[ju.Set[A]] = new AsJava(setAsJavaSet(s)) /** - * Adds an `asJava` method that implicitly converts a Scala mutable `Map` - * to a Java `Map`. - * - * The returned Java `Map` is backed by the provided Scala `Map` and any - * side-effects of using it via the Java interface will be visible via the - * Scala interface and vice versa. - * - * If the Scala `Map` was previously obtained from an implicit or explicit - * call of `asMap(java.util.Map)` then the original Java `Map` will be - * returned. - * - * @param m The `Map` to be converted. - * @return An object with an `asJava` method that returns a Java `Map` view - * of the argument. + * Adds an `asJava` method that implicitly converts a Scala mutable `Map` to a Java `Map`. + * See [[mutableMapAsJavaMap]]. */ implicit def mutableMapAsJavaMapConverter[A, B](m : mutable.Map[A, B]): AsJava[ju.Map[A, B]] = new AsJava(mutableMapAsJavaMap(m)) /** - * Adds an `asJavaDictionary` method that implicitly converts a Scala - * mutable `Map` to a Java `Dictionary`. - * - * The returned Java `Dictionary` is backed by the provided Scala - * `Dictionary` and any side-effects of using it via the Java interface - * will be visible via the Scala interface and vice versa. - * - * If the Scala `Dictionary` was previously obtained from an implicit or - * explicit call of `asMap(java.util.Dictionary)` then the original - * Java `Dictionary` will be returned. - * - * @param m The `Map` to be converted. - * @return An object with an `asJavaDictionary` method that returns a - * Java `Dictionary` view of the argument. + * Adds an `asJavaDictionary` method that implicitly converts a Scala mutable `Map` to a Java + * `Dictionary`. See [[asJavaDictionary]]. */ implicit def asJavaDictionaryConverter[A, B](m : mutable.Map[A, B]): AsJavaDictionary[A, B] = new AsJavaDictionary(m) /** - * Adds an `asJava` method that implicitly converts a Scala `Map` to - * a Java `Map`. - * - * The returned Java `Map` is backed by the provided Scala `Map` and any - * side-effects of using it via the Java interface will be visible via - * the Scala interface and vice versa. - * - * If the Scala `Map` was previously obtained from an implicit or explicit - * call of `asMap(java.util.Map)` then the original Java `Map` will be - * returned. - * - * @param m The `Map` to be converted. - * @return An object with an `asJava` method that returns a Java `Map` view - * of the argument. + * Adds an `asJava` method that implicitly converts a Scala `Map` to a Java `Map`. + * See [[mapAsJavaMap]]. */ implicit def mapAsJavaMapConverter[A, B](m : Map[A, B]): AsJava[ju.Map[A, B]] = new AsJava(mapAsJavaMap(m)) /** - * Adds an `asJava` method that implicitly converts a Scala mutable - * `concurrent.Map` to a Java `ConcurrentMap`. - * - * The returned Java `ConcurrentMap` is backed by the provided Scala - * `concurrent.Map` and any side-effects of using it via the Java interface - * will be visible via the Scala interface and vice versa. - * - * If the Scala `concurrent.Map` was previously obtained from an implicit or - * explicit call of `asConcurrentMap(java.util.concurrent.ConcurrentMap)` - * then the original Java `ConcurrentMap` will be returned. - * - * @param m The Scala `concurrent.Map` to be converted. - * @return An object with an `asJava` method that returns a Java - * `ConcurrentMap` view of the argument. + * Adds an `asJava` method that implicitly converts a Scala mutable `concurrent.Map` to a Java + * `ConcurrentMap`. See [[mapAsJavaConcurrentMap]]. */ implicit def mapAsJavaConcurrentMapConverter[A, B](m: concurrent.Map[A, B]): AsJava[juc.ConcurrentMap[A, B]] = new AsJava(mapAsJavaConcurrentMap(m)) diff --git a/src/library/scala/collection/convert/DecorateAsScala.scala b/src/library/scala/collection/convert/DecorateAsScala.scala index 5448f5f91c8d..b74a06ece5b1 100644 --- a/src/library/scala/collection/convert/DecorateAsScala.scala +++ b/src/library/scala/collection/convert/DecorateAsScala.scala @@ -1,6 +1,6 @@ /* __ *\ ** ________ ___ / / ___ Scala API ** -** / __/ __// _ | / / / _ | (c) 2006-2013, LAMP/EPFL ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** ** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** ** /____/\___/_/ |_/____/_/ | | ** ** |/ ** @@ -12,185 +12,76 @@ package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } import Decorators._ -import WrapAsScala._ import scala.language.implicitConversions -trait DecorateAsScala { +/** Defines `asScala` extension methods for [[JavaConverters]]. */ +trait DecorateAsScala extends AsScalaConverters { /** - * Adds an `asScala` method that implicitly converts a Java `Iterator` to - * a Scala `Iterator`. - * - * The returned Scala `Iterator` is backed by the provided Java `Iterator` - * and any side-effects of using it via the Scala interface will be visible - * via the Java interface and vice versa. - * - * If the Java `Iterator` was previously obtained from an implicit or - * explicit call of `asIterator(scala.collection.Iterator)` then the - * original Scala `Iterator` will be returned. - * - * @param i The `Iterator` to be converted. - * @return An object with an `asScala` method that returns a Scala - * `Iterator` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Iterator` to a Scala `Iterator`. + * See [[asScalaIterator]]. */ implicit def asScalaIteratorConverter[A](i : ju.Iterator[A]): AsScala[Iterator[A]] = new AsScala(asScalaIterator(i)) /** - * Adds an `asScala` method that implicitly converts a Java `Enumeration` - * to a Scala `Iterator`. - * - * The returned Scala `Iterator` is backed by the provided Java - * `Enumeration` and any side-effects of using it via the Scala interface - * will be visible via the Java interface and vice versa. - * - * If the Java `Enumeration` was previously obtained from an implicit or - * explicit call of `asEnumeration(scala.collection.Iterator)` then the - * original Scala `Iterator` will be returned. - * - * @param i The `Enumeration` to be converted. - * @return An object with an `asScala` method that returns a Scala - * `Iterator` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Enumeration` to a Scala `Iterator`. + * See [[enumerationAsScalaIterator]]. */ implicit def enumerationAsScalaIteratorConverter[A](i : ju.Enumeration[A]): AsScala[Iterator[A]] = new AsScala(enumerationAsScalaIterator(i)) /** - * Adds an `asScala` method that implicitly converts a Java `Iterable` to - * a Scala `Iterable`. - * - * The returned Scala `Iterable` is backed by the provided Java `Iterable` - * and any side-effects of using it via the Scala interface will be visible - * via the Java interface and vice versa. - * - * If the Java `Iterable` was previously obtained from an implicit or - * explicit call of `asIterable(scala.collection.Iterable)` then the original - * Scala `Iterable` will be returned. - * - * @param i The `Iterable` to be converted. - * @return An object with an `asScala` method that returns a Scala `Iterable` - * view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Iterable` to a Scala `Iterable`. + * See [[iterableAsScalaIterable]]. */ implicit def iterableAsScalaIterableConverter[A](i : jl.Iterable[A]): AsScala[Iterable[A]] = new AsScala(iterableAsScalaIterable(i)) /** - * Adds an `asScala` method that implicitly converts a Java `Collection` to - * an Scala `Iterable`. - * - * If the Java `Collection` was previously obtained from an implicit or - * explicit call of `asCollection(scala.collection.SizedIterable)` then - * the original Scala `SizedIterable` will be returned. - * - * @param i The `Collection` to be converted. - * @return An object with an `asScala` method that returns a Scala - * `SizedIterable` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Collection` to an Scala `Iterable`. + * See [[collectionAsScalaIterable]]. */ implicit def collectionAsScalaIterableConverter[A](i : ju.Collection[A]): AsScala[Iterable[A]] = new AsScala(collectionAsScalaIterable(i)) /** - * Adds an `asScala` method that implicitly converts a Java `List` to a - * Scala mutable `Buffer`. - * - * The returned Scala `Buffer` is backed by the provided Java `List` and - * any side-effects of using it via the Scala interface will be visible via - * the Java interface and vice versa. - * - * If the Java `List` was previously obtained from an implicit or explicit - * call of `asList(scala.collection.mutable.Buffer)` then the original - * Scala `Buffer` will be returned. - * - * @param l The `List` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `Buffer` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `List` to a Scala mutable `Buffer`. + * See [[asScalaBuffer]]. */ implicit def asScalaBufferConverter[A](l : ju.List[A]): AsScala[mutable.Buffer[A]] = new AsScala(asScalaBuffer(l)) /** - * Adds an `asScala` method that implicitly converts a Java `Set` to a - * Scala mutable `Set`. - * - * The returned Scala `Set` is backed by the provided Java `Set` and any - * side-effects of using it via the Scala interface will be visible via - * the Java interface and vice versa. - * - * If the Java `Set` was previously obtained from an implicit or explicit - * call of `asSet(scala.collection.mutable.Set)` then the original - * Scala `Set` will be returned. - * - * @param s The `Set` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `Set` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Set` to a Scala mutable `Set`. + * See [[asScalaSet]]. */ implicit def asScalaSetConverter[A](s : ju.Set[A]): AsScala[mutable.Set[A]] = new AsScala(asScalaSet(s)) /** - * Adds an `asScala` method that implicitly converts a Java `Map` to a Scala - * mutable `Map`. The returned Scala `Map` is backed by the provided Java - * `Map` and any side-effects of using it via the Scala interface will - * be visible via the Java interface and vice versa. - * - * If the Java `Map` was previously obtained from an implicit or explicit - * call of `asMap(scala.collection.mutable.Map)` then the original - * Scala `Map` will be returned. - * - * If the wrapped map is synchronized (e.g. from `java.util.Collections.synchronizedMap`), - * it is your responsibility to wrap all - * non-atomic operations with `underlying.synchronized`. - * This includes `get`, as `java.util.Map`'s API does not allow for an - * atomic `get` when `null` values may be present. - * - * @param m The `Map` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `Map` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Map` to a Scala mutable `Map`. + * See [[mapAsScalaMap]]. */ implicit def mapAsScalaMapConverter[A, B](m : ju.Map[A, B]): AsScala[mutable.Map[A, B]] = new AsScala(mapAsScalaMap(m)) /** - * Adds an `asScala` method that implicitly converts a Java `ConcurrentMap` - * to a Scala mutable `concurrent.Map`. The returned Scala `concurrent.Map` is - * backed by the provided Java `ConcurrentMap` and any side-effects of using - * it via the Scala interface will be visible via the Java interface and - * vice versa. - * - * If the Java `ConcurrentMap` was previously obtained from an implicit or - * explicit call of `mapAsScalaConcurrentMap(scala.collection.mutable.ConcurrentMap)` - * then the original Scala `concurrent.Map` will be returned. - * - * @param m The `ConcurrentMap` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `concurrent.Map` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `ConcurrentMap` to a Scala mutable + * `concurrent.Map`. See [[mapAsScalaConcurrentMap]]. */ implicit def mapAsScalaConcurrentMapConverter[A, B](m: juc.ConcurrentMap[A, B]): AsScala[concurrent.Map[A, B]] = new AsScala(mapAsScalaConcurrentMap(m)) /** - * Adds an `asScala` method that implicitly converts a Java `Dictionary` - * to a Scala mutable `Map[String, String]`. The returned Scala - * `Map[String, String]` is backed by the provided Java `Dictionary` and - * any side-effects of using it via the Scala interface will be visible via - * the Java interface and vice versa. - * - * @param p The `Dictionary` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `Map[String, String]` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Dictionary` to a Scala mutable `Map`. + * See [[dictionaryAsScalaMap]]. */ implicit def dictionaryAsScalaMapConverter[A, B](p: ju.Dictionary[A, B]): AsScala[mutable.Map[A, B]] = new AsScala(dictionaryAsScalaMap(p)) /** - * Adds an `asScala` method that implicitly converts a Java `Properties` - * to a Scala mutable `Map[String, String]`. The returned Scala - * `Map[String, String]` is backed by the provided Java `Properties` and - * any side-effects of using it via the Scala interface will be visible via - * the Java interface and vice versa. - * - * @param p The `Properties` to be converted. - * @return An object with an `asScala` method that returns a Scala mutable - * `Map[String, String]` view of the argument. + * Adds an `asScala` method that implicitly converts a Java `Properties` to a Scala mutable + * `Map[String, String]`. See [[propertiesAsScalaMap]]. */ implicit def propertiesAsScalaMapConverter(p: ju.Properties): AsScala[mutable.Map[String, String]] = new AsScala(propertiesAsScalaMap(p)) diff --git a/src/library/scala/collection/convert/Decorators.scala b/src/library/scala/collection/convert/Decorators.scala index d232fa04e18f..3e45a0225430 100644 --- a/src/library/scala/collection/convert/Decorators.scala +++ b/src/library/scala/collection/convert/Decorators.scala @@ -12,7 +12,7 @@ package convert import java.{ util => ju } -private[collection] trait Decorators { +private[collection] object Decorators { /** Generic class containing the `asJava` converter method */ class AsJava[A](op: => A) { /** Converts a Scala collection to the corresponding Java collection */ @@ -28,20 +28,18 @@ private[collection] trait Decorators { /** Generic class containing the `asJavaCollection` converter method */ class AsJavaCollection[A](i: Iterable[A]) { /** Converts a Scala `Iterable` to a Java `Collection` */ - def asJavaCollection: ju.Collection[A] = JavaConversions.asJavaCollection(i) + def asJavaCollection: ju.Collection[A] = JavaConverters.asJavaCollection(i) } /** Generic class containing the `asJavaEnumeration` converter method */ class AsJavaEnumeration[A](i: Iterator[A]) { /** Converts a Scala `Iterator` to a Java `Enumeration` */ - def asJavaEnumeration: ju.Enumeration[A] = JavaConversions.asJavaEnumeration(i) + def asJavaEnumeration: ju.Enumeration[A] = JavaConverters.asJavaEnumeration(i) } /** Generic class containing the `asJavaDictionary` converter method */ class AsJavaDictionary[A, B](m : mutable.Map[A, B]) { /** Converts a Scala `Map` to a Java `Dictionary` */ - def asJavaDictionary: ju.Dictionary[A, B] = JavaConversions.asJavaDictionary(m) + def asJavaDictionary: ju.Dictionary[A, B] = JavaConverters.asJavaDictionary(m) } } - -private[collection] object Decorators extends Decorators diff --git a/src/library/scala/collection/convert/ImplicitConversions.scala b/src/library/scala/collection/convert/ImplicitConversions.scala new file mode 100644 index 000000000000..747e0606c838 --- /dev/null +++ b/src/library/scala/collection/convert/ImplicitConversions.scala @@ -0,0 +1,125 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2016, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://www.scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +package scala +package collection +package convert + +import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } +import scala.language.implicitConversions + +import JavaConverters._ + +/** Defines implicit converter methods from Java to Scala collections. */ +trait ToScalaImplicits { + /** Implicitly converts a Java `Iterator` to a Scala `Iterator`. See [[asScalaIterator]]. */ + implicit def `iterator asScala`[A](it: ju.Iterator[A]): Iterator[A] = asScalaIterator(it) + + /** Implicitly converts a Java `Enumeration` to a Scala `Iterator`. See [[enumerationAsScalaIterator]]. */ + implicit def `enumeration AsScalaIterator`[A](i: ju.Enumeration[A]): Iterator[A] = enumerationAsScalaIterator(i) + + /** Implicitly converts a Java `Iterable` to a Scala `Iterable`. See [[iterableAsScalaIterable]]. */ + implicit def `iterable AsScalaIterable`[A](i: jl.Iterable[A]): Iterable[A] = iterableAsScalaIterable(i) + + /** Implicitly converts a Java `Collection` to an Scala `Iterable`. See [[collectionAsScalaIterable]]. */ + implicit def `collection AsScalaIterable`[A](i: ju.Collection[A]): Iterable[A] = collectionAsScalaIterable(i) + + /** Implicitly converts a Java `List` to a Scala mutable `Buffer`. See [[asScalaBuffer]]. */ + implicit def `list asScalaBuffer`[A](l: ju.List[A]): mutable.Buffer[A] = asScalaBuffer(l) + + /** Implicitly converts a Java `Set` to a Scala mutable `Set`. See [[asScalaSet]]. */ + implicit def `set asScala`[A](s: ju.Set[A]): mutable.Set[A] = asScalaSet(s) + + /** Implicitly converts a Java `Map` to a Scala mutable `Map`. See [[mapAsScalaMap]]. */ + implicit def `map AsScala`[A, B](m: ju.Map[A, B]): mutable.Map[A, B] = mapAsScalaMap(m) + + /** Implicitly converts a Java `ConcurrentMap` to a Scala mutable `ConcurrentMap`. See [[mapAsScalaConcurrentMap]]. */ + implicit def `map AsScalaConcurrentMap`[A, B](m: juc.ConcurrentMap[A, B]): concurrent.Map[A, B] = mapAsScalaConcurrentMap(m) + + /** Implicitly converts a Java `Dictionary` to a Scala mutable `Map`. See [[dictionaryAsScalaMap]]. */ + implicit def `dictionary AsScalaMap`[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = dictionaryAsScalaMap(p) + + /** Implicitly converts a Java `Properties` to a Scala `mutable Map[String, String]`. See [[propertiesAsScalaMap]]. */ + implicit def `properties AsScalaMap`(p: ju.Properties): mutable.Map[String, String] = propertiesAsScalaMap(p) +} + +/** Defines implicit conversions from Scala to Java collections. */ +trait ToJavaImplicits { + /** Implicitly converts a Scala `Iterator` to a Java `Iterator`. See [[asJavaIterator]]. */ + implicit def `iterator asJava`[A](it: Iterator[A]): ju.Iterator[A] = asJavaIterator(it) + + /** Implicitly converts a Scala `Iterator` to a Java `Enumeration`. See [[asJavaEnumeration]]. */ + implicit def `enumeration asJava`[A](it: Iterator[A]): ju.Enumeration[A] = asJavaEnumeration(it) + + /** Implicitly converts a Scala `Iterable` to a Java `Iterable`. See [[asJavaIterable]]. */ + implicit def `iterable asJava`[A](i: Iterable[A]): jl.Iterable[A] = asJavaIterable(i) + + /** Implicitly converts a Scala `Iterable` to an immutable Java `Collection`. See [[asJavaCollection]]. */ + implicit def `collection asJava`[A](it: Iterable[A]): ju.Collection[A] = asJavaCollection(it) + + /** Implicitly converts a Scala mutable `Buffer` to a Java `List`. See [[bufferAsJavaList]]. */ + implicit def `buffer AsJavaList`[A](b: mutable.Buffer[A]): ju.List[A] = bufferAsJavaList(b) + + /** Implicitly converts a Scala mutable `Seq` to a Java `List`. See [[mutableSeqAsJavaList]]. */ + implicit def `mutableSeq AsJavaList`[A](seq: mutable.Seq[A]): ju.List[A] = mutableSeqAsJavaList(seq) + + /** Implicitly converts a Scala `Seq` to a Java `List`. See [[seqAsJavaList]]. */ + implicit def `seq AsJavaList`[A](seq: Seq[A]): ju.List[A] = seqAsJavaList(seq) + + /** Implicitly converts a Scala mutable `Set` to a Java `Set`. See [[mutableSetAsJavaSet]]. */ + implicit def `mutableSet AsJavaSet`[A](s: mutable.Set[A]): ju.Set[A] = mutableSetAsJavaSet(s) + + /** Implicitly converts a Scala `Set` to a Java `Set`. See [[setAsJavaSet]]. */ + implicit def `set AsJavaSet`[A](s: Set[A]): ju.Set[A] = setAsJavaSet(s) + + /** Implicitly converts a Scala mutable `Map` to a Java `Map`. See [[mutableMapAsJavaMap]]. */ + implicit def `mutableMap AsJavaMap`[A, B](m: mutable.Map[A, B]): ju.Map[A, B] = mutableMapAsJavaMap(m) + + /** Implicitly converts a Scala mutable `Map` to a Java `Dictionary`. See [[asJavaDictionary]]. */ + implicit def `dictionary asJava`[A, B](m: mutable.Map[A, B]): ju.Dictionary[A, B] = asJavaDictionary(m) + + /** Implicitly converts a Scala `Map` to a Java `Map`. See [[mapAsJavaMap]]. */ + implicit def `map AsJavaMap`[A, B](m: Map[A, B]): ju.Map[A, B] = mapAsJavaMap(m) + + /** Implicitly converts a Scala mutable `concurrent.Map` to a Java `ConcurrentMap`. See [[mapAsJavaConcurrentMap]]. */ + implicit def `map AsJavaConcurrentMap`[A, B](m: concurrent.Map[A, B]): juc.ConcurrentMap[A, B] = mapAsJavaConcurrentMap(m) +} + +/** + * Convenience for miscellaneous implicit conversions from Scala to Java collections API. + * + * It is recommended to use explicit conversions provided by [[collection.JavaConverters]] instead. + * Implicit conversions may cause unexpected issues, see [[ImplicitConversions]]. + */ +object ImplicitConversionsToJava extends ToJavaImplicits + +/** + * Convenience for miscellaneous implicit conversions from Java to Scala collections API. + * + * It is recommended to use explicit conversions provided by [[collection.JavaConverters]] instead. + * Implicit conversions may cause unexpected issues, see [[ImplicitConversions]]. + */ +object ImplicitConversionsToScala extends ToScalaImplicits + +/** + * Convenience for miscellaneous implicit conversions between Java and Scala collections API. + * + * It is recommended to use explicit conversions provided by [[collection.JavaConverters]] instead. + * Implicit conversions may cause unexpected issues. Example: + * + * {{{ + * import collection.convert.ImplicitConversions._ + * case class StringBox(s: String) + * val m = Map(StringBox("one") -> "uno") + * m.get("one") + * }}} + * + * The above example returns `null` instead of producing a type error at compile-time. The map is + * implicitly converted to a `java.util.Map` which provides a method `get(x: AnyRef)`. + */ +object ImplicitConversions extends ToScalaImplicits with ToJavaImplicits diff --git a/src/library/scala/collection/convert/WrapAsJava.scala b/src/library/scala/collection/convert/WrapAsJava.scala index e97a2ff1fcd2..e45c1666a5ed 100644 --- a/src/library/scala/collection/convert/WrapAsJava.scala +++ b/src/library/scala/collection/convert/WrapAsJava.scala @@ -13,7 +13,27 @@ package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } import scala.language.implicitConversions -trait WrapAsJava { +@deprecated("Use JavaConverters or consider ToJavaImplicits", since="2.12") +trait WrapAsJava extends LowPriorityWrapAsJava { + // provide higher-priority implicits with names that don't exist in JavaConverters for the case + // when importing both JavaConverters._ and JavaConversions._. otherwise implicit conversions + // would not apply, see https://github.com/scala/scala/pull/5109#issuecomment-212417789 + implicit def `deprecated asJavaIterator`[A](it: Iterator[A]): ju.Iterator[A] = asJavaIterator(it) + implicit def `deprecated asJavaEnumeration`[A](it: Iterator[A]): ju.Enumeration[A] = asJavaEnumeration(it) + implicit def `deprecated asJavaIterable`[A](i: Iterable[A]): jl.Iterable[A] = asJavaIterable(i) + implicit def `deprecated asJavaCollection`[A](it: Iterable[A]): ju.Collection[A] = asJavaCollection(it) + implicit def `deprecated bufferAsJavaList`[A](b: mutable.Buffer[A]): ju.List[A] = bufferAsJavaList(b) + implicit def `deprecated mutableSeqAsJavaList`[A](seq: mutable.Seq[A]): ju.List[A] = mutableSeqAsJavaList(seq) + implicit def `deprecated seqAsJavaList`[A](seq: Seq[A]): ju.List[A] = seqAsJavaList(seq) + implicit def `deprecated mutableSetAsJavaSet`[A](s: mutable.Set[A]): ju.Set[A] = mutableSetAsJavaSet(s) + implicit def `deprecated setAsJavaSet`[A](s: Set[A]): ju.Set[A] = setAsJavaSet(s) + implicit def `deprecated mutableMapAsJavaMap`[A, B](m: mutable.Map[A, B]): ju.Map[A, B] = mutableMapAsJavaMap(m) + implicit def `deprecated asJavaDictionary`[A, B](m: mutable.Map[A, B]): ju.Dictionary[A, B] = asJavaDictionary(m) + implicit def `deprecated mapAsJavaMap`[A, B](m: Map[A, B]): ju.Map[A, B] = mapAsJavaMap(m) + implicit def `deprecated mapAsJavaConcurrentMap`[A, B](m: concurrent.Map[A, B]): juc.ConcurrentMap[A, B] = mapAsJavaConcurrentMap(m) +} + +private[convert] trait LowPriorityWrapAsJava { import Wrappers._ /** @@ -266,4 +286,5 @@ trait WrapAsJava { } } -object WrapAsJava extends WrapAsJava { } +@deprecated("Use JavaConverters or consider ImplicitConversionsToJava", since="2.12") +object WrapAsJava extends WrapAsJava diff --git a/src/library/scala/collection/convert/WrapAsScala.scala b/src/library/scala/collection/convert/WrapAsScala.scala index ee161dcc878a..514490e34856 100644 --- a/src/library/scala/collection/convert/WrapAsScala.scala +++ b/src/library/scala/collection/convert/WrapAsScala.scala @@ -13,8 +13,26 @@ package convert import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } import scala.language.implicitConversions -trait WrapAsScala { +@deprecated("Use JavaConverters or consider ToScalaImplicits", since="2.12") +trait WrapAsScala extends LowPriorityWrapAsScala { + // provide higher-priority implicits with names that don't exist in JavaConverters for the case + // when importing both JavaConverters._ and JavaConversions._. otherwise implicit conversions + // would not apply, see https://github.com/scala/scala/pull/5109#issuecomment-212417789 + implicit def `deprecated asScalaIterator`[A](it: ju.Iterator[A]): Iterator[A] = asScalaIterator(it) + implicit def `deprecated enumerationAsScalaIterator`[A](i: ju.Enumeration[A]): Iterator[A] = enumerationAsScalaIterator(i) + implicit def `deprecated iterableAsScalaIterable`[A](i: jl.Iterable[A]): Iterable[A] = iterableAsScalaIterable(i) + implicit def `deprecated collectionAsScalaIterable`[A](i: ju.Collection[A]): Iterable[A] = collectionAsScalaIterable(i) + implicit def `deprecated asScalaBuffer`[A](l: ju.List[A]): mutable.Buffer[A] = asScalaBuffer(l) + implicit def `deprecated asScalaSet`[A](s: ju.Set[A]): mutable.Set[A] = asScalaSet(s) + implicit def `deprecated mapAsScalaMap`[A, B](m: ju.Map[A, B]): mutable.Map[A, B] = mapAsScalaMap(m) + implicit def `deprecated mapAsScalaConcurrentMap`[A, B](m: juc.ConcurrentMap[A, B]): concurrent.Map[A, B] = mapAsScalaConcurrentMap(m) + implicit def `deprecated dictionaryAsScalaMap`[A, B](p: ju.Dictionary[A, B]): mutable.Map[A, B] = dictionaryAsScalaMap(p) + implicit def `deprecated propertiesAsScalaMap`(p: ju.Properties): mutable.Map[String, String] = propertiesAsScalaMap(p) +} + +private[convert] trait LowPriorityWrapAsScala { import Wrappers._ + /** * Implicitly converts a Java `Iterator` to a Scala `Iterator`. * @@ -207,4 +225,5 @@ trait WrapAsScala { } } -object WrapAsScala extends WrapAsScala { } +@deprecated("Use JavaConverters or consider ImplicitConversionsToScala", since="2.12") +object WrapAsScala extends WrapAsScala diff --git a/src/library/scala/collection/convert/Wrappers.scala b/src/library/scala/collection/convert/Wrappers.scala index 3edc5ba1b425..6d745bc6b0c0 100644 --- a/src/library/scala/collection/convert/Wrappers.scala +++ b/src/library/scala/collection/convert/Wrappers.scala @@ -14,10 +14,7 @@ import java.{ lang => jl, util => ju }, java.util.{ concurrent => juc } import WrapAsScala._ import WrapAsJava._ -/** Don't put the implementations in the same scope as the implicits - * which utilize them, or they will stow away into every scope which - * extends one of those implementations. See SI-5580. - */ +/** Adapters for Java/Scala collections API. */ private[collection] trait Wrappers { trait IterableWrapperTrait[A] extends ju.AbstractCollection[A] { val underlying: Iterable[A] diff --git a/src/library/scala/collection/convert/package.scala b/src/library/scala/collection/convert/package.scala index 13970f9a3ecc..7f48023b58b6 100644 --- a/src/library/scala/collection/convert/package.scala +++ b/src/library/scala/collection/convert/package.scala @@ -10,10 +10,17 @@ package scala package collection package object convert { + @deprecated("Use JavaConverters", since="2.12") val decorateAsJava = new DecorateAsJava { } + @deprecated("Use JavaConverters", since="2.12") val decorateAsScala = new DecorateAsScala { } - val decorateAll = new DecorateAsJava with DecorateAsScala { } + @deprecated("Use JavaConverters", since="2.12") + val decorateAll = JavaConverters + + @deprecated("Use JavaConverters or consider ImplicitConversionsToJava", since="2.12") val wrapAsJava = new WrapAsJava { } + @deprecated("Use JavaConverters or consider ImplicitConversionsToScala", since="2.12") val wrapAsScala = new WrapAsScala { } + @deprecated("Use JavaConverters or consider ImplicitConversions", since="2.12") val wrapAll = new WrapAsJava with WrapAsScala { } } diff --git a/src/library/scala/collection/mutable/PriorityQueue.scala b/src/library/scala/collection/mutable/PriorityQueue.scala index b4112c03dd62..d5b7673c373c 100644 --- a/src/library/scala/collection/mutable/PriorityQueue.scala +++ b/src/library/scala/collection/mutable/PriorityQueue.scala @@ -200,9 +200,7 @@ sealed class PriorityQueue[A](implicit val ord: Ordering[A]) * @return A reversed priority queue. */ def reverse = { - val revq = new PriorityQueue[A]()(new scala.math.Ordering[A] { - def compare(x: A, y: A) = ord.compare(y, x) - }) + val revq = new PriorityQueue[A]()(ord.reverse) for (i <- 1 until resarr.length) revq += resarr(i) revq } diff --git a/src/library/scala/collection/package.scala b/src/library/scala/collection/package.scala index 856f901b770f..6df254c0e0df 100644 --- a/src/library/scala/collection/package.scala +++ b/src/library/scala/collection/package.scala @@ -76,13 +76,9 @@ package scala * The concrete parallel collections also have specific performance characteristics which are * described in [[http://docs.scala-lang.org/overviews/parallel-collections/concrete-parallel-collections.html#performance-characteristics the parallel collections guide]] * - * === Converting between Java Collections === + * === Converting to and from Java Collections === * - * The [[scala.collection.JavaConversions]] object provides implicit defs that - * will allow mostly seamless integration between APIs using Java Collections - * and the Scala collections library. - * - * Alternatively the [[scala.collection.JavaConverters]] object provides a collection + * The [[scala.collection.JavaConverters]] object provides a collection * of decorators that allow converting between Scala and Java collections using `asScala` * and `asJava` methods. */ diff --git a/src/library/scala/concurrent/Future.scala b/src/library/scala/concurrent/Future.scala index b10aad0eccd1..d9d3d572e8ed 100644 --- a/src/library/scala/concurrent/Future.scala +++ b/src/library/scala/concurrent/Future.scala @@ -29,8 +29,8 @@ import scala.reflect.ClassTag * val f: Future[String] = Future { * s + " future!" * } - * f onSuccess { - * case msg => println(msg) + * f foreach { + * msg => println(msg) * } * }}} * diff --git a/src/library/scala/sys/process/package.scala b/src/library/scala/sys/process/package.scala index 0e9a1bfc5620..ff0fd920c9ec 100644 --- a/src/library/scala/sys/process/package.scala +++ b/src/library/scala/sys/process/package.scala @@ -203,9 +203,9 @@ package scala.sys { package object process extends ProcessImplicits { /** The arguments passed to `java` when creating this process */ def javaVmArguments: List[String] = { - import scala.collection.JavaConversions._ + import scala.collection.JavaConverters._ - java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments().toList + java.lang.management.ManagementFactory.getRuntimeMXBean.getInputArguments.asScala.toList } /** The input stream of this process */ def stdin = java.lang.System.in diff --git a/src/library/scala/util/control/Exception.scala b/src/library/scala/util/control/Exception.scala index 8aa4073a51b3..64f491d7f0b8 100644 --- a/src/library/scala/util/control/Exception.scala +++ b/src/library/scala/util/control/Exception.scala @@ -13,21 +13,136 @@ package control import scala.reflect.{ ClassTag, classTag } import scala.language.implicitConversions - /** Classes representing the components of exception handling. - * Each class is independently composable. Some example usages: + * + * Each class is independently composable. + * + * This class differs from [[scala.util.Try]] in that it focuses on composing exception handlers rather than + * composing behavior. All behavior should be composed first and fed to a [[Catch]] object using one of the + * `opt`, `either` or `withTry` methods. Taken together the classes provide a DSL for composing catch and finally + * behaviors. + * + * === Examples === + * + * Create a `Catch` which handles specified exceptions. * {{{ * import scala.util.control.Exception._ * import java.net._ * * val s = "http://www.scala-lang.org/" - * val x1 = catching(classOf[MalformedURLException]) opt new URL(s) - * val x2 = catching(classOf[MalformedURLException], classOf[NullPointerException]) either new URL(s) + * + * // Some(http://www.scala-lang.org/) + * val x1: Option[URL] = catching(classOf[MalformedURLException]) opt new URL(s) + * + * // Right(http://www.scala-lang.org/) + * val x2: Either[Throwable,URL] = + * catching(classOf[MalformedURLException], classOf[NullPointerException]) either new URL(s) + * + * // Success(http://www.scala-lang.org/) + * val x3: Try[URL] = catching(classOf[MalformedURLException], classOf[NullPointerException]) withTry new URL(s) + * + * val defaultUrl = new URL("http://example.com") + * // URL(http://example.com) because htt/xx throws MalformedURLException + * val x4: URL = failAsValue(classOf[MalformedURLException])(defaultUrl)(new URL("htt/xx")) + * }}} + * + * Create a `Catch` which logs exceptions using `handling` and `by`. + * {{{ + * def log(t: Throwable): Unit = t.printStackTrace + * + * val withThrowableLogging: Catch[Unit] = handling(classOf[MalformedURLException]) by (log) + * + * def printUrl(url: String) : Unit = { + * val con = new URL(url) openConnection() + * val source = scala.io.Source.fromInputStream(con.getInputStream()) + * source.getLines.foreach(println) + * } + * + * val badUrl = "htt/xx" + * // Prints stacktrace, + * // java.net.MalformedURLException: no protocol: htt/xx + * // at java.net.URL.(URL.java:586) + * withThrowableLogging { printUrl(badUrl) } + * + * val goodUrl = "http://www.scala-lang.org/" + * // Prints page content, + * // <!DOCTYPE html> + * // <html> + * withThrowableLogging { printUrl(goodUrl) } + * }}} + * + * Use `unwrapping` to create a `Catch` that unwraps exceptions before rethrowing. + * {{{ + * class AppException(cause: Throwable) extends RuntimeException(cause) + * + * val unwrappingCatch: Catch[Nothing] = unwrapping(classOf[AppException]) + * + * def calcResult: Int = throw new AppException(new NullPointerException) + * + * // Throws NPE not AppException, + * // java.lang.NullPointerException + * // at .calcResult(<console>:17) + * val result = unwrappingCatch(calcResult) * }}} * - * This class differs from `scala.util.Try` in that it focuses on composing exception handlers rather than - * composing behavior. All behavior should be composed first and fed to a `Catch` object using one of the - * `opt` or `either` methods. + * Use `failAsValue` to provide a default when a specified exception is caught. + * + * {{{ + * val inputDefaulting: Catch[Int] = failAsValue(classOf[NumberFormatException])(0) + * val candidatePick = "seven" // scala.io.StdIn.readLine() + * + * // Int = 0 + * val pick = inputDefaulting(candidatePick.toInt) + * }}} + * + * Compose multiple `Catch`s with `or` to build a `Catch` that provides default values varied by exception. + * {{{ + * val formatDefaulting: Catch[Int] = failAsValue(classOf[NumberFormatException])(0) + * val nullDefaulting: Catch[Int] = failAsValue(classOf[NullPointerException])(-1) + * val otherDefaulting: Catch[Int] = nonFatalCatch withApply(_ => -100) + * + * val combinedDefaulting: Catch[Int] = formatDefaulting or nullDefaulting or otherDefaulting + * + * def p(s: String): Int = s.length * s.toInt + * + * // Int = 0 + * combinedDefaulting(p("tenty-nine")) + * + * // Int = -1 + * combinedDefaulting(p(null: String)) + * + * // Int = -100 + * combinedDefaulting(throw new IllegalStateException) + * + * // Int = 22 + * combinedDefaulting(p("11")) + * }}} + * + * @groupname composition-catch Catch behavior composition + * @groupprio composition-catch 10 + * @groupdesc composition-catch Build Catch objects from exception lists and catch logic + * + * @groupname composition-finally Finally behavior composition + * @groupprio composition-finally 20 + * @groupdesc composition-finally Build Catch objects from finally logic + * + * @groupname canned-behavior General purpose catch objects + * @groupprio canned-behavior 30 + * @groupdesc canned-behavior Catch objects with predefined behavior. Use combinator methods to compose additional behavior. + * + * @groupname dsl DSL behavior composition + * @groupprio dsl 40 + * @groupdesc dsl Expressive Catch behavior composition + * + * @groupname composition-catch-promiscuously Promiscuous Catch behaviors + * @groupprio composition-catch-promiscuously 50 + * @groupdesc composition-catch-promiscuously Useful if catching `ControlThrowable` or `InterruptedException` is required. + * + * @groupname logic-container Logic Containers + * @groupprio logic-container 60 + * @groupdesc logic-container Containers for catch and finally behavior. + * + * @define protectedExceptions `ControlThrowable` or `InterruptedException` * * @author Paul Phillips */ @@ -51,6 +166,7 @@ object Exception { /** !!! Not at all sure of every factor which goes into this, * and/or whether we need multiple standard variations. + * @return true if `x` is $protectedExceptions otherwise false. */ def shouldRethrow(x: Throwable): Boolean = x match { case _: ControlThrowable => true @@ -70,7 +186,9 @@ object Exception { override def toString() = name + "(" + desc + ")" } - /** A container class for finally code. */ + /** A container class for finally code. + * @group logic-container + */ class Finally private[Exception](body: => Unit) extends Described { protected val name = "Finally" @@ -87,6 +205,7 @@ object Exception { * @param pf Partial function used when applying catch logic to determine result value * @param fin Finally logic which if defined will be invoked after catch logic * @param rethrow Predicate on throwables determining when to rethrow a caught [[Throwable]] + * @group logic-container */ class Catch[+T]( val pf: Catcher[T], @@ -153,23 +272,30 @@ object Exception { final def nonFatalCatcher[T]: Catcher[T] = mkThrowableCatcher({ case NonFatal(_) => true; case _ => false }, throw _) final def allCatcher[T]: Catcher[T] = mkThrowableCatcher(_ => true, throw _) - /** The empty `Catch` object. */ + /** The empty `Catch` object. + * @group canned-behavior + **/ final val noCatch: Catch[Nothing] = new Catch(nothingCatcher) withDesc "" - /** A `Catch` object which catches everything. */ + /** A `Catch` object which catches everything. + * @group canned-behavior + **/ final def allCatch[T]: Catch[T] = new Catch(allCatcher[T]) withDesc "" - /** A `Catch` object which catches non-fatal exceptions. */ + /** A `Catch` object which catches non-fatal exceptions. + * @group canned-behavior + **/ final def nonFatalCatch[T]: Catch[T] = new Catch(nonFatalCatcher[T]) withDesc "" /** Creates a `Catch` object which will catch any of the supplied exceptions. * Since the returned `Catch` object has no specific logic defined and will simply - * rethrow the exceptions it catches, you will typically want to call `opt` or - * `either` on the return value, or assign custom logic by calling "withApply". + * rethrow the exceptions it catches, you will typically want to call `opt`, + * `either` or `withTry` on the return value, or assign custom logic by calling "withApply". * * Note that `Catch` objects automatically rethrow `ControlExceptions` and others * which should only be caught in exceptional circumstances. If you really want * to catch exactly what you specify, use `catchingPromiscuously` instead. + * @group composition-catch */ def catching[T](exceptions: Class[_]*): Catch[T] = new Catch(pfFromExceptions(exceptions : _*)) withDesc (exceptions map (_.getName) mkString ", ") @@ -178,42 +304,56 @@ object Exception { /** Creates a `Catch` object which will catch any of the supplied exceptions. * Unlike "catching" which filters out those in shouldRethrow, this one will - * catch whatever you ask of it: `ControlThrowable`, `InterruptedException`, - * `OutOfMemoryError`, you name it. + * catch whatever you ask of it including $protectedExceptions. + * @group composition-catch-promiscuously */ def catchingPromiscuously[T](exceptions: Class[_]*): Catch[T] = catchingPromiscuously(pfFromExceptions(exceptions : _*)) def catchingPromiscuously[T](c: Catcher[T]): Catch[T] = new Catch(c, None, _ => false) - /** Creates a `Catch` object which catches and ignores any of the supplied exceptions. */ + /** Creates a `Catch` object which catches and ignores any of the supplied exceptions. + * @group composition-catch + */ def ignoring(exceptions: Class[_]*): Catch[Unit] = catching(exceptions: _*) withApply (_ => ()) - /** Creates a `Catch` object which maps all the supplied exceptions to `None`. */ + /** Creates a `Catch` object which maps all the supplied exceptions to `None`. + * @group composition-catch + */ def failing[T](exceptions: Class[_]*): Catch[Option[T]] = catching(exceptions: _*) withApply (_ => None) - /** Creates a `Catch` object which maps all the supplied exceptions to the given value. */ + /** Creates a `Catch` object which maps all the supplied exceptions to the given value. + * @group composition-catch + */ def failAsValue[T](exceptions: Class[_]*)(value: => T): Catch[T] = catching(exceptions: _*) withApply (_ => value) + class By[T,R](f: T => R) { + def by(x: T): R = f(x) + } + /** Returns a partially constructed `Catch` object, which you must give - * an exception handler function as an argument to `by`. Example: + * an exception handler function as an argument to `by`. + * @example * {{{ - * handling(ex1, ex2) by (_.printStackTrace) + * handling(classOf[MalformedURLException], classOf[NullPointerException]) by (_.printStackTrace) * }}} + * @group dsl */ - class By[T,R](f: T => R) { - def by(x: T): R = f(x) - } + // TODO: Add return type def handling[T](exceptions: Class[_]*) = { def fun(f: Throwable => T) = catching(exceptions: _*) withApply f new By[Throwable => T, Catch[T]](fun _) } - /** Returns a `Catch` object with no catch logic and the argument as `Finally`. */ + /** Returns a `Catch` object with no catch logic and the argument as the finally logic. + * @group composition-finally + */ def ultimately[T](body: => Unit): Catch[T] = noCatch andFinally body - /** Creates a `Catch` object which unwraps any of the supplied exceptions. */ + /** Creates a `Catch` object which unwraps any of the supplied exceptions. + * @group composition-catch + */ def unwrapping[T](exceptions: Class[_]*): Catch[T] = { def unwrap(x: Throwable): Throwable = if (wouldMatch(x, exceptions) && x.getCause != null) unwrap(x.getCause) diff --git a/src/partest-extras/scala/tools/partest/BytecodeTest.scala b/src/partest-extras/scala/tools/partest/BytecodeTest.scala index 290b7b434edc..532dfd2a7300 100644 --- a/src/partest-extras/scala/tools/partest/BytecodeTest.scala +++ b/src/partest-extras/scala/tools/partest/BytecodeTest.scala @@ -1,10 +1,10 @@ package scala.tools.partest -import scala.tools.nsc.util.JavaClassPath import scala.collection.JavaConverters._ -import scala.tools.asm.{ClassWriter, ClassReader} +import scala.tools.asm.{ClassReader, ClassWriter} import scala.tools.asm.tree._ -import java.io.{File => JFile, InputStream} +import java.io.{InputStream, File => JFile} + import AsmNode._ /** @@ -125,12 +125,16 @@ abstract class BytecodeTest { cn } - protected lazy val classpath: JavaClassPath = { - import scala.tools.nsc.util.ClassPath.DefaultJavaContext + protected lazy val classpath: scala.tools.nsc.util.ClassPath = { + import scala.tools.nsc.classpath.AggregateClassPath + import scala.tools.nsc.classpath.ClassPathFactory import scala.tools.util.PathResolver.Defaults + import scala.tools.nsc.Settings // logic inspired by scala.tools.util.PathResolver implementation - val containers = DefaultJavaContext.classesInExpandedPath(Defaults.javaUserClassPath) - new JavaClassPath(containers, DefaultJavaContext) + // `Settings` is used to check YdisableFlatCpCaching in ZipArchiveFlatClassPath + val factory = new ClassPathFactory(new Settings()) + val containers = factory.classesInExpandedPath(Defaults.javaUserClassPath) + new AggregateClassPath(containers) } } diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index d2312440ccc5..db8ac9b0cbe7 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -1152,6 +1152,7 @@ trait Definitions extends api.StandardDefinitions { lazy val ElidableMethodClass = requiredClass[scala.annotation.elidable] lazy val ImplicitNotFoundClass = requiredClass[scala.annotation.implicitNotFound] lazy val ImplicitAmbiguousClass = getClassIfDefined("scala.annotation.implicitAmbiguous") + lazy val JUnitTestClass = getClassIfDefined("org.junit.Test") lazy val MigrationAnnotationClass = requiredClass[scala.annotation.migration] lazy val ScalaStrictFPAttr = requiredClass[scala.annotation.strictfp] lazy val SwitchClass = requiredClass[scala.annotation.switch] diff --git a/src/reflect/scala/reflect/internal/transform/Erasure.scala b/src/reflect/scala/reflect/internal/transform/Erasure.scala index c069e2c1982a..412c49f571a2 100644 --- a/src/reflect/scala/reflect/internal/transform/Erasure.scala +++ b/src/reflect/scala/reflect/internal/transform/Erasure.scala @@ -113,7 +113,8 @@ trait Erasure { def apply(tp: Type): Type = tp match { case ConstantType(ct) => - if (ct.tag == ClazzTag) ConstantType(Constant(apply(ct.typeValue))) + // erase classOf[List[_]] to classOf[List]. special case for classOf[Unit], avoid erasing to classOf[BoxedUnit]. + if (ct.tag == ClazzTag && ct.typeValue.typeSymbol != UnitClass) ConstantType(Constant(apply(ct.typeValue))) else tp case st: ThisType if st.sym.isPackageClass => tp @@ -165,7 +166,7 @@ trait Erasure { /** The erasure |T| of a type T. This is: * - * - For a constant type, itself. + * - For a constant type classOf[T], classOf[|T|], unless T is Unit. For any other constant type, itself. * - For a type-bounds structure, the erasure of its upper bound. * - For every other singleton type, the erasure of its supertype. * - For a typeref scala.Array+[T] where T is an abstract type, AnyRef. diff --git a/src/reflect/scala/reflect/io/ZipArchive.scala b/src/reflect/scala/reflect/io/ZipArchive.scala index 42e22fda6fbf..262ab22ce941 100644 --- a/src/reflect/scala/reflect/io/ZipArchive.scala +++ b/src/reflect/scala/reflect/io/ZipArchive.scala @@ -13,7 +13,7 @@ import java.io.{ File => JFile } import java.util.zip.{ ZipEntry, ZipFile, ZipInputStream } import java.util.jar.Manifest import scala.collection.mutable -import scala.collection.convert.WrapAsScala.asScalaIterator +import scala.collection.JavaConverters._ import scala.annotation.tailrec /** An abstraction for zip files and streams. Everything is written the way @@ -238,7 +238,7 @@ final class ManifestResources(val url: URL) extends ZipArchive(null) { val root = new DirEntry("/") val dirs = mutable.HashMap[String, DirEntry]("/" -> root) val manifest = new Manifest(input) - val iter = manifest.getEntries().keySet().iterator().filter(_.endsWith(".class")).map(new ZipEntry(_)) + val iter = manifest.getEntries().keySet().iterator().asScala.filter(_.endsWith(".class")).map(new ZipEntry(_)) for (zipEntry <- iter) { val dir = getDir(dirs, zipEntry) diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala index 37b07ce775c0..9b0d66f41c95 100644 --- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala +++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala @@ -613,7 +613,7 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive loadBytes[String]("scala.reflect.ScalaSignature") match { case Some(ssig) => info(s"unpickling Scala $clazz and $module, owner = ${clazz.owner}") - val bytes = ssig.getBytes + val bytes = ssig.getBytes(java.nio.charset.StandardCharsets.UTF_8) val len = ByteCodecs.decode(bytes) assignAssociatedFile(clazz, module, jclazz) unpickler.unpickle(bytes take len, 0, clazz, module, jclazz.getName) @@ -622,7 +622,7 @@ private[scala] trait JavaMirrors extends internal.SymbolTable with api.JavaUnive loadBytes[Array[String]]("scala.reflect.ScalaLongSignature") match { case Some(slsig) => info(s"unpickling Scala $clazz and $module with long Scala signature") - val encoded = slsig flatMap (_.getBytes) + val encoded = slsig flatMap (_.getBytes(java.nio.charset.StandardCharsets.UTF_8)) val len = ByteCodecs.decode(encoded) val decoded = encoded.take(len) assignAssociatedFile(clazz, module, jclazz) diff --git a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala index 4630597668ef..d50debd7ee93 100644 --- a/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala +++ b/src/reflect/scala/reflect/runtime/JavaUniverseForce.scala @@ -377,6 +377,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse => definitions.ElidableMethodClass definitions.ImplicitNotFoundClass definitions.ImplicitAmbiguousClass + definitions.JUnitTestClass definitions.MigrationAnnotationClass definitions.ScalaStrictFPAttr definitions.SwitchClass diff --git a/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala b/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala index 1f2b0952e790..95964e18d94b 100644 --- a/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala +++ b/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala @@ -11,10 +11,9 @@ import java.util.{Collection => JCollection, List => JList} import _root_.jline.{console => jconsole} import jline.console.ConsoleReader -import jline.console.completer.{CompletionHandler, Completer} +import jline.console.completer.{CandidateListCompletionHandler, Completer, CompletionHandler} import jconsole.history.{History => JHistory} - import scala.tools.nsc.interpreter import scala.tools.nsc.interpreter.{Completion, NoCompletion} import scala.tools.nsc.interpreter.Completion.Candidates @@ -133,32 +132,15 @@ private class JLineConsoleReader extends jconsole.ConsoleReader with interpreter newCursor } } + getCompletionHandler match { + case clch: CandidateListCompletionHandler => clch.setPrintSpaceAfterFullCompletion(false) + } completion match { case NoCompletion => () case _ => this addCompleter completer } - // This is a workaround for https://github.com/jline/jline2/issues/208 - // and should not be necessary once we upgrade to JLine 2.13.1 - /// - // Test by: - // scala> {" ".char} - // - // And checking we don't get an extra } on the line. - /// - val handler = getCompletionHandler - setCompletionHandler(new CompletionHandler { - override def complete(consoleReader: ConsoleReader, list: JList[CharSequence], i: Int): Boolean = { - try { - handler.complete(consoleReader, list, i) - } finally if (getCursorBuffer.cursor != getCursorBuffer.length()) { - print(" ") - getCursorBuffer.write(' ') - backspace() - } - } - }) setAutoprintThreshold(400) // max completion candidates without warning } } diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala index 893bde42ab71..8c91242b36ac 100644 --- a/src/repl/scala/tools/nsc/interpreter/IMain.scala +++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala @@ -11,18 +11,18 @@ import PartialFunction.cond import scala.language.implicitConversions import scala.beans.BeanProperty import scala.collection.mutable -import scala.concurrent.{ Future, ExecutionContext } -import scala.reflect.runtime.{ universe => ru } -import scala.reflect.{ ClassTag, classTag } -import scala.reflect.internal.util.{ BatchSourceFile, SourceFile } -import scala.tools.util.PathResolverFactory +import scala.concurrent.{ExecutionContext, Future} +import scala.reflect.runtime.{universe => ru} +import scala.reflect.{ClassTag, classTag} +import scala.reflect.internal.util.{BatchSourceFile, SourceFile} import scala.tools.nsc.io.AbstractFile -import scala.tools.nsc.typechecker.{ TypeStrings, StructuredTypeStrings } +import scala.tools.nsc.typechecker.{StructuredTypeStrings, TypeStrings} import scala.tools.nsc.util._ import ScalaClassLoader.URLClassLoader import scala.tools.nsc.util.Exceptional.unwrap -import javax.script.{AbstractScriptEngine, Bindings, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException, CompiledScript, Compilable} +import javax.script.{AbstractScriptEngine, Bindings, Compilable, CompiledScript, ScriptContext, ScriptEngine, ScriptEngineFactory, ScriptException} import java.net.URL +import scala.tools.util.PathResolver /** An interpreter for Scala code. * @@ -91,7 +91,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set def compilerClasspath: Seq[java.net.URL] = ( if (isInitializeComplete) global.classPath.asURLs - else PathResolverFactory.create(settings).resultAsURLs // the compiler's classpath + else new PathResolver(settings).resultAsURLs // the compiler's classpath ) def settings = initialSettings // Run the code body with the given boolean settings flipped to true. diff --git a/src/repl/scala/tools/nsc/interpreter/Imports.scala b/src/repl/scala/tools/nsc/interpreter/Imports.scala index 6a75432ac660..fdbd93d8628b 100644 --- a/src/repl/scala/tools/nsc/interpreter/Imports.scala +++ b/src/repl/scala/tools/nsc/interpreter/Imports.scala @@ -183,7 +183,7 @@ trait Imports { case _ => val valName = req.lineRep.packageName + req.lineRep.readName if (!tempValLines.contains(req.lineRep.lineId)) { - code.append(s"val $valName = $objName\n") + code.append(s"val $valName: ${objName}.type = $objName\n") tempValLines += req.lineRep.lineId } code.append(s"import $valName${req.accessPath}.`$imv`;\n") diff --git a/src/repl/scala/tools/nsc/interpreter/PresentationCompilation.scala b/src/repl/scala/tools/nsc/interpreter/PresentationCompilation.scala index e3dc72b71702..b9a4054ffccf 100644 --- a/src/repl/scala/tools/nsc/interpreter/PresentationCompilation.scala +++ b/src/repl/scala/tools/nsc/interpreter/PresentationCompilation.scala @@ -7,9 +7,7 @@ package scala.tools.nsc.interpreter import scala.reflect.internal.util.RangePosition import scala.reflect.io.AbstractFile import scala.tools.nsc.backend.JavaPlatform -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.nsc.util.ClassPath.DefaultJavaContext -import scala.tools.nsc.util.{ClassPath, MergedClassPath, DirectoryClassPath} +import scala.tools.nsc.util.ClassPath import scala.tools.nsc.{interactive, Settings} import scala.tools.nsc.reporters.StoreReporter import scala.tools.nsc.classpath._ @@ -58,12 +56,8 @@ trait PresentationCompilation { */ def newPresentationCompiler(): interactive.Global = { def mergedFlatClasspath = { - val replOutClasspath = FlatClassPathFactory.newClassPath(replOutput.dir, settings) - AggregateFlatClassPath(replOutClasspath :: global.platform.flatClassPath :: Nil) - } - def mergedRecursiveClasspath = { - val replOutClasspath: DirectoryClassPath = new DirectoryClassPath(replOutput.dir, DefaultJavaContext) - new MergedClassPath[AbstractFile](replOutClasspath :: global.platform.classPath :: Nil, DefaultJavaContext) + val replOutClasspath = ClassPathFactory.newClassPath(replOutput.dir, settings) + AggregateClassPath(replOutClasspath :: global.platform.classPath :: Nil) } def copySettings: Settings = { val s = new Settings(_ => () /* ignores "bad option -nc" errors, etc */) @@ -74,16 +68,9 @@ trait PresentationCompilation { val storeReporter: StoreReporter = new StoreReporter val interactiveGlobal = new interactive.Global(copySettings, storeReporter) { self => override lazy val platform: ThisPlatform = { - if (settings.YclasspathImpl.value == ClassPathRepresentationType.Flat) { - new JavaPlatform { - val global: self.type = self - override private[nsc] lazy val flatClassPath: FlatClassPath = mergedFlatClasspath - } - } else { - new JavaPlatform { - val global: self.type = self - override def classPath: ClassPath[AbstractFile] = mergedRecursiveClasspath - } + new JavaPlatform { + val global: self.type = self + override private[nsc] lazy val classPath: ClassPath = mergedFlatClasspath } } } diff --git a/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala b/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala index 1664546cabdb..bf7508cb4e72 100644 --- a/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala +++ b/src/repl/scala/tools/nsc/interpreter/ReplStrings.scala @@ -11,13 +11,21 @@ import scala.reflect.internal.Chars trait ReplStrings { /** Convert a string into code that can recreate the string. * This requires replacing all special characters by escape - * codes. It does not add the surrounding " marks. */ + * codes. It does not add the surrounding " marks. + */ def string2code(str: String): String = { val res = new StringBuilder for (c <- str) c match { - case '"' | '\'' | '\\' => res += '\\' ; res += c - case _ if c.isControl => res ++= Chars.char2uescape(c) - case _ => res += c + case '"' => res ++= """\"""" + case '\'' => res ++= """\'""" + case '\\' => res ++= """\\""" + case '\b' => res ++= """\b""" + case '\t' => res ++= """\t""" + case '\n' => res ++= """\n""" + case '\f' => res ++= """\f""" + case '\r' => res ++= """\r""" + case _ if c.isControl => res ++= Chars.char2uescape(c) + case _ => res += c } res.toString } diff --git a/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala b/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala index 8cd8a7ee092f..d3b4bf8ff5f3 100644 --- a/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala +++ b/src/scaladoc/scala/tools/nsc/doc/base/CommentFactoryBase.scala @@ -295,7 +295,7 @@ trait CommentFactoryBase { this: MemberLookupBase => } case line :: ls if (lastTagKey.isDefined) => { - val newtags = if (!line.isEmpty) { + val newtags = if (!line.isEmpty || inCodeBlock) { val key = lastTagKey.get val value = ((tags get key): @unchecked) match { diff --git a/src/scalap/scala/tools/scalap/Main.scala b/src/scalap/scala/tools/scalap/Main.scala index 3d2bfd7251d3..6a37bbc27075 100644 --- a/src/scalap/scala/tools/scalap/Main.scala +++ b/src/scalap/scala/tools/scalap/Main.scala @@ -8,17 +8,12 @@ package scala package tools.scalap -import java.io.{ PrintStream, OutputStreamWriter, ByteArrayOutputStream } +import java.io.{ByteArrayOutputStream, OutputStreamWriter, PrintStream} import scala.reflect.NameTransformer import scala.tools.nsc.Settings -import scala.tools.nsc.classpath.AggregateFlatClassPath -import scala.tools.nsc.classpath.FlatClassPathFactory -import scala.tools.nsc.io.AbstractFile -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.nsc.util.ClassFileLookup -import scala.tools.nsc.util.ClassPath.DefaultJavaContext -import scala.tools.nsc.util.JavaClassPath -import scala.tools.util.PathResolverFactory +import scala.tools.nsc.classpath.{AggregateClassPath, ClassPathFactory} +import scala.tools.nsc.util.ClassPath +import scala.tools.util.PathResolver import scalax.rules.scalasig._ /**The main object used to execute scalap on the command-line. @@ -101,7 +96,7 @@ class Main { /** Executes scalap with the given arguments and classpath for the * class denoted by `classname`. */ - def process(args: Arguments, path: ClassFileLookup[AbstractFile])(classname: String): Unit = { + def process(args: Arguments, path: ClassPath)(classname: String): Unit = { // find the classfile val encName = classname match { case "scala.AnyRef" => "java.lang.Object" @@ -145,7 +140,6 @@ object Main extends Main { val verbose = "-verbose" val version = "-version" - val classPathImplType = "-YclasspathImpl" val disableFlatClassPathCaching = "-YdisableFlatCpCaching" val logClassPath = "-Ylog-classpath" } @@ -183,7 +177,6 @@ object Main extends Main { val settings = new Settings() - arguments getArgument opts.classPathImplType foreach settings.YclasspathImpl.tryToSetFromPropertyValue settings.YdisableFlatCpCaching.value = arguments contains opts.disableFlatClassPathCaching settings.Ylogcp.value = arguments contains opts.logClassPath @@ -205,21 +198,16 @@ object Main extends Main { .withOption(opts.help) .withOptionalArg(opts.classpath) .withOptionalArg(opts.cp) - // TODO three temporary, hidden options to be able to test different classpath representations - .withOptionalArg(opts.classPathImplType) + // TODO two temporary, hidden options to be able to test different classpath representations .withOption(opts.disableFlatClassPathCaching) .withOption(opts.logClassPath) .parse(args) private def createClassPath(cpArg: Option[String], settings: Settings) = cpArg match { - case Some(cp) => settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Flat => - AggregateFlatClassPath(new FlatClassPathFactory(settings).classesInExpandedPath(cp)) - case ClassPathRepresentationType.Recursive => - new JavaClassPath(DefaultJavaContext.classesInExpandedPath(cp), DefaultJavaContext) - } + case Some(cp) => + AggregateClassPath(new ClassPathFactory(settings).classesInExpandedPath(cp)) case _ => settings.classpath.value = "." // include '.' in the default classpath SI-6669 - PathResolverFactory.create(settings).result + new PathResolver(settings).result } } diff --git a/test/disabled/presentation/akka/src/akka/actor/ActorRegistry.scala b/test/disabled/presentation/akka/src/akka/actor/ActorRegistry.scala index 5d649fcd36f1..224775129b0f 100644 --- a/test/disabled/presentation/akka/src/akka/actor/ActorRegistry.scala +++ b/test/disabled/presentation/akka/src/akka/actor/ActorRegistry.scala @@ -342,7 +342,7 @@ class Index[K <: AnyRef, V <: AnyRef: ArrayTag] { * if no matches it returns None */ def findValue(key: K)(f: (V) => Boolean): Option[V] = { - import scala.collection.JavaConversions._ + import scala.collection.convert.wrapAsScala._ val set = container get key if (set ne null) set.iterator.find(f) else None @@ -352,7 +352,7 @@ class Index[K <: AnyRef, V <: AnyRef: ArrayTag] { * Applies the supplied function to all keys and their values */ def foreach(fun: (K, V) => Unit) { - import scala.collection.JavaConversions._ + import scala.collection.convert.wrapAsScala._ container.entrySet foreach { (e) => e.getValue.foreach(fun(e.getKey, _)) } diff --git a/test/disabled/presentation/akka/src/akka/actor/Scheduler.scala b/test/disabled/presentation/akka/src/akka/actor/Scheduler.scala index 128584f3c52b..ef540f2df31d 100644 --- a/test/disabled/presentation/akka/src/akka/actor/Scheduler.scala +++ b/test/disabled/presentation/akka/src/akka/actor/Scheduler.scala @@ -15,8 +15,6 @@ */ package akka.actor -import scala.collection.JavaConversions - import java.util.concurrent._ import akka.event.EventHandler diff --git a/test/disabled/presentation/akka/src/akka/japi/JavaAPI.scala b/test/disabled/presentation/akka/src/akka/japi/JavaAPI.scala index f5c4ccdcaa6a..17ef3f7bb8eb 100644 --- a/test/disabled/presentation/akka/src/akka/japi/JavaAPI.scala +++ b/test/disabled/presentation/akka/src/akka/japi/JavaAPI.scala @@ -58,7 +58,7 @@ trait Creator[T] { * Java API */ sealed abstract class Option[A] extends java.lang.Iterable[A] { - import scala.collection.JavaConversions._ + import scala.collection.convert.wrapAsScala._ def get: A def isEmpty: Boolean diff --git a/test/disabled/presentation/akka/src/akka/routing/Iterators.scala b/test/disabled/presentation/akka/src/akka/routing/Iterators.scala index 315e7bea51d6..d110f2c2695e 100644 --- a/test/disabled/presentation/akka/src/akka/routing/Iterators.scala +++ b/test/disabled/presentation/akka/src/akka/routing/Iterators.scala @@ -5,7 +5,7 @@ package akka.routing import akka.actor.ActorRef -import scala.collection.JavaConversions._ +import scala.collection.convert.wrapAsScala._ import scala.collection.immutable.Seq /** diff --git a/test/disabled/presentation/akka/src/akka/routing/Listeners.scala b/test/disabled/presentation/akka/src/akka/routing/Listeners.scala index 04f6c1259f54..d8a537731d65 100644 --- a/test/disabled/presentation/akka/src/akka/routing/Listeners.scala +++ b/test/disabled/presentation/akka/src/akka/routing/Listeners.scala @@ -6,7 +6,7 @@ package akka.routing import akka.actor.{ Actor, ActorRef } import java.util.concurrent.ConcurrentSkipListSet -import scala.collection.JavaConversions._ +import scala.collection.convert.wrapAsScala._ sealed trait ListenerMessage case class Listen(listener: ActorRef) extends ListenerMessage diff --git a/test/files/neg/nowarnDefaultJunitMethods.check b/test/files/neg/nowarnDefaultJunitMethods.check new file mode 100644 index 000000000000..7efdcc299a94 --- /dev/null +++ b/test/files/neg/nowarnDefaultJunitMethods.check @@ -0,0 +1,6 @@ +C_1.scala:2: warning: JUnit tests in traits that are compiled as default methods are not executed by JUnit 4. JUnit 5 will fix this issue. + @org.junit.Test def foo = 0 + ^ +error: No warnings can be incurred under -Xfatal-warnings. +one warning found +one error found diff --git a/test/files/neg/nowarnDefaultJunitMethods.flags b/test/files/neg/nowarnDefaultJunitMethods.flags new file mode 100644 index 000000000000..85d8eb2ba295 --- /dev/null +++ b/test/files/neg/nowarnDefaultJunitMethods.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/neg/nowarnDefaultJunitMethods/C_1.scala b/test/files/neg/nowarnDefaultJunitMethods/C_1.scala new file mode 100644 index 000000000000..e2565a48bc93 --- /dev/null +++ b/test/files/neg/nowarnDefaultJunitMethods/C_1.scala @@ -0,0 +1,5 @@ +trait T { + @org.junit.Test def foo = 0 +} + +class C extends T diff --git a/test/files/neg/nowarnDefaultJunitMethods/Test.java b/test/files/neg/nowarnDefaultJunitMethods/Test.java new file mode 100644 index 000000000000..e8d64c2cc863 --- /dev/null +++ b/test/files/neg/nowarnDefaultJunitMethods/Test.java @@ -0,0 +1,3 @@ +package org.junit; + +public @interface Test { } diff --git a/test/files/neg/saferJavaConversions.scala b/test/files/neg/saferJavaConversions.scala index f0611204e6b5..b70a918404e9 100644 --- a/test/files/neg/saferJavaConversions.scala +++ b/test/files/neg/saferJavaConversions.scala @@ -3,17 +3,17 @@ case class Foo(s: String) object Test { def f1 = { - import scala.collection.JavaConversions._ + import scala.collection.convert.ImplicitConversions._ val map: Map[Foo, String] = Map(Foo("a") -> "a", Foo("b") -> "b") val v = map.get("a") // should be a type error, actually returns null } def f2 = { - import scala.collection.convert.wrapAsScala._ + import scala.collection.convert.ImplicitConversionsToScala._ val map: Map[Foo, String] = Map(Foo("a") -> "a", Foo("b") -> "b") val v = map.get("a") // now this is a type error } def f3 = { - import scala.collection.convert.wrapAsJava._ + import scala.collection.convert.ImplicitConversionsToJava._ val map: Map[Foo, String] = Map(Foo("a") -> "a", Foo("b") -> "b") val v = map.get("a") } diff --git a/test/files/neg/t5580b.scala b/test/files/neg/t5580b.scala index 2161da45849c..98b493e80342 100644 --- a/test/files/neg/t5580b.scala +++ b/test/files/neg/t5580b.scala @@ -1,5 +1,5 @@ import scala.collection.mutable.WeakHashMap -import scala.collection.JavaConversions._ +import scala.collection.JavaConverters._ class bar { } diff --git a/test/files/neg/t9684.check b/test/files/neg/t9684.check new file mode 100644 index 000000000000..833ca3341a1d --- /dev/null +++ b/test/files/neg/t9684.check @@ -0,0 +1,9 @@ +t9684.scala:6: warning: object JavaConversions in package collection is deprecated: Use JavaConverters + null.asInstanceOf[java.util.List[Int]] : Buffer[Int] + ^ +t9684.scala:8: warning: object JavaConversions in package collection is deprecated: Use JavaConverters + null.asInstanceOf[Iterable[Int]] : java.util.Collection[Int] + ^ +error: No warnings can be incurred under -Xfatal-warnings. +two warnings found +one error found diff --git a/test/files/neg/t9684.flags b/test/files/neg/t9684.flags new file mode 100644 index 000000000000..c6bfaf1f64a4 --- /dev/null +++ b/test/files/neg/t9684.flags @@ -0,0 +1 @@ +-deprecation -Xfatal-warnings diff --git a/test/files/neg/t9684.scala b/test/files/neg/t9684.scala new file mode 100644 index 000000000000..f7ece269e652 --- /dev/null +++ b/test/files/neg/t9684.scala @@ -0,0 +1,9 @@ + +import scala.collection.JavaConversions._ +import scala.collection.mutable.Buffer + +trait Test { + null.asInstanceOf[java.util.List[Int]] : Buffer[Int] + + null.asInstanceOf[Iterable[Int]] : java.util.Collection[Int] +} diff --git a/test/files/neg/t9684b.check b/test/files/neg/t9684b.check new file mode 100644 index 000000000000..5f328abd4389 --- /dev/null +++ b/test/files/neg/t9684b.check @@ -0,0 +1,7 @@ +t9684b.scala:6: error: reference to asScalaIterator is ambiguous; +it is imported twice in the same scope by +import scala.collection.JavaConversions._ +and import scala.collection.JavaConverters._ + asScalaIterator(null) // fails: asScalaIterator is imported twice. + ^ +one error found diff --git a/test/files/neg/t9684b.scala b/test/files/neg/t9684b.scala new file mode 100644 index 000000000000..010e9d1b5dc3 --- /dev/null +++ b/test/files/neg/t9684b.scala @@ -0,0 +1,14 @@ +trait T1 { + import scala.collection.JavaConverters._ + import scala.collection.JavaConversions._ + + null.asInstanceOf[java.util.Iterator[String]]: Iterator[String] // works + asScalaIterator(null) // fails: asScalaIterator is imported twice. +} + +trait T2 { + import scala.collection.JavaConversions.asScalaIterator + + null.asInstanceOf[java.util.Iterator[String]]: Iterator[String] // works + asScalaIterator(null) // works +} diff --git a/test/files/neg/trait-defaults-super.check b/test/files/neg/trait-defaults-super.check new file mode 100644 index 000000000000..153aba459b8e --- /dev/null +++ b/test/files/neg/trait-defaults-super.check @@ -0,0 +1,4 @@ +trait-defaults-super.scala:14: error: Implementation restruction: unable to emit a super call to java/lang/Iterable.spliterator from C. Add java/lang/Iterable as a direct parent of C +class C extends T + ^ +one error found diff --git a/test/files/neg/trait-defaults-super.scala b/test/files/neg/trait-defaults-super.scala new file mode 100644 index 000000000000..def271e8e747 --- /dev/null +++ b/test/files/neg/trait-defaults-super.scala @@ -0,0 +1,21 @@ +trait T extends java.lang.Iterable[String] { + + override def spliterator(): java.util.Spliterator[String] = { + super[Iterable].spliterator + super.spliterator + null + } + def foo = { + super[Iterable].spliterator + super.spliterator + } + def iterator(): java.util.Iterator[String] = java.util.Collections.emptyList().iterator() +} +class C extends T +object Test { + def main(args: Array[String]): Unit = { + val t: T = new C + t.spliterator + t.foo + } +} diff --git a/test/files/pos/arrays2.scala b/test/files/pos/arrays2.scala index 795c486e370c..b770d21b8a26 100644 --- a/test/files/pos/arrays2.scala +++ b/test/files/pos/arrays2.scala @@ -17,7 +17,7 @@ object arrays4 { // #2461 object arrays3 { - import scala.collection.JavaConversions._ + import collection.convert.ImplicitConversions._ def apply[X](xs : X*) : java.util.List[X] = java.util.Arrays.asList(xs: _*) } diff --git a/test/files/pos/constant-warning.check b/test/files/pos/constant-warning.check new file mode 100644 index 000000000000..f7df2165d133 --- /dev/null +++ b/test/files/pos/constant-warning.check @@ -0,0 +1,4 @@ +constant-warning.scala:2: warning: Evaluation of a constant expression results in an arithmetic error: / by zero + val fails = 1 + 2 / (3 - 2 - 1) + ^ +one warning found diff --git a/test/files/pos/constant-warning.flags b/test/files/pos/constant-warning.flags new file mode 100644 index 000000000000..d00cbbe77b61 --- /dev/null +++ b/test/files/pos/constant-warning.flags @@ -0,0 +1 @@ +-Xlint:constant diff --git a/test/files/pos/constant-warning.scala b/test/files/pos/constant-warning.scala new file mode 100644 index 000000000000..c8ca8823e759 --- /dev/null +++ b/test/files/pos/constant-warning.scala @@ -0,0 +1,3 @@ +object Test { + val fails = 1 + 2 / (3 - 2 - 1) +} diff --git a/test/files/pos/javaConversions-2.10-ambiguity.scala b/test/files/pos/javaConversions-2.10-ambiguity.scala index c4aad6cbfc84..b08568f4757f 100644 --- a/test/files/pos/javaConversions-2.10-ambiguity.scala +++ b/test/files/pos/javaConversions-2.10-ambiguity.scala @@ -1,5 +1,5 @@ -import collection.{JavaConversions, mutable, concurrent} -import JavaConversions._ +import collection.{mutable, concurrent} +import collection.convert.ImplicitConversionsToScala._ import java.util.concurrent.{ConcurrentHashMap => CHM} object Bar { diff --git a/test/files/pos/javaConversions-2.10-regression.scala b/test/files/pos/javaConversions-2.10-regression.scala index 7c7ff03b55d7..8d84c92b6151 100644 --- a/test/files/pos/javaConversions-2.10-regression.scala +++ b/test/files/pos/javaConversions-2.10-regression.scala @@ -1,10 +1,10 @@ -import collection.{JavaConversions, mutable, concurrent} -import JavaConversions._ +import collection.{convert, mutable, concurrent, JavaConverters} +import convert.ImplicitConversionsToScala._ import java.util.concurrent.{ConcurrentHashMap => CHM} object Foo { def buildCache2_9_simple[K <: AnyRef, V <: AnyRef]: concurrent.Map[K, V] = - mapAsScalaConcurrentMap(new CHM()) + JavaConverters.mapAsScalaConcurrentMap(new CHM()) def buildCache2_9_implicit[K <: AnyRef, V <: AnyRef]: concurrent.Map[K, V] = new CHM[K, V]() diff --git a/test/files/pos/t2293.scala b/test/files/pos/t2293.scala index 65f717f8519c..baa44552c9b8 100644 --- a/test/files/pos/t2293.scala +++ b/test/files/pos/t2293.scala @@ -1,5 +1,5 @@ -import scala.collection.JavaConversions._ +import scala.collection.convert.ImplicitConversionsToJava._ object Test { val m: java.util.Map[String,String] = collection.mutable.Map("1"->"2") -} \ No newline at end of file +} diff --git a/test/files/pos/t2956/t2956.scala b/test/files/pos/t2956/t2956.scala index eb6e817465d7..9b6ae8098fb8 100644 --- a/test/files/pos/t2956/t2956.scala +++ b/test/files/pos/t2956/t2956.scala @@ -1,7 +1,7 @@ -import scala.collection.JavaConversions._ +import scala.collection.convert.ImplicitConversionsToScala._ class Outer { protected class Inner extends BeanDefinitionVisitor { protected def visitMap(mapVal: Map[_, _]): Unit = () } -} \ No newline at end of file +} diff --git a/test/files/pos/t3688.scala b/test/files/pos/t3688.scala index d15e9d1a840d..58464332d1c9 100644 --- a/test/files/pos/t3688.scala +++ b/test/files/pos/t3688.scala @@ -1,5 +1,5 @@ import collection.mutable -import collection.JavaConversions._ +import collection.convert.ImplicitConversionsToJava._ import java.{util => ju} object Test { @@ -11,4 +11,4 @@ object Test { object Test2 { def m[P <% ju.List[Int]](l: P) = 1 m(List(1)) // bug: should compile -} \ No newline at end of file +} diff --git a/test/files/pos/trait-defaults-super.scala b/test/files/pos/trait-defaults-super.scala new file mode 100644 index 000000000000..8f867ab5632d --- /dev/null +++ b/test/files/pos/trait-defaults-super.scala @@ -0,0 +1,21 @@ +trait T extends java.lang.Iterable[String] { + + override def spliterator(): java.util.Spliterator[String] = { + super[Iterable].spliterator + super.spliterator + null + } + def foo = { + super[Iterable].spliterator + super.spliterator + } + def iterator(): java.util.Iterator[String] = java.util.Collections.emptyList().iterator() +} +class C extends T with java.lang.Iterable[String] // super accessor is okay with Iterable as a direct parent +object Test { + def main(args: Array[String]): Unit = { + val t: T = new C + t.spliterator + t.foo + } +} diff --git a/test/files/presentation/ide-bug-1000531/src/CrashOnLoad.scala b/test/files/presentation/ide-bug-1000531/src/CrashOnLoad.scala index 3f59282083f5..25e0a9580f10 100644 --- a/test/files/presentation/ide-bug-1000531/src/CrashOnLoad.scala +++ b/test/files/presentation/ide-bug-1000531/src/CrashOnLoad.scala @@ -1,6 +1,6 @@ /** When this files is opened within the IDE, a typing error is reported. */ class A[B] extends TestIterable[B] { - import scala.collection.JavaConversions._ + import collection.convert.ImplicitConversionsToScala._ def iterator: other.TestIterator[Nothing] = ??? iterator./*!*/ diff --git a/test/files/run/concurrent-map-conversions.scala b/test/files/run/concurrent-map-conversions.scala index d23d5bbbe4d6..1179764e37eb 100644 --- a/test/files/run/concurrent-map-conversions.scala +++ b/test/files/run/concurrent-map-conversions.scala @@ -1,14 +1,5 @@ - - - - -object Test { - - def main(args: Array[String]) { - testConversions() - testConverters() - } +object Test extends App { def needPackageConcurrentMap(map: collection.concurrent.Map[Int, Int]) { } @@ -16,7 +7,7 @@ object Test { } def testConversions() { - import collection.JavaConversions._ + import collection.convert.ImplicitConversions._ val skiplist = new java.util.concurrent.ConcurrentSkipListMap[Int, Int] val ctrie = new collection.concurrent.TrieMap[Int, Int] @@ -33,4 +24,6 @@ object Test { needJavaConcurrent(ctrie.asJava) } + testConversions() + testConverters() } diff --git a/test/files/run/delambdafy_t6028.check b/test/files/run/delambdafy_t6028.check index b90dea62ed69..8b0ae7e9b97e 100644 --- a/test/files/run/delambdafy_t6028.check +++ b/test/files/run/delambdafy_t6028.check @@ -11,7 +11,7 @@ package { def foo(methodParam: String): Function0 = { val methodLocal: String = ""; { - (() => T.this.$anonfun$1(methodParam, methodLocal)) + (() => T.this.$anonfun$foo$1(methodParam, methodLocal)) } }; def bar(barParam: String): Object = { @@ -21,10 +21,10 @@ package { def tryy(tryyParam: String): Function0 = { var tryyLocal: runtime.ObjectRef = scala.runtime.ObjectRef.create(""); { - (() => T.this.$anonfun$2(tryyParam, tryyLocal)) + (() => T.this.$anonfun$tryy$1(tryyParam, tryyLocal)) } }; - final private[this] def $anonfun$1(methodParam$1: String, methodLocal$1: String): String = T.this.classParam.+(T.this.field()).+(methodParam$1).+(methodLocal$1); + final private[this] def $anonfun$foo$1(methodParam$1: String, methodLocal$1: String): String = T.this.classParam.+(T.this.field()).+(methodParam$1).+(methodLocal$1); abstract trait MethodLocalTrait$1 extends Object { def /*MethodLocalTrait$1*/$init$(barParam$1: String): Unit = { () @@ -54,7 +54,7 @@ package { T.this.MethodLocalObject$lzycompute$1(barParam$1, MethodLocalObject$module$1) else MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type](); - final private[this] def $anonfun$2(tryyParam$1: String, tryyLocal$1: runtime.ObjectRef): Unit = try { + final private[this] def $anonfun$tryy$1(tryyParam$1: String, tryyLocal$1: runtime.ObjectRef): Unit = try { tryyLocal$1.elem = tryyParam$1 } finally () } diff --git a/test/files/run/delambdafy_t6555.check b/test/files/run/delambdafy_t6555.check index b6ccebde78f0..d8b834edc7ce 100644 --- a/test/files/run/delambdafy_t6555.check +++ b/test/files/run/delambdafy_t6555.check @@ -6,8 +6,8 @@ package { () }; private[this] val f: String => String = { - final def $anonfun(param: String): String = param; - ((param: String) => $anonfun(param)) + final def $anonfun$f(param: String): String = param; + ((param: String) => $anonfun$f(param)) }; def f(): String => String = Foo.this.f } diff --git a/test/files/run/delambdafy_uncurry_byname_method.check b/test/files/run/delambdafy_uncurry_byname_method.check index e0f281b1cd8c..71e404ce64f5 100644 --- a/test/files/run/delambdafy_uncurry_byname_method.check +++ b/test/files/run/delambdafy_uncurry_byname_method.check @@ -7,8 +7,8 @@ package { }; def bar(x: () => String): String = x.apply(); def foo(): String = Foo.this.bar({ - final def $anonfun(): String = ""; - (() => $anonfun()) + final def $anonfun$foo(): String = ""; + (() => $anonfun$foo()) }) } } diff --git a/test/files/run/delambdafy_uncurry_method.check b/test/files/run/delambdafy_uncurry_method.check index 5ee3d174b3a0..8aa0b92054b3 100644 --- a/test/files/run/delambdafy_uncurry_method.check +++ b/test/files/run/delambdafy_uncurry_method.check @@ -7,8 +7,8 @@ package { }; def bar(): Unit = { val f: Int => Int = { - final def $anonfun(x: Int): Int = x.+(1); - ((x: Int) => $anonfun(x)) + final def $anonfun|(x: Int): Int = x.+(1); + ((x: Int) => $anonfun|(x)) }; () } diff --git a/test/files/run/map_java_conversions.scala b/test/files/run/map_java_conversions.scala index c007b3e0ebb1..e287b0eb097a 100644 --- a/test/files/run/map_java_conversions.scala +++ b/test/files/run/map_java_conversions.scala @@ -1,20 +1,16 @@ - - - - +import collection.convert.ImplicitConversionsToScala._ +import collection.JavaConverters._ object Test { def main(args: Array[String]) { - import collection.JavaConversions._ - test(new java.util.HashMap[String, String]) test(new java.util.Properties) testConcMap } def testConcMap { - import collection.JavaConversions._ + import collection.convert.ImplicitConversionsToScala._ val concMap = new java.util.concurrent.ConcurrentHashMap[String, String] @@ -50,7 +46,6 @@ object Test { for (i <- 0 until 10) m += (("key" + i, "value" + i)) for ((k, v) <- m) assert(k.startsWith("key")) } - } diff --git a/test/files/run/mixin-signatures.check b/test/files/run/mixin-signatures.check index 9961992e2d1c..9bf906d42a1e 100644 --- a/test/files/run/mixin-signatures.check +++ b/test/files/run/mixin-signatures.check @@ -60,6 +60,7 @@ interface Foo1 { generic: public abstract R Base.f(T) public default java.lang.String Foo1.f(java.lang.Object) generic: public default java.lang.String Foo1.f(T) + public static java.lang.String Foo1.f(Foo1,java.lang.Object) public abstract java.lang.Object Base.g(java.lang.Object) generic: public abstract R Base.g(T) public abstract java.lang.String Foo1.g(java.lang.Object) @@ -73,6 +74,7 @@ interface Foo2 { generic: public abstract R Base.f(T) public default java.lang.Object Foo2.f(java.lang.String) generic: public default R Foo2.f(java.lang.String) + public static java.lang.Object Foo2.f(Foo2,java.lang.String) public abstract java.lang.Object Base.g(java.lang.Object) generic: public abstract R Base.g(T) public abstract java.lang.Object Foo2.g(java.lang.String) diff --git a/test/files/run/noInlineUnknownIndy/Test.scala b/test/files/run/noInlineUnknownIndy/Test.scala index 16d81265437a..8d2d20a3cd52 100644 --- a/test/files/run/noInlineUnknownIndy/Test.scala +++ b/test/files/run/noInlineUnknownIndy/Test.scala @@ -1,6 +1,6 @@ import java.io.File -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.asm.tree.{ClassNode, InvokeDynamicInsnNode} import scala.tools.asm.{Handle, Opcodes} import scala.tools.partest.BytecodeTest.modifyClassFile diff --git a/test/files/run/repl-no-uescape.check b/test/files/run/repl-no-uescape.check new file mode 100644 index 000000000000..01eeafaa701f --- /dev/null +++ b/test/files/run/repl-no-uescape.check @@ -0,0 +1,5 @@ + +scala> object A +defined object A + +scala> :quit diff --git a/test/files/run/repl-no-uescape.scala b/test/files/run/repl-no-uescape.scala new file mode 100644 index 000000000000..1865109ebf09 --- /dev/null +++ b/test/files/run/repl-no-uescape.scala @@ -0,0 +1,31 @@ +import scala.tools.partest.ReplTest +import scala.tools.nsc.Settings + +/* +scala> object A +:10: error: invalid escape character ++ "defined object " + "A" + "\u000A" + +Under -Dscala.color=true control chars are common + $eval.this.$print = { + $line2.$read.$iw.$iw; + "\033[1m\033[34mres1\033[0m: \033[1m\033[32mInt\033[0m = ".+(scala.runtime.ScalaRunTime.replStringOf($line2.$read.$iw.$iw.res1, 1000)) + }; + +$ skala -Dscala.color=true -Xno-uescape +Welcome to Scala 2.11.9-20160323-163638-1fcfdd8c8b (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_60). +Type in expressions for evaluation. Or try :help. + +scala> 42 +:10: error: invalid escape character + + "\u001B[1m\u001B[34mres0\u001B[0m: \u001B[1m\u001B[32mInt\u001B[0m = " + scala.runtime.ScalaRunTime.replStringOf(res0, 1000) + */ +object Test extends ReplTest { + override def transformSettings(settings: Settings): Settings = { + settings.nouescape.value = true + settings + } + def code = """ +object A + """ +} diff --git a/test/files/run/repl-paste-6.check b/test/files/run/repl-paste-6.check new file mode 100755 index 000000000000..efcea9274fa1 --- /dev/null +++ b/test/files/run/repl-paste-6.check @@ -0,0 +1,17 @@ + +scala> :paste < EOF +// Entering paste mode (EOF to finish) + +case class C(i: Int) +val c = C(42) +EOF + +// Exiting paste mode, now interpreting. + +defined class C +c: C = C(42) + +scala> val d: C = c // shew +d: C = C(42) + +scala> :quit diff --git a/test/files/run/repl-paste-6.scala b/test/files/run/repl-paste-6.scala new file mode 100644 index 000000000000..8040d24f8a11 --- /dev/null +++ b/test/files/run/repl-paste-6.scala @@ -0,0 +1,23 @@ + +import scala.tools.partest.ReplTest +import scala.tools.nsc.Settings + + +/* + * Add // show to witness: + * val $line3$read: $line3.$read.INSTANCE.type = $line3.$read.INSTANCE; + */ +object Test extends ReplTest { + override def transformSettings(settings: Settings): Settings = { + settings.Yreplclassbased.value = true + settings + } + def code = + """ +:paste < EOF +case class C(i: Int) +val c = C(42) +EOF +val d: C = c // shew + """ +} diff --git a/test/files/run/t2250.scala b/test/files/run/t2250.scala index 1ed333792ae9..f87b76d4d760 100644 --- a/test/files/run/t2250.scala +++ b/test/files/run/t2250.scala @@ -6,7 +6,7 @@ object Test { // we'll say rather unlikely a.sameElements(b) unless // they are pointing to the same array - import scala.collection.JavaConversions._ + import scala.collection.convert.ImplicitConversionsToScala._ assert(a sameElements b) } } diff --git a/test/files/run/t2813.2.scala b/test/files/run/t2813.2.scala index f41f6451f4f8..f26753600d6d 100644 --- a/test/files/run/t2813.2.scala +++ b/test/files/run/t2813.2.scala @@ -1,5 +1,5 @@ import java.util.LinkedList -import collection.JavaConversions._ +import collection.convert.ImplicitConversions._ object Test extends App { def assertListEquals[A](expected: List[A], actual: Seq[A]) { diff --git a/test/files/run/t5880.scala b/test/files/run/t5880.scala index f88df901606c..284ba03ff643 100644 --- a/test/files/run/t5880.scala +++ b/test/files/run/t5880.scala @@ -1,8 +1,5 @@ - -import scala.collection.JavaConversions._ - - +import scala.collection.convert.ImplicitConversionsToJava._ object Test { diff --git a/test/files/run/t6502.scala b/test/files/run/t6502.scala index dffb0e2f98a0..cb2b3ff4493a 100644 --- a/test/files/run/t6502.scala +++ b/test/files/run/t6502.scala @@ -1,6 +1,5 @@ import scala.tools.nsc.Settings import scala.tools.nsc.interpreter.{ ILoop, replProps } -import scala.tools.nsc.settings.ClassPathRepresentationType import scala.tools.partest._ object Test extends StoreReporterDirectTest { @@ -14,14 +13,6 @@ object Test extends StoreReporterDirectTest { compileString(newCompiler("-cp", classpath, "-d", s"${testOutput.path}/$jarFileName"))(code) } - var classPathKind: String = "" - - override def settings = { - val settings = new Settings - settings.YclasspathImpl.value = classPathKind - settings - } - def app1 = """ package test @@ -155,7 +146,7 @@ object Test extends StoreReporterDirectTest { assert(output.contains("created test6.Z"), output) } - def testAll(): Unit = { + def show(): Unit = { test1() test2() test3() @@ -163,11 +154,4 @@ object Test extends StoreReporterDirectTest { test5() test6() } - - def show(): Unit = { - classPathKind = ClassPathRepresentationType.Flat - testAll() - classPathKind = ClassPathRepresentationType.Recursive - testAll() - } } diff --git a/test/files/run/t7269.scala b/test/files/run/t7269.scala index d22e57dfee86..1102d49ecbb4 100644 --- a/test/files/run/t7269.scala +++ b/test/files/run/t7269.scala @@ -1,4 +1,4 @@ -import scala.collection.JavaConversions._ +import scala.collection.convert.ImplicitConversionsToScala._ import scala.collection.mutable object Test extends App { diff --git a/test/files/run/t7974/Test.scala b/test/files/run/t7974/Test.scala index 296ec32ee26a..53ec71bc2bc6 100644 --- a/test/files/run/t7974/Test.scala +++ b/test/files/run/t7974/Test.scala @@ -4,7 +4,7 @@ import scala.tools.partest.BytecodeTest import scala.tools.nsc.backend.jvm.AsmUtils import scala.tools.asm.util._ import scala.tools.nsc.util.stringFromWriter -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ object Test extends BytecodeTest { def show { diff --git a/test/files/run/t9097.scala b/test/files/run/t9097.scala index 49c0bbe79a19..49c9e2f2e5d8 100644 --- a/test/files/run/t9097.scala +++ b/test/files/run/t9097.scala @@ -28,6 +28,6 @@ object Test extends StoreReporterDirectTest { assert(!storeReporter.hasErrors, message = filteredInfos map (_.msg) mkString "; ") val out = baos.toString("UTF-8") // was 2 before the fix, the two PackageDefs for a would both contain the ClassDef for the closure - assert(out.lines.count(_ contains "def $anonfun$1(x$1: Int): String") == 1, out) + assert(out.lines.count(_ contains "def $anonfun$hihi$1(x$1: Int): String") == 1, out) } } diff --git a/test/files/run/t9516.scala b/test/files/run/t9516.scala new file mode 100644 index 000000000000..b3068dd1ff18 --- /dev/null +++ b/test/files/run/t9516.scala @@ -0,0 +1,52 @@ +object Test { + def main(args: Array[String]): Unit = { + intShiftLeftLongConstantFolded() + intShiftLeftLongAtRuntime() + intShiftLogicalRightLongConstantFolded() + intShiftLogicalRightLongAtRuntime() + intShiftArithmeticRightLongConstantFolded() + intShiftArithmeticRightLongAtRuntime() + } + + def intShiftLeftLongConstantFolded(): Unit = { + assert(0x01030507 << 36L == 271601776) + val r = 0x01030507 << 36L + assert(r == 271601776) + } + + def intShiftLeftLongAtRuntime(): Unit = { + var x: Int = 0x01030507 + var y: Long = 36L + assert(x << y == 271601776) + val r = x << y + assert(r == 271601776) + } + + def intShiftLogicalRightLongConstantFolded(): Unit = { + assert(0x90503010 >>> 36L == 151323393) + val r = 0x90503010 >>> 36L + assert(r == 151323393) + } + + def intShiftLogicalRightLongAtRuntime(): Unit = { + var x: Int = 0x90503010 + var y: Long = 36L + assert(x >>> y == 151323393) + val r = x >>> y + assert(r == 151323393) + } + + def intShiftArithmeticRightLongConstantFolded(): Unit = { + assert(0x90503010 >> 36L == -117112063) + val r = 0x90503010 >> 36L + assert(r == -117112063) + } + + def intShiftArithmeticRightLongAtRuntime(): Unit = { + var x: Int = 0x90503010 + var y: Long = 36L + assert(x >> y == -117112063) + val r = x >> y + assert(r == -117112063) + } +} diff --git a/test/files/run/various-flat-classpath-types.scala b/test/files/run/various-flat-classpath-types.scala index d39019e88562..bc54ffb6cce2 100644 --- a/test/files/run/various-flat-classpath-types.scala +++ b/test/files/run/various-flat-classpath-types.scala @@ -5,7 +5,7 @@ import java.io.{File => JFile, FileInputStream, FileOutputStream} import java.util.zip.{ZipEntry, ZipOutputStream} import scala.reflect.io.{Directory, File} -import scala.tools.nsc.classpath.FlatClassPath.RootPackage +import scala.tools.nsc.util.ClassPath.RootPackage import scala.tools.nsc.classpath.PackageNameUtils import scala.tools.nsc.io.Jar @@ -80,7 +80,6 @@ object Test { private val compiler = new scala.tools.nsc.MainClass private val appRunner = new scala.tools.nsc.MainGenericRunner - private val classPathImplFlag = "-YclasspathImpl:flat" private val javaClassPath = sys.props("java.class.path") // creates a test dir in a temporary dir containing compiled files of this test @@ -166,13 +165,13 @@ object Test { val classPath = mkPath(javaClassPath, binDir.path, zipsDir.path + "/Bin.zip", jarsDir.path + "/Bin.jar") val sourcePath = mkPath(srcDir.path, zipsDir.path + "/Src.zip", jarsDir.path + "/Src.jar") - compiler.process(Array(classPathImplFlag, "-cp", classPath, "-sourcepath", sourcePath, + compiler.process(Array("-cp", classPath, "-sourcepath", sourcePath, "-d", outDir.path, s"${srcDir.path}/Main.scala")) } private def runApp(): Unit = { val classPath = mkPath(javaClassPath, outDir.path, binDir.path, zipsDir.path + "/Bin.zip", jarsDir.path + "/Bin.jar") - appRunner.process(Array(classPathImplFlag, "-cp", classPath, "Main")) + appRunner.process(Array("-cp", classPath, "Main")) } private def createStandardSrcHierarchy(baseFileName: String): Unit = @@ -200,7 +199,7 @@ object Test { private def compileSrc(baseFileName: String, destination: JFile = outDir): Unit = { val srcDirPath = srcDir.path - compiler.process(Array(classPathImplFlag, "-cp", javaClassPath, "-d", destination.path, + compiler.process(Array("-cp", javaClassPath, "-d", destination.path, s"$srcDirPath/$baseFileName.scala", s"$srcDirPath/nested/Nested$baseFileName.scala")) } diff --git a/test/junit/scala/collection/convert/NullSafetyTest.scala b/test/junit/scala/collection/convert/NullSafetyTest.scala index de5481d9e2a7..173568408c19 100644 --- a/test/junit/scala/collection/convert/NullSafetyTest.scala +++ b/test/junit/scala/collection/convert/NullSafetyTest.scala @@ -7,7 +7,7 @@ import org.junit.Test import org.junit.experimental.runners.Enclosed import org.junit.runner.RunWith -import scala.collection.JavaConversions._ +import collection.convert.ImplicitConversions._ import scala.collection.JavaConverters._ import scala.collection.{mutable, concurrent} diff --git a/test/junit/scala/collection/mutable/PriorityQueueTest.scala b/test/junit/scala/collection/mutable/PriorityQueueTest.scala index a14f1bf4c802..faedcf11f002 100644 --- a/test/junit/scala/collection/mutable/PriorityQueueTest.scala +++ b/test/junit/scala/collection/mutable/PriorityQueueTest.scala @@ -13,6 +13,12 @@ class PriorityQueueTest { val elements = List.fill(1000)(scala.util.Random.nextInt(Int.MaxValue)) priorityQueue.enqueue(elements :_*) + @Test + def orderingReverseReverse() { + val pq = new mutable.PriorityQueue[Nothing]()((_,_)=>42) + assert(pq.ord eq pq.reverse.reverse.ord) + } + @Test def canSerialize() { val outputStream = new ByteArrayOutputStream() @@ -27,6 +33,7 @@ class PriorityQueueTest { val objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes)) val deserializedPriorityQueue = objectInputStream.readObject().asInstanceOf[PriorityQueue[Int]] + //correct sequencing is also tested here: assert(deserializedPriorityQueue.dequeueAll == elements.sorted.reverse) } } diff --git a/test/junit/scala/issues/RunTest.scala b/test/junit/scala/issues/RunTest.scala index 781f2ef343f7..0605947e630d 100644 --- a/test/junit/scala/issues/RunTest.scala +++ b/test/junit/scala/issues/RunTest.scala @@ -147,4 +147,16 @@ class RunTest extends ClearAfterClass { assertEquals(run[String](definitions("Object") + runCode), "hi" * 9) assertEquals(run[String](definitions("String") + runCode), "hi" * 9) // bridge method for clone generated } + + @Test + def classOfUnitConstant(): Unit = { + val code = + """abstract class A { def f: Class[_] } + |class C extends A { final val f = classOf[Unit] } + |val c = new C + |(c.f, (c: A).f) + """.stripMargin + val u = Void.TYPE + assertEquals(run[(Class[_], Class[_])](code), (u, u)) + } } diff --git a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala index fe43ed2f6a9a..389e5b2ead3b 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala @@ -206,6 +206,12 @@ object CodeGenTools { assert(actual == expected, s"\nFound : ${quote(actual)}\nExpected: ${quote(expected)}") } + def assertNoIndy(m: Method): Unit = assertNoIndy(m.instructions) + def assertNoIndy(l: List[Instruction]) = { + val indy = l collect { case i: InvokeDynamic => i } + assert(indy.isEmpty, indy) + } + def getSingleMethod(classNode: ClassNode, name: String): Method = convertMethod(classNode.methods.asScala.toList.find(_.name == name).get) diff --git a/test/junit/scala/tools/nsc/backend/jvm/analysis/NullnessAnalyzerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/analysis/NullnessAnalyzerTest.scala index ab05c15e8541..571d84c8726f 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/analysis/NullnessAnalyzerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/analysis/NullnessAnalyzerTest.scala @@ -17,7 +17,7 @@ import scala.tools.testing.ClearAfterClass import scala.tools.nsc.backend.jvm.opt.BytecodeUtils._ import AsmUtils._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ object NullnessAnalyzerTest extends ClearAfterClass.Clearable { var noOptCompiler = newCompiler(extraArgs = "-Yopt:l:none") diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/AnalyzerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/AnalyzerTest.scala index 11014f5e6434..930f7f2f1099 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/AnalyzerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/AnalyzerTest.scala @@ -18,7 +18,7 @@ import AsmUtils._ import BackendReporting._ import BytecodeUtils._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object AnalyzerTest extends ClearAfterClass.Clearable { diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/BTypesFromClassfileTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/BTypesFromClassfileTest.scala index 9e6a148dba72..aba0aab0386e 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/BTypesFromClassfileTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/BTypesFromClassfileTest.scala @@ -17,7 +17,7 @@ import ASMConverters._ import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ @RunWith(classOf[JUnit4]) class BTypesFromClassfileTest { diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/CallGraphTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/CallGraphTest.scala index b37b5efa7e5e..1d30e42e3c0a 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/CallGraphTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/CallGraphTest.scala @@ -21,7 +21,7 @@ import ASMConverters._ import AsmUtils._ import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object CallGraphTest extends ClearAfterClass.Clearable { diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala index a0b9d6b4edc9..12bfba71a804 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala @@ -24,7 +24,7 @@ import AsmUtils._ import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object ClosureOptimizerTest extends ClearAfterClass.Clearable { diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlineInfoTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlineInfoTest.scala index 261d6beb9651..23386bb5aeef 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlineInfoTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlineInfoTest.scala @@ -16,7 +16,7 @@ import scala.tools.testing.ClearAfterClass import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ object InlineInfoTest extends ClearAfterClass.Clearable { var compiler = newCompiler(extraArgs = "-Yopt:l:classpath") diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlineWarningTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlineWarningTest.scala index 90236265e6db..1597c75a7e22 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlineWarningTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlineWarningTest.scala @@ -24,7 +24,7 @@ import AsmUtils._ import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object InlineWarningTest extends ClearAfterClass.Clearable { @@ -103,12 +103,12 @@ class InlineWarningTest extends ClearAfterClass { val warns = List( """failed to determine if bar should be inlined: |The method bar()I could not be found in the class A or any of its parents. - |Note that the following parent classes are defined in Java sources (mixed compilation), no bytecode is available: A""".stripMargin, + |Note that the parent class A is defined in a Java source (mixed compilation), no bytecode is available.""".stripMargin, """B::flop()I is annotated @inline but could not be inlined: |Failed to check if B::flop()I can be safely inlined to B without causing an IllegalAccessError. Checking instruction INVOKESTATIC A.bar ()I failed: |The method bar()I could not be found in the class A or any of its parents. - |Note that the following parent classes are defined in Java sources (mixed compilation), no bytecode is available: A""".stripMargin) + |Note that the parent class A is defined in a Java source (mixed compilation), no bytecode is available.""".stripMargin) var c = 0 val List(b) = compile(scalaCode, List((javaCode, "A.java")), allowMessage = i => {c += 1; warns.tail.exists(i.msg contains _)}) diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerIllegalAccessTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerIllegalAccessTest.scala index 884027cd90cf..6460158e7152 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerIllegalAccessTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerIllegalAccessTest.scala @@ -16,7 +16,7 @@ import scala.tools.partest.ASMConverters import ASMConverters._ import AsmUtils._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object InlinerIllegalAccessTest extends ClearAfterClass.Clearable { diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerSeparateCompilationTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerSeparateCompilationTest.scala index 6562f81e4c08..075513a2b712 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerSeparateCompilationTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerSeparateCompilationTest.scala @@ -13,7 +13,7 @@ import scala.tools.partest.ASMConverters import ASMConverters._ import AsmUtils._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ object InlinerSeparateCompilationTest { val args = "-Yopt:l:classpath" diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala index 15665fca3313..e2a495fb2b1c 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala @@ -19,7 +19,7 @@ import AsmUtils._ import BackendReporting._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object InlinerTest extends ClearAfterClass.Clearable { @@ -428,7 +428,7 @@ class InlinerTest extends ClearAfterClass { """B::flop()I is annotated @inline but could not be inlined: |Failed to check if B::flop()I can be safely inlined to B without causing an IllegalAccessError. Checking instruction INVOKESTATIC A.bar ()I failed: |The method bar()I could not be found in the class A or any of its parents. - |Note that the following parent classes are defined in Java sources (mixed compilation), no bytecode is available: A""".stripMargin + |Note that the parent class A is defined in a Java source (mixed compilation), no bytecode is available.""".stripMargin var c = 0 val List(b) = compile(scalaCode, List((javaCode, "A.java")), allowMessage = i => {c += 1; i.msg contains warn}) @@ -833,7 +833,7 @@ class InlinerTest extends ClearAfterClass { val warn = """failed to determine if should be inlined: |The method ()V could not be found in the class A$Inner or any of its parents. - |Note that the following parent classes could not be found on the classpath: A$Inner""".stripMargin + |Note that the parent class A$Inner could not be found on the classpath.""".stripMargin var c = 0 @@ -955,18 +955,12 @@ class InlinerTest extends ClearAfterClass { val List(c, _, _) = compile(code) val t1 = getSingleMethod(c, "t1") - assert(t1.instructions forall { // indy is eliminated by push-pop - case _: InvokeDynamic => false - case _ => true - }) + assertNoIndy(t1) // the indy call is inlined into t, and the closure elimination rewrites the closure invocation to the body method assertInvoke(t1, "C", "C$$$anonfun$2") val t2 = getSingleMethod(c, "t2") - assert(t2.instructions forall { // indy is eliminated by push-pop - case _: InvokeDynamic => false - case _ => true - }) + assertNoIndy(t2) assertInvoke(t2, "M$", "M$$$anonfun$1") } @@ -1492,4 +1486,31 @@ class InlinerTest extends ClearAfterClass { // the forwarder C.f is inlined, so there's no invocation assertSameSummary(getSingleMethod(c, "f"), List(ICONST_1, IRETURN)) } + + @Test + def sd140(): Unit = { + val code = + """trait T { @inline def f = 0 } + |trait U extends T { @inline override def f = 1 } + |trait V extends T { def m = 0 } + |final class K extends V with U { override def m = super[V].m } + |class C { def t = (new K).f } + """.stripMargin + val c :: _ = compile(code) + assertSameSummary(getSingleMethod(c, "t"), List(NEW, "", ICONST_1, IRETURN)) // ICONST_1, U.f is inlined (not T.f) + } + + @Test + def inlineArrayForeach(): Unit = { + val code = + """class C { + | def consume(x: Int) = () + | def t(a: Array[Int]): Unit = a foreach consume + |} + """.stripMargin + val List(c) = compile(code) + val t = getSingleMethod(c, "t") + assertNoIndy(t) + assertInvoke(t, "C", "C$$$anonfun$1") + } } diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala index 0a9a26cda70b..dd7fbd9977da 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala @@ -16,7 +16,7 @@ import CodeGenTools._ import scala.tools.partest.ASMConverters import ASMConverters._ import scala.tools.testing.ClearAfterClass -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ object MethodLevelOptsTest extends ClearAfterClass.Clearable { var methodOptCompiler = newCompiler(extraArgs = "-Yopt:l:method") diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala index e2d03d8c62d2..8dd23ec3ce26 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala @@ -13,7 +13,7 @@ import scala.tools.asm.tree.ClassNode import scala.tools.nsc.backend.jvm.BTypes.{MethodInlineInfo, InlineInfo} import scala.tools.partest.ASMConverters import ASMConverters._ -import scala.collection.convert.decorateAsScala._ +import scala.collection.JavaConverters._ import scala.tools.testing.ClearAfterClass object ScalaInlineInfoTest extends ClearAfterClass.Clearable { diff --git a/test/junit/scala/tools/nsc/classpath/AggregateFlatClassPathTest.scala b/test/junit/scala/tools/nsc/classpath/AggregateClassPathTest.scala similarity index 89% rename from test/junit/scala/tools/nsc/classpath/AggregateFlatClassPathTest.scala rename to test/junit/scala/tools/nsc/classpath/AggregateClassPathTest.scala index 9a004d5e0eae..a7aca31ee38e 100644 --- a/test/junit/scala/tools/nsc/classpath/AggregateFlatClassPathTest.scala +++ b/test/junit/scala/tools/nsc/classpath/AggregateClassPathTest.scala @@ -10,6 +10,7 @@ import org.junit.runner.RunWith import org.junit.runners.JUnit4 import scala.reflect.io.VirtualFile import scala.tools.nsc.io.AbstractFile +import scala.tools.nsc.util.ClassPath /** * Tests whether AggregateFlatClassPath returns correct entries taken from @@ -17,14 +18,14 @@ import scala.tools.nsc.io.AbstractFile * (in the case of the repeated entry for a class or a source it returns the first one). */ @RunWith(classOf[JUnit4]) -class AggregateFlatClassPathTest { +class AggregateClassPathTest { - private class TestFlatClassPath extends FlatClassPath { + private abstract class TestClassPathBase extends ClassPath { override def packages(inPackage: String): Seq[PackageEntry] = unsupported override def sources(inPackage: String): Seq[SourceFileEntry] = unsupported override def classes(inPackage: String): Seq[ClassFileEntry] = unsupported - override def list(inPackage: String): FlatClassPathEntries = unsupported + override def list(inPackage: String): ClassPathEntries = unsupported override def findClassFile(name: String): Option[AbstractFile] = unsupported override def asClassPathStrings: Seq[String] = unsupported @@ -32,7 +33,7 @@ class AggregateFlatClassPathTest { override def asURLs: Seq[URL] = unsupported } - private case class TestClassPath(virtualPath: String, classesInPackage: EntryNamesInPackage*) extends TestFlatClassPath { + private case class TestClassPath(virtualPath: String, classesInPackage: EntryNamesInPackage*) extends TestClassPathBase { override def classes(inPackage: String): Seq[ClassFileEntry] = for { @@ -43,10 +44,10 @@ class AggregateFlatClassPathTest { override def sources(inPackage: String): Seq[SourceFileEntry] = Nil // we'll ignore packages - override def list(inPackage: String): FlatClassPathEntries = FlatClassPathEntries(Nil, classes(inPackage)) + override def list(inPackage: String): ClassPathEntries = ClassPathEntries(Nil, classes(inPackage)) } - private case class TestSourcePath(virtualPath: String, sourcesInPackage: EntryNamesInPackage*) extends TestFlatClassPath { + private case class TestSourcePath(virtualPath: String, sourcesInPackage: EntryNamesInPackage*) extends TestClassPathBase { override def sources(inPackage: String): Seq[SourceFileEntry] = for { @@ -57,7 +58,7 @@ class AggregateFlatClassPathTest { override def classes(inPackage: String): Seq[ClassFileEntry] = Nil // we'll ignore packages - override def list(inPackage: String): FlatClassPathEntries = FlatClassPathEntries(Nil, sources(inPackage)) + override def list(inPackage: String): ClassPathEntries = ClassPathEntries(Nil, sources(inPackage)) } private case class EntryNamesInPackage(inPackage: String)(val names: String*) @@ -88,7 +89,7 @@ class AggregateFlatClassPathTest { private def virtualFile(pathPrefix: String, inPackage: String, fileName: String, extension: String) = { val packageDirs = - if (inPackage == FlatClassPath.RootPackage) "" + if (inPackage == ClassPath.RootPackage) "" else inPackage.split('.').mkString("/", "/", "") new VirtualFile(fileName + extension, s"$pathPrefix$packageDirs/$fileName$extension") } @@ -101,12 +102,12 @@ class AggregateFlatClassPathTest { TestSourcePath(dir2, EntryNamesInPackage(pkg3)("J", "K", "L")) ) - AggregateFlatClassPath(partialClassPaths) + AggregateClassPath(partialClassPaths) } @Test def testGettingPackages: Unit = { - case class ClassPathWithPackages(packagesInPackage: EntryNamesInPackage*) extends TestFlatClassPath { + case class ClassPathWithPackages(packagesInPackage: EntryNamesInPackage*) extends TestClassPathBase { override def packages(inPackage: String): Seq[PackageEntry] = packagesInPackage.find(_.inPackage == inPackage).map(_.names).getOrElse(Nil) map PackageEntryImpl } @@ -115,7 +116,7 @@ class AggregateFlatClassPathTest { ClassPathWithPackages(EntryNamesInPackage(pkg1)("pkg1.c", "pkg1.b", "pkg1.a"), EntryNamesInPackage(pkg2)("pkg2.d", "pkg2.a", "pkg2.e")) ) - val cp = AggregateFlatClassPath(partialClassPaths) + val cp = AggregateClassPath(partialClassPaths) val packagesInPkg1 = Seq("pkg1.a", "pkg1.d", "pkg1.f", "pkg1.c", "pkg1.b") assertEquals(packagesInPkg1, cp.packages(pkg1).map(_.name)) @@ -156,7 +157,7 @@ class AggregateFlatClassPathTest { TestClassPath(dir4, EntryNamesInPackage(pkg2)("A", "H", "I")), TestClassPath(dir2, EntryNamesInPackage(pkg3)("J", "K", "L")) ) - val cp = AggregateFlatClassPath(partialClassPaths) + val cp = AggregateClassPath(partialClassPaths) val sourcesInPkg1 = Seq(sourceFileEntry(dir2, pkg1, "C"), sourceFileEntry(dir2, pkg1, "B"), @@ -190,7 +191,7 @@ class AggregateFlatClassPathTest { ) assertEquals(classesAndSourcesInPkg1, cp.list(pkg1).classesAndSources) - assertEquals(FlatClassPathEntries(Nil, Nil), cp.list(nonexistingPkg)) + assertEquals(ClassPathEntries(Nil, Nil), cp.list(nonexistingPkg)) } @Test diff --git a/test/junit/scala/tools/nsc/classpath/FlatClassPathResolverTest.scala b/test/junit/scala/tools/nsc/classpath/PathResolverBaseTest.scala similarity index 60% rename from test/junit/scala/tools/nsc/classpath/FlatClassPathResolverTest.scala rename to test/junit/scala/tools/nsc/classpath/PathResolverBaseTest.scala index 5dee488285c0..d3d4289d8b94 100644 --- a/test/junit/scala/tools/nsc/classpath/FlatClassPathResolverTest.scala +++ b/test/junit/scala/tools/nsc/classpath/PathResolverBaseTest.scala @@ -9,20 +9,17 @@ import org.junit._ import org.junit.rules.TemporaryFolder import org.junit.runner.RunWith import org.junit.runners.JUnit4 -import scala.annotation.tailrec -import scala.tools.nsc.io.AbstractFile import scala.tools.nsc.util.ClassPath import scala.tools.nsc.Settings -import scala.tools.util.FlatClassPathResolver import scala.tools.util.PathResolver @RunWith(classOf[JUnit4]) -class FlatClassPathResolverTest { +class PathResolverBaseTest { val tempDir = new TemporaryFolder() - private val packagesToTest = List(FlatClassPath.RootPackage, "scala", "scala.reflect", "scala.reflect.io") - private val classFilesToFind = List("scala.tools.util.FlatClassPathResolver", + private val packagesToTest = List(ClassPath.RootPackage, "scala", "scala.reflect", "scala.reflect.io") + private val classFilesToFind = List("scala.tools.util.PathResolver", "scala.reflect.io.AbstractFile", "scala.collection.immutable.List", "scala.Option", @@ -60,7 +57,7 @@ class FlatClassPathResolverTest { def deleteTempDir: Unit = tempDir.delete() private def createFlatClassPath(settings: Settings) = - new FlatClassPathResolver(settings).result + new PathResolver(settings).result @Test def testEntriesFromListOperationAgainstSeparateMethods: Unit = { @@ -70,7 +67,7 @@ class FlatClassPathResolverTest { val packages = classPath.packages(inPackage) val classes = classPath.classes(inPackage) val sources = classPath.sources(inPackage) - val FlatClassPathEntries(packagesFromList, classesAndSourcesFromList) = classPath.list(inPackage) + val ClassPathEntries(packagesFromList, classesAndSourcesFromList) = classPath.list(inPackage) val packageNames = packages.map(_.name).sorted val packageNamesFromList = packagesFromList.map(_.name).sorted @@ -95,52 +92,6 @@ class FlatClassPathResolverTest { packagesToTest foreach compareEntriesInPackage } - @Test - def testCreatedEntriesAgainstRecursiveClassPath: Unit = { - val flatClassPath = createFlatClassPath(settings) - val recursiveClassPath = new PathResolver(settings).result - - def compareEntriesInPackage(inPackage: String): Unit = { - - @tailrec - def traverseToPackage(packageNameParts: Seq[String], cp: ClassPath[AbstractFile]): ClassPath[AbstractFile] = { - packageNameParts match { - case Nil => cp - case h :: t => - cp.packages.find(_.name == h) match { - case Some(nestedCp) => traverseToPackage(t, nestedCp) - case _ => throw new Exception(s"There's no package $inPackage in recursive classpath - error when searching for '$h'") - } - } - } - - val packageNameParts = if (inPackage == FlatClassPath.RootPackage) Nil else inPackage.split('.').toList - val recursiveClassPathInPackage = traverseToPackage(packageNameParts, recursiveClassPath) - - val flatCpPackages = flatClassPath.packages(inPackage).map(_.name) - val pkgPrefix = PackageNameUtils.packagePrefix(inPackage) - val recursiveCpPackages = recursiveClassPathInPackage.packages.map(pkgPrefix + _.name) - assertEquals(s"Packages in package '$inPackage' on flat cp should be the same as on the recursive cp", - recursiveCpPackages, flatCpPackages) - - val flatCpSources = flatClassPath.sources(inPackage).map(_.name).sorted - val recursiveCpSources = recursiveClassPathInPackage.classes - .filter(_.source.nonEmpty) - .map(_.name).sorted - assertEquals(s"Source entries in package '$inPackage' on flat cp should be the same as on the recursive cp", - recursiveCpSources, flatCpSources) - - val flatCpClasses = flatClassPath.classes(inPackage).map(_.name).sorted - val recursiveCpClasses = recursiveClassPathInPackage.classes - .filter(_.binary.nonEmpty) - .map(_.name).sorted - assertEquals(s"Class entries in package '$inPackage' on flat cp should be the same as on the recursive cp", - recursiveCpClasses, flatCpClasses) - } - - packagesToTest foreach compareEntriesInPackage - } - @Test def testFindClassFile: Unit = { val classPath = createFlatClassPath(settings) diff --git a/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala index 812c298c48d6..8cc7aefdd34e 100644 --- a/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala +++ b/test/junit/scala/tools/nsc/symtab/SymbolTableForUnitTesting.scala @@ -3,11 +3,8 @@ package symtab import scala.reflect.ClassTag import scala.reflect.internal.{NoPhase, Phase, SomePhase} -import scala.tools.nsc.classpath.FlatClassPath -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.util.FlatClassPathResolver import scala.tools.util.PathResolver -import util.{ClassFileLookup, ClassPath} +import util.ClassPath import io.AbstractFile /** @@ -30,8 +27,7 @@ class SymbolTableForUnitTesting extends SymbolTable { override def isCompilerUniverse: Boolean = true - def classPath = platform.classPath - def flatClassPath: FlatClassPath = platform.flatClassPath + def classPath: ClassPath = platform.classPath object platform extends backend.Platform { val symbolTable: SymbolTableForUnitTesting.this.type = SymbolTableForUnitTesting.this @@ -39,22 +35,12 @@ class SymbolTableForUnitTesting extends SymbolTable { def platformPhases: List[SubComponent] = Nil - lazy val classPath: ClassPath[AbstractFile] = { - assert(settings.YclasspathImpl.value == ClassPathRepresentationType.Recursive, - "It's not possible to use the recursive classpath representation, when it's not the chosen classpath scanning method") - new PathResolver(settings).result - } - - private[nsc] lazy val flatClassPath: FlatClassPath = { - assert(settings.YclasspathImpl.value == ClassPathRepresentationType.Flat, - "It's not possible to use the flat classpath representation, when it's not the chosen classpath scanning method") - new FlatClassPathResolver(settings).result - } + private[nsc] lazy val classPath: ClassPath = new PathResolver(settings).result def isMaybeBoxed(sym: Symbol): Boolean = ??? def needCompile(bin: AbstractFile, src: AbstractFile): Boolean = ??? def externalEquals: Symbol = ??? - def updateClassPath(subst: Map[ClassFileLookup[AbstractFile], ClassFileLookup[AbstractFile]]): Unit = ??? + def updateClassPath(subst: Map[ClassPath, ClassPath]): Unit = ??? } object loaders extends symtab.SymbolLoaders { @@ -69,10 +55,7 @@ class SymbolTableForUnitTesting extends SymbolTable { class GlobalMirror extends Roots(NoSymbol) { val universe: SymbolTableForUnitTesting.this.type = SymbolTableForUnitTesting.this - def rootLoader: LazyType = settings.YclasspathImpl.value match { - case ClassPathRepresentationType.Flat => new loaders.PackageLoaderUsingFlatClassPath(FlatClassPath.RootPackage, flatClassPath) - case ClassPathRepresentationType.Recursive => new loaders.PackageLoader(classPath) - } + def rootLoader: LazyType = new loaders.PackageLoader(ClassPath.RootPackage, classPath) override def toString = "compiler mirror" } diff --git a/test/junit/scala/tools/nsc/util/ClassPathImplComparator.scala b/test/junit/scala/tools/nsc/util/ClassPathImplComparator.scala deleted file mode 100644 index f2926e3e176d..000000000000 --- a/test/junit/scala/tools/nsc/util/ClassPathImplComparator.scala +++ /dev/null @@ -1,143 +0,0 @@ -/* - * Copyright (c) 2014 Contributor. All rights reserved. - */ -package scala.tools.nsc.util - -import scala.reflect.io.AbstractFile -import scala.tools.nsc.Settings -import scala.tools.nsc.settings.ClassPathRepresentationType -import scala.tools.util.PathResolverFactory - -/** - * Simple application to compare efficiency of the recursive and the flat classpath representations - */ -object ClassPathImplComparator { - - private class TestSettings extends Settings { - val checkClasses = PathSetting("-checkClasses", "Specify names of classes which should be found separated with ;", "") - val requiredIterations = IntSetting("-requiredIterations", - "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) - val cpCreationRepetitions = IntSetting("-cpCreationRepetitions", - "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) - val cpLookupRepetitions = IntSetting("-cpLookupRepetitions", - "Repeat tests specified number of times (to check e.g. impact of caches)", 1, Some((1, Int.MaxValue)), (_: String) => None) - } - - private class DurationStats(name: String) { - private var sum = 0L - private var iterations = 0 - - def noteMeasuredTime(millis: Long): Unit = { - sum += millis - iterations += 1 - } - - def printResults(): Unit = { - val avg = if (iterations == 0) 0 else sum.toDouble / iterations - println(s"$name - total duration: $sum ms; iterations: $iterations; avg: $avg ms") - } - } - - private lazy val defaultClassesToFind = List( - "scala.collection.immutable.List", - "scala.Option", - "scala.Int", - "scala.collection.immutable.Vector", - "scala.util.hashing.MurmurHash3" - ) - - private val oldCpCreationStats = new DurationStats("Old classpath - create") - private val oldCpSearchingStats = new DurationStats("Old classpath - search") - - private val flatCpCreationStats = new DurationStats("Flat classpath - create") - private val flatCpSearchingStats = new DurationStats("Flat classpath - search") - - def main(args: Array[String]): Unit = { - - if (args contains "-help") - usage() - else { - val oldCpSettings = loadSettings(args.toList, ClassPathRepresentationType.Recursive) - val flatCpSettings = loadSettings(args.toList, ClassPathRepresentationType.Flat) - - val classesToCheck = oldCpSettings.checkClasses.value - val classesToFind = - if (classesToCheck.isEmpty) defaultClassesToFind - else classesToCheck.split(";").toList - - def doTest(classPath: => ClassFileLookup[AbstractFile], cpCreationStats: DurationStats, cpSearchingStats: DurationStats, - cpCreationRepetitions: Int, cpLookupRepetitions: Int)= { - - def createClassPaths() = (1 to cpCreationRepetitions).map(_ => classPath).last - def testClassLookup(cp: ClassFileLookup[AbstractFile]): Boolean = (1 to cpCreationRepetitions).foldLeft(true) { - case (a, _) => a && checkExistenceOfClasses(classesToFind)(cp) - } - - val cp = withMeasuredTime("Creating classpath", createClassPaths(), cpCreationStats) - val result = withMeasuredTime("Searching for specified classes", testClassLookup(cp), cpSearchingStats) - println(s"The end of the test case. All expected classes found = $result \n") - } - - (1 to oldCpSettings.requiredIterations.value) foreach { iteration => - if (oldCpSettings.requiredIterations.value > 1) - println(s"Iteration no $iteration") - - println("Recursive (old) classpath representation:") - doTest(PathResolverFactory.create(oldCpSettings).result, oldCpCreationStats, oldCpSearchingStats, - oldCpSettings.cpCreationRepetitions.value, oldCpSettings.cpLookupRepetitions.value) - - println("Flat classpath representation:") - doTest(PathResolverFactory.create(flatCpSettings).result, flatCpCreationStats, flatCpSearchingStats, - flatCpSettings.cpCreationRepetitions.value, flatCpSettings.cpLookupRepetitions.value) - } - - if (oldCpSettings.requiredIterations.value > 1) { - println("\nOld classpath - summary") - oldCpCreationStats.printResults() - oldCpSearchingStats.printResults() - - println("\nFlat classpath - summary") - flatCpCreationStats.printResults() - flatCpSearchingStats.printResults() - } - } - } - - /** - * Prints usage information - */ - private def usage(): Unit = - println("""Use classpath and sourcepath options like in the case of e.g. 'scala' command. - | There are also two additional options: - | -checkClasses Specify names of classes which should be found - | -requiredIterations Repeat tests specified count of times (to check e.g. impact of caches) - | Note: Option -YclasspathImpl will be set automatically for each case. - """.stripMargin.trim) - - private def loadSettings(args: List[String], implType: String) = { - val settings = new TestSettings() - settings.processArguments(args, processAll = true) - settings.YclasspathImpl.value = implType - if (settings.classpath.isDefault) - settings.classpath.value = sys.props("java.class.path") - settings - } - - private def withMeasuredTime[T](operationName: String, f: => T, durationStats: DurationStats): T = { - val startTime = System.currentTimeMillis() - val res = f - val elapsed = System.currentTimeMillis() - startTime - durationStats.noteMeasuredTime(elapsed) - println(s"$operationName - elapsed $elapsed ms") - res - } - - private def checkExistenceOfClasses(classesToCheck: Seq[String])(classPath: ClassFileLookup[AbstractFile]): Boolean = - classesToCheck.foldLeft(true) { - case (res, classToCheck) => - val found = classPath.findClass(classToCheck).isDefined - if (!found) - println(s"Class $classToCheck not found") // of course in this case the measured time will be affected by IO operation - found - } -} diff --git a/test/scaladoc/run/t9752.check b/test/scaladoc/run/t9752.check new file mode 100644 index 000000000000..daeafb8ecc7f --- /dev/null +++ b/test/scaladoc/run/t9752.check @@ -0,0 +1,5 @@ +List(Body(List(Paragraph(Chain(List(Summary(Text())))), Code(class A + + +class B)))) +Done. diff --git a/test/scaladoc/run/t9752.scala b/test/scaladoc/run/t9752.scala new file mode 100644 index 000000000000..b11c7f5c32a2 --- /dev/null +++ b/test/scaladoc/run/t9752.scala @@ -0,0 +1,28 @@ +import scala.tools.nsc.doc.model._ +import scala.tools.partest.ScaladocModelTest + +object Test extends ScaladocModelTest { + + override def code = s""" + /** + * Foo + * + * @example + * {{{ + * class A + * + * + * class B + * }}} + */ + object Foo + """ + + def scaladocSettings = "" + + def testModel(root: Package) = { + import access._ + val obj = root._object("Foo") + println(obj.comment.get.example) + } +} diff --git a/versions.properties b/versions.properties index d4112325d2fd..2ab66086fb5f 100644 --- a/versions.properties +++ b/versions.properties @@ -26,7 +26,7 @@ scala-xml.version.number=1.0.5 scala-parser-combinators.version.number=1.0.4 scala-swing.version.number=2.0.0-M2 scala-swing.version.osgi=2.0.0.M2 -jline.version=2.12.1 +jline.version=2.14.1 scala-asm.version=5.0.4-scala-3 # external modules, used internally (not shipped)