Skip to content

Commit 26f60b0

Browse files
committed
Add interfaces.AbstractFile
1 parent 1a57ca6 commit 26f60b0

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package dotty.tools.dotc.interfaces;
2+
3+
import java.io.File;
4+
import java.util.Optional;
5+
6+
/** An abstract file may either be a file on disk or a virtual file.
7+
*
8+
* Do not rely on the identity of instances of this class.
9+
*
10+
* User code should not implement this interface, but it may have to
11+
* manipulate objects of this type.
12+
*/
13+
public interface AbstractFile {
14+
/** The name of this file, note that two files may have the same name. */
15+
String name();
16+
17+
/** The path of this file, this might be a virtual path of an unspecified format. */
18+
String path();
19+
20+
/** If this is a real file on disk, a `java.io.File` that corresponds to this file.
21+
* Otherwise, an empty `Optional`.
22+
*/
23+
Optional<File> jfile();
24+
}

interfaces/src/main/java/dotty/tools/dotc/interfaces/CompilerCallback.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package dotty.tools.dotc.interfaces;
22

3-
import java.io.File;
4-
53
/** Set of callbacks called in response to events during the compilation process.
64
*
75
* You should implement this interface if you want to react to one or more of
@@ -19,12 +17,12 @@ public interface CompilerCallback {
1917
* @param className The name of this class.
2018
* Example: scala.collection.Seq$
2119
*/
22-
default void onClassGenerated(File source, File generatedClass, String className) {};
20+
default void onClassGenerated(SourceFile source, AbstractFile generatedClass, String className) {};
2321

2422
/** Called when every class for this file has been generated.
2523
*
2624
* @param source The source file.
2725
* Example: ./src/library/scala/collection/Seq.scala
2826
*/
29-
default void onSourceCompiled(File source) {};
27+
default void onSourceCompiled(SourceFile source) {};
3028
}

interfaces/src/main/java/dotty/tools/dotc/interfaces/SourceFile.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22

33
import java.io.File;
44

5-
/** A source file that may correspond to a file on disk but may also be virtual.
5+
/** A source file.
66
*
77
* User code should not implement this interface, but it may have to
88
* manipulate objects of this type.
99
*/
10-
public interface SourceFile {
11-
/** The name of this file, note that two files may have the same name. */
12-
String name();
13-
14-
/** The path of this file, this might be a virtual path of an unspecified format. */
15-
String path();
16-
10+
public interface SourceFile extends AbstractFile {
1711
/** The content of this file as seen by the compiler. */
1812
char[] content();
1913
}

src/dotty/tools/backend/jvm/GenBCode.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import dotty.tools.dotc
1313
import dotty.tools.dotc.backend.jvm.DottyPrimitives
1414
import dotty.tools.dotc.transform.Erasure
1515

16+
import dotty.tools.dotc.interfaces
17+
import java.util.Optional
18+
1619
import scala.reflect.ClassTag
1720
import dotty.tools.dotc.core._
1821
import Periods._
@@ -51,7 +54,17 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
5154

5255
var tree: Tree = _
5356

54-
val sourceJFile: JFile = ctx.compilationUnit.source.file.file
57+
val sourceFile = ctx.compilationUnit.source
58+
59+
/** Convert a `scala.reflect.io.AbstractFile` into a
60+
* `dotty.tools.dotc.interfaces.AbstractFile`.
61+
*/
62+
private[this] def convertAbstractFile(absfile: scala.reflect.io.AbstractFile): interfaces.AbstractFile =
63+
new interfaces.AbstractFile {
64+
override def name = absfile.name
65+
override def path = absfile.path
66+
override def jfile = Optional.ofNullable(absfile.file)
67+
}
5568

5669
final class PlainClassBuilder(cunit: CompilationUnit) extends SyncAndTryBuilder(cunit)
5770

@@ -307,7 +320,7 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
307320
// Statistics.stopTimer(BackendStats.bcodeTimer, bcodeStart)
308321

309322
if (ctx.compilerCallback != null)
310-
ctx.compilerCallback.onSourceCompiled(sourceJFile)
323+
ctx.compilerCallback.onSourceCompiled(sourceFile)
311324

312325
/* TODO Bytecode can be verified (now that all classfiles have been written to disk)
313326
*
@@ -373,10 +386,9 @@ class GenBCodePipeline(val entryPoints: List[Symbol], val int: DottyBackendInter
373386
else getFileForClassfile(outFolder, jclassName, ".class")
374387
bytecodeWriter.writeClass(jclassName, jclassName, jclassBytes, outFile)
375388

376-
val outJFile = outFile.file
377389
val className = jclassName.replace('/', '.')
378390
if (ctx.compilerCallback != null)
379-
ctx.compilerCallback.onClassGenerated(sourceJFile, outJFile, className)
391+
ctx.compilerCallback.onClassGenerated(sourceFile, convertAbstractFile(outFile), className)
380392
}
381393
catch {
382394
case e: FileConflictException =>

src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import Chars._
1111
import ScriptSourceFile._
1212
import Positions._
1313

14+
import java.util.Optional
15+
1416
object ScriptSourceFile {
1517
@sharable private val headerPattern = Pattern.compile("""^(::)?!#.*(\r|\n|\r\n)""", Pattern.MULTILINE)
1618
private val headerStarts = List("#!", "::#!")
@@ -43,6 +45,7 @@ case class SourceFile(file: AbstractFile, content: Array[Char]) extends interfac
4345

4446
override def name = file.name
4547
override def path = file.path
48+
override def jfile = Optional.ofNullable(file.file)
4649

4750
override def equals(that : Any) = that match {
4851
case that : SourceFile => file.path == that.file.path && start == that.start

test/test/InterfaceEntryPointTest.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package test
33
import org.junit.Test
44
import org.junit.Assert._
55
import dotty.tools.dotc.interfaces._
6-
import java.io.File
76
import scala.collection.mutable.ListBuffer
87

98
/** Test that demonstrates how to use dotty-interfaces
@@ -53,8 +52,9 @@ class InterfaceEntryPointTest {
5352
private val pathsBuffer = new ListBuffer[String]
5453
def paths = pathsBuffer.toList
5554

56-
override def onSourceCompiled(source: File): Unit = {
57-
pathsBuffer += source.getPath
55+
override def onSourceCompiled(source: SourceFile): Unit = {
56+
if (source.jfile.isPresent)
57+
pathsBuffer += source.jfile.get.getPath
5858
}
5959
}
6060
}

test/test/OtherEntryPointsTest.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package test
33
import org.junit.Test
44
import org.junit.Assert._
55
import dotty.tools.dotc.Main
6-
import dotty.tools.dotc.interfaces.CompilerCallback
6+
import dotty.tools.dotc.interfaces.{CompilerCallback, SourceFile}
77
import dotty.tools.dotc.reporting._
88
import dotty.tools.dotc.core.Contexts._
99
import java.io.File
@@ -58,8 +58,9 @@ class OtherEntryPointsTest {
5858
private val pathsBuffer = new ListBuffer[String]
5959
def paths = pathsBuffer.toList
6060

61-
override def onSourceCompiled(source: File): Unit = {
62-
pathsBuffer += source.getPath
61+
override def onSourceCompiled(source: SourceFile): Unit = {
62+
if (source.jfile.isPresent)
63+
pathsBuffer += source.jfile.get.getPath
6364
}
6465
}
6566
}

0 commit comments

Comments
 (0)