-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTestCallback.scala
169 lines (152 loc) · 5.11 KB
/
TestCallback.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Taken from https://github.com/sbt/zinc/blob/aa1c04f445092e87f76aaceee4da61ea0724419e/internal/zinc-testing/src/main/scala/xsbti/TestCallback.scala
package xsbti
import java.io.File
import java.nio.file.Path
import scala.collection.mutable.ArrayBuffer
import xsbti.VirtualFileRef
import xsbti.api.ClassLike
import xsbti.api.DependencyContext
import DependencyContext._
import java.{util => ju}
import ju.Optional
class TestCallback extends AnalysisCallback2 {
case class TestUsedName(name: String, scopes: ju.EnumSet[UseScope])
val classDependencies = new ArrayBuffer[(String, String, DependencyContext)]
val binaryDependencies =
new ArrayBuffer[(Path, String, String, VirtualFileRef, DependencyContext)]
val productClassesToSources =
scala.collection.mutable.Map.empty[Path, VirtualFileRef]
val usedNamesAndScopes = scala.collection.mutable.Map
.empty[String, Set[TestUsedName]]
.withDefaultValue(Set.empty)
val classNames = scala.collection.mutable.Map
.empty[VirtualFileRef, Set[(String, String)]]
.withDefaultValue(Set.empty)
val apis: scala.collection.mutable.Map[VirtualFileRef, Seq[ClassLike]] =
scala.collection.mutable.Map.empty
def usedNames = usedNamesAndScopes.view.mapValues(_.map(_.name)).toMap
override def startSource(source: File): Unit = ???
override def startSource(source: VirtualFile): Unit = {
assert(
!apis.contains(source),
s"startSource can be called only once per source file: $source"
)
apis(source) = Seq.empty
}
override def binaryDependency(
binary: File,
name: String,
fromClassName: String,
source: File,
context: DependencyContext
): Unit = ???
override def binaryDependency(
binary: Path,
name: String,
fromClassName: String,
source: VirtualFileRef,
context: DependencyContext
): Unit = {
binaryDependencies += ((binary, name, fromClassName, source, context))
}
override def generatedNonLocalClass(
source: File,
module: File,
binaryClassName: String,
srcClassName: String
): Unit = ???
override def generatedNonLocalClass(
sourceFile: VirtualFileRef,
classFile: Path,
binaryClassName: String,
srcClassName: String
): Unit = {
productClassesToSources += ((classFile, sourceFile))
classNames(sourceFile) += ((srcClassName, binaryClassName))
()
}
override def generatedLocalClass(source: File, module: File): Unit = ???
override def generatedLocalClass(
sourceFile: VirtualFileRef,
classFile: Path
): Unit = {
productClassesToSources += ((classFile, sourceFile))
()
}
override def classDependency(
onClassName: String,
sourceClassName: String,
context: DependencyContext
): Unit = {
if (onClassName != sourceClassName)
classDependencies += ((onClassName, sourceClassName, context))
}
override def usedName(
className: String,
name: String,
scopes: ju.EnumSet[UseScope]
): Unit = {
usedNamesAndScopes(className) += TestUsedName(name, scopes)
}
override def api(source: File, classApi: ClassLike): Unit = ???
override def api(source: VirtualFileRef, classApi: ClassLike): Unit = {
apis(source) = classApi +: apis(source)
}
override def problem(
category: String,
pos: xsbti.Position,
message: String,
severity: xsbti.Severity,
reported: Boolean
): Unit = ()
override def problem2(
category: String,
pos: Position,
msg: String,
severity: Severity,
reported: Boolean,
rendered: Optional[String],
diagnosticCode: Optional[xsbti.DiagnosticCode],
diagnosticRelatedInformation: ju.List[xsbti.DiagnosticRelatedInformation],
actions: ju.List[xsbti.Action]
): Unit = ()
override def dependencyPhaseCompleted(): Unit = ()
override def apiPhaseCompleted(): Unit = ()
override def enabled(): Boolean = true
override def mainClass(source: File, className: String): Unit = ()
override def mainClass(source: VirtualFileRef, className: String): Unit = ???
override def classesInOutputJar(): java.util.Set[String] = ???
override def getPickleJarPair(): java.util.Optional[xsbti.T2[Path, Path]] =
???
override def isPickleJava(): Boolean = ???
}
object TestCallback {
case class ExtractedClassDependencies(
memberRef: Map[String, Set[String]],
inheritance: Map[String, Set[String]],
localInheritance: Map[String, Set[String]]
)
object ExtractedClassDependencies {
def fromPairs(
memberRefPairs: collection.Seq[(String, String)],
inheritancePairs: collection.Seq[(String, String)],
localInheritancePairs: collection.Seq[(String, String)]
): ExtractedClassDependencies = {
ExtractedClassDependencies(
pairsToMultiMap(memberRefPairs),
pairsToMultiMap(inheritancePairs),
pairsToMultiMap(localInheritancePairs)
)
}
private def pairsToMultiMap[A, B](
pairs: collection.Seq[(A, B)]
): Map[A, Set[B]] = {
pairs
.groupBy(_._1)
.view
.mapValues(values => values.map(_._2).toSet)
.toMap
.withDefaultValue(Set.empty)
}
}
}