-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaIterableTestMacro.test.scala
More file actions
56 lines (47 loc) · 1.65 KB
/
JavaIterableTestMacro.test.scala
File metadata and controls
56 lines (47 loc) · 1.65 KB
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
package org.encalmo.utils
import JavaIterableUtils.*
import scala.quoted.*
object JavaIterableTestMacro {
inline def testMaybeVisitJavaIterable[A](inline value: A): String = {
${ testMaybeVisitJavaIterableImpl[A]('{ value }) }
}
def testMaybeVisitJavaIterableImpl[A: Type](valueExpr: Expr[A])(using Quotes): Expr[String] = {
given cache: StatementsCache = new StatementsCache
testMaybeVisitJavaIterable2Impl[A](valueExpr)
}
def testMaybeVisitJavaIterable2Impl[A: Type](valueExpr: Expr[A])(using cache: StatementsCache): Expr[String] = {
given cache.quotes.type = cache.quotes
import cache.quotes.reflect.*
val bufferRef = cache.getValueRefOfExpr("buffer", '{ collection.mutable.ListBuffer.empty[String] })
TypeRepr.of[A] match {
case TypeReprIsJavaIterable(tpe) =>
cache.put(
buildIterableLoop(
TypeNameUtils.valueNameOf(tpe),
tpe,
valueExpr.asTerm,
functionOnItem = { (tpe, valueTerm, indexTerm) =>
bufferRef.methodCall(
"append",
List(
StringUtils.concat(
StringUtils.applyToString(valueTerm),
Literal(StringConstant(" at ")),
StringUtils.applyToString(indexTerm)
)
)
)
}
)
)
case _ =>
report.warning("not a java iterable type")
}
cache.put {
bufferRef.methodCall("mkString", List(Literal(StringConstant(", "))))
}
val result = cache.asTerm
// report.warning(result.show(using Printer.TreeCode))
result.asExprOf[String]
}
}