-
Notifications
You must be signed in to change notification settings - Fork 3
/
Factory.scala
539 lines (502 loc) · 20.6 KB
/
Factory.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package com.thoughtworks.feature
import com.github.ghik.silencer.silent
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
import com.thoughtworks.Extractor._
import scala.annotation.meta.{companionObject, getter}
import scala.annotation.{StaticAnnotation, compileTimeOnly, tailrec}
import scala.collection.generic.Growable
import scala.collection.mutable
import scala.collection.mutable.{ArrayBuffer, ListBuffer}
/** A factory to create new instances, especially dynamic mix-ins.
*
* @note Factories may create types that contains refinement types
* {{{
* trait SomeBuilder {
* type A
* def makeSome(a: A) = Some(a)
* }
* val someBuilder = Factory[SomeBuilder { type A = Int }].newInstance()
* someBuilder.makeSome(42) should be(Some(42))
* }}}
*
* @note Factories may be nested
*
* {{{
* import com.thoughtworks.feature.Factory.inject
* import com.thoughtworks.feature.Factory.Factory1
* import com.thoughtworks.feature.ByName.`=>`
* trait Outer {
* trait AbstractParameterApi
* type AbstractParameter <: AbstractParameterApi
*
* trait InnerApi {
* def foo: AbstractParameter
* }
* type Inner <: InnerApi
*
* @inject val innerFactory: Factory1[`=>`[AbstractParameter], Inner]
* }
*
* val outer = Factory[Outer].newInstance()
* outer.innerFactory.newInstance(new outer.AbstractParameterApi {}) should be(an[outer.Inner])
* }}}
*
* @note [[Factory.inject @inject]] works on implicit abstract methods as well.
*
* {{{
* import com.thoughtworks.feature.Factory.inject
* trait Foo[A] {
* @inject implicit def orderingA: Ordering[A]
* }
* Factory[Foo[Int]].newInstance().orderingA should be(implicitly[Ordering[Int]])
* }}}
*
* @example Given a trait that contains an abstract method annotated as [[Factory.inject @inject]].
*
* {{{
* import com.thoughtworks.feature.Factory.inject
* trait Foo[A] {
* @inject def orderingA: Ordering[A]
* }
* }}}
*
* When creating a factory for the trait
*
* {{{
* val factory = Factory[Foo[Int]]
* }}}
*
* Then the `@inject` method will be replaced to an implicit value.
*
* {{{
* val foo = factory.newInstance()
* foo.orderingA should be(implicitly[Ordering[Int]])
* }}}
*
* It will not compile if no implicit value found.
*
* For example, `Foo[Symbol]` requires an implicit value of type `Ordering[Symbol]`, which is not availble.
*
* {{{
* "Factory[Foo[Symbol]]" shouldNot compile
* }}}
*
* If the trait does not contain abstract methods other than `@inject` methods,
* then the factory type class is a [[Factory.Factory0]],
* which can be summoned by [[Predef.implicitly]],
*
* {{{
* val nullaryFactory = implicitly[Factory.Factory0[Foo[Int]]]
* }}}
*
* and [[newInstance]] method is available on the [[Factory.Factory0]] as well.
*
* {{{
* nullaryFactory.newInstance().orderingA should be(implicitly[Ordering[Int]])
* }}}
*
*
* @example Given two traits that have no abstract member.
*
* {{{
* trait Foo
* trait Bar
* }}}
*
* When creating a factory for mix-in type of the two types.
*
* {{{
* val factory = Factory[Foo with Bar]
* }}}
*
* Then the [[newInstance]] method of the factory should accept no parameters.
*
* {{{
* val fooBar: Foo with Bar = factory.newInstance()
* fooBar should be(a[Foo])
* fooBar should be(a[Bar])
* }}}
*
* @example Given a trait that has abstract members.
* {{{
* trait Foo {
* val myValue: String
* var myVariable: Long
* def myMethod0(): Option[Double]
* def myMethod2(p0: Int, p1: Int): Int
* def myCurriedMethod(seq: Seq[Int])(mapper: Int => String): Seq[String]
*
* // Methods without pararmeters is disabled for now, due to https://github.com/scala/bug/issues/10647
* // def myByNameMethod: Option[Int]
* }
* }}}
* When creating a factory for the trait.
* {{{
* val factory = Factory[Foo]
* }}}
* Then the [[newInstance]] method of the factory should accept named arguments according to abstract members.
* {{{
* val createdFromNamedArguments: Foo = factory.newInstance(
* myValue = "string value",
* myVariable = 42L,
* myMethod0 = () => Some(0.5),
* myMethod2 = _ + _,
* myCurriedMethod = seq => mapper => seq.map(mapper)
* )
* createdFromNamedArguments.myValue should be("string value")
* createdFromNamedArguments.myVariable should be(42L)
*
* createdFromNamedArguments.myMethod0() should be(Some(0.5))
* createdFromNamedArguments.myMethod0() shouldNot be theSameInstanceAs createdFromNamedArguments.myMethod0()
*
* createdFromNamedArguments.myMethod2(1000, 24) should be(1024)
*
* createdFromNamedArguments.myCurriedMethod(Seq(2, 3, 4))(_.toString) should be(Seq("2", "3", "4"))
*
* }}}
* When using unnamed parameters, the parameters should be passed in alphabetical order
* {{{
* val createdFromUnnamedArguments: Foo = factory.newInstance(
* seq => mapper => seq.map(mapper), // myCurriedMethod
* () => Some(0.5), // myMethod0
* _ + _, // myMethod2
* "string value", // myValue
* 42L // myVariable
* )
* }}}
*
* @note This [[Factory]] disallows creating types that has an abstract member whose type depends on nested types.
*
* Given an abstract `val inner` whose type is the `Inner`,
*
* {{{
* trait Outer {
* trait Inner
* val inner: Option[Inner]
* }
* }}}
*
* then the attempt to create factory for `Outer`,
* like `Factory[Outer].newInstance(inner = None)`,
* will cause a `value inner has incompatible type` compile error.
*
* @note However, if the nested type is an alias to another type outside of the type to create, then it is allowed
*
* {{{
* trait Outer {
* type Inner = String
* val inner: Option[Inner]
* }
*
* val outer: Outer = Factory[Outer].newInstance(inner = Some("my value"))
* outer.inner should be(Some("my value"))
* }}}
* @author 杨博 (Yang Bo) <pop.atry@gmail.com>
*/
trait Factory[Output] extends Serializable {
/** A function type that returns `Output`.
*
* The parameter types are all abstract members in `Output`
*/
type Constructor
/** Returns an instance of `Output`, which overrides abstract members in `Output` according to parameters pass to this [[newInstance]] method. */
val newInstance: Constructor
}
private[feature] trait LowPriorityFactory {
implicit def factoryLt[Output, Constructor0, LubConstructor](
implicit factory: Factory.Aux[Output, Constructor0],
asLt: Factory.Aux[Output, Constructor0] <:< Factory.Lt[Output, LubConstructor]
): Factory.Lt[Output, LubConstructor] = {
asLt(factory)
}
}
object Factory extends LowPriorityFactory {
@getter
final class inject extends StaticAnnotation
type Aux[Output, Constructor0] = Factory[Output] {
type Constructor = Constructor0
}
type Lt[Output, +Constructor0] = Factory[Output] {
type Constructor <: Constructor0
}
type Factory0[Output] = Lt[Output, () => Output]
type Factory1[-Parameter0, Output] = Lt[Output, Parameter0 => Output]
type Factory2[-Parameter0, -Parameter1, Output] = Lt[Output, (Parameter0, Parameter1) => Output]
type Factory3[-Parameter0, -Parameter1, -Parameter2, Output] =
Lt[Output, (Parameter0, Parameter1, Parameter2) => Output]
type Factory4[-Parameter0, -Parameter1, -Parameter2, -Parameter3, Output] =
Lt[Output, (Parameter0, Parameter1, Parameter2, Parameter3) => Output]
type Factory5[-Parameter0, -Parameter1, -Parameter2, -Parameter3, -Parameter4, Output] =
Lt[Output, (Parameter0, Parameter1, Parameter2, Parameter3, Parameter4) => Output]
type Factory6[-Parameter0, -Parameter1, -Parameter2, -Parameter3, -Parameter4, -Parameter5, Output] =
Lt[Output, (Parameter0, Parameter1, Parameter2, Parameter3, Parameter4, Parameter5) => Output]
def make[Output, Constructor0](constructor: Constructor0): Factory.Aux[Output, Constructor0] = new Factory[Output] {
type Constructor = Constructor0
val newInstance: Constructor0 = constructor
}
implicit def apply[Output]: Factory[Output] = macro Macros.apply[Output]
private[Factory] final class Macros(val c: whitebox.Context) {
import c.universe._
private def unzip5[A, B, C, D, E](xs: Traversable[(A, B, C, D, E)]): (List[A], List[B], List[C], List[D], List[E]) =
xs.foldRight[(List[A], List[B], List[C], List[D], List[E])]((Nil, Nil, Nil, Nil, Nil)) { (x, res) =>
val (a, b, c, d, e) = x
(a :: res._1, b :: res._2, c :: res._3, d :: res._4, e :: res._5)
}
@(silent @companionObject)
final case class SelfTypes(allTypes: List[Type], classTypes: List[Type], refinedScopes: Seq[Scope])
private def selfTypes(t: Type): SelfTypes = {
val allTypeBuilder = new ListBuffer[Type]
val classTypeBuilder = new ListBuffer[Type]
val refinedScopes = new ArrayBuffer[Scope]
val parts = scala.collection.mutable.HashSet.empty[Type]
def go(t: Type): Unit = {
val dealiased = t.dealias
if (!parts(dealiased)) {
parts += dealiased
dealiased match {
case RefinedType(superTypes, refinedScope) =>
superTypes.foreach(go)
if (refinedScope.nonEmpty) {
refinedScopes += refinedScope
allTypeBuilder += dealiased
}
case typeRef: TypeRef =>
val symbol = dealiased.typeSymbol
if (symbol.isClass) {
val selfType = symbol.asClass.selfType.asSeenFrom(dealiased, symbol)
go(selfType)
}
classTypeBuilder += dealiased
allTypeBuilder += dealiased
case _ =>
classTypeBuilder += dealiased
allTypeBuilder += dealiased
}
}
}
go(t)
SelfTypes(allTypeBuilder.result(), classTypeBuilder.result(), refinedScopes.result())
}
private def demixin(t: Type): List[Type] = {
val builder = new ListBuffer[Type]
def go(t: Type): Unit = {
val dealiased = t.dealias
dealiased match {
case RefinedType(superTypes, refinedScope) if refinedScope.isEmpty =>
for (superType <- superTypes) {
go(superType)
}
case _ =>
builder += dealiased
}
}
go(t)
builder.result()
}
private val injectType = typeOf[inject]
private def isAbstractType[Output: WeakTypeTag](symbol: c.universe.TypeSymbol): Boolean = {
symbol.isAbstract && !symbol.isClass
}
def apply[Output: WeakTypeTag]: Tree = {
val output = weakTypeOf[Output]
val flattenSelfTypes = selfTypes(output)
val componentTypes = demixin(glb(flattenSelfTypes.classTypes))
val demixinTypes = demixin(glb(flattenSelfTypes.allTypes))
val linearOutput = internal.refinedType(demixinTypes, c.internal.enclosingOwner)
val linearSymbol = linearOutput.typeSymbol
val linearThis = internal.thisType(linearSymbol)
val mixinClassName = c.freshName(c.internal.enclosingOwner.name.encodedName.toTypeName)
/** An [[Untyper]] that produces a [[Tree]] used inside the generated anonymous class. */
def internalUntyper = new Untyper[c.universe.type](c.universe) {
private def replaceThisValue: PartialFunction[Type, Tree] = {
case ThisType(symbol) if symbol == linearSymbol =>
This(mixinClassName)
}
override def singletonValue: PartialFunction[Type, Tree] = {
replaceThisValue.orElse(super.singletonValue)
}
}
/** An [[Untyper]] that produces a [[Tree]] used outside the generated anonymous class. */
def externalUntyper = new Untyper[c.universe.type](c.universe) {
private def replaceThisValue: PartialFunction[Type, Tree] = {
case tt @ ThisType(symbol) if symbol == linearSymbol =>
TypeTree(linearOutput)
}
override def singletonValue: PartialFunction[Type, Tree] = {
replaceThisValue.orElse(super.singletonValue)
}
}
val injectedNames = (for {
baseClass <- linearOutput.baseClasses.reverse
member <- baseClass.info.decls
if member.isTerm && {
internal.initialize(member)
member.annotations.exists { a =>
a.tree.tpe <:< injectType
}
}
} yield member.name)(collection.breakOut(Set.canBuildFrom))
val injects = for {
injectedName <- injectedNames
} yield {
val methodName = injectedName.toTermName
val memberSymbol = linearOutput.member(methodName).asTerm
val methodType = memberSymbol.infoIn(linearThis)
val resultTypeTree: Tree = internalUntyper.untype(methodType.finalResultType)
val modifiers = Modifiers(
Flag.OVERRIDE |
(if (memberSymbol.isImplicit) Flag.IMPLICIT else NoFlags) |
(if (memberSymbol.isLazy) Flag.LAZY else NoFlags)
)
val result =
if (memberSymbol.isVar || memberSymbol.setter != NoSymbol) {
q"""$modifiers var $methodName = {
val $methodName = ()
_root_.com.thoughtworks.feature.The.apply[$resultTypeTree].value
}
"""
} else if (memberSymbol.isVal || memberSymbol.isGetter || memberSymbol.isStable) {
q"""
$modifiers val $methodName = {
val $methodName = ()
_root_.com.thoughtworks.feature.The.apply[$resultTypeTree].value
}
"""
} else {
val argumentTrees = methodType.paramLists.map(_.map { argumentSymbol =>
if (argumentSymbol.asTerm.isImplicit) {
q"implicit val ${argumentSymbol.name.toTermName}: ${internalUntyper.untype(argumentSymbol.info)}"
} else {
q"val ${argumentSymbol.name.toTermName}: ${internalUntyper.untype(argumentSymbol.info)}"
}
})
val typeParameterTrees = methodType.typeParams.map { typeParamSymbol =>
internalUntyper.typeDefinition(linearThis)(typeParamSymbol.asType)
}
q"""
$modifiers def $methodName[..$typeParameterTrees](...$argumentTrees) = {
val $methodName = ()
_root_.com.thoughtworks.feature.The.apply[$resultTypeTree].value
}
"""
}
// c.info(c.enclosingPosition, show(result), true)
result
}
val zippedProxies: Array[(Tree, Option[Tree], Tree, Tree, Tree)] = for {
member <- linearOutput.members.toArray.sortBy(_.name.toString)
if !injectedNames(member.name) && member.isTerm && member.isAbstract && !member.asTerm.isSetter
} yield {
val memberSymbol = member.asTerm
val methodName = memberSymbol.name.toTermName
val argumentName = c.freshName(methodName)
val methodType = memberSymbol.infoIn(linearThis)
val resultTypeTree: Tree = externalUntyper.untype(methodType.finalResultType)
if (memberSymbol.isVar || memberSymbol.setter != NoSymbol) {
(q"override var $methodName = $argumentName",
None,
resultTypeTree,
q"val $argumentName: $resultTypeTree",
q"val $methodName: $resultTypeTree")
} else if (memberSymbol.isVal || memberSymbol.isGetter || memberSymbol.isStable) {
(q"override val $methodName = $argumentName",
None,
resultTypeTree,
q"val $argumentName: $resultTypeTree",
q"val $methodName: $resultTypeTree")
} else {
val (argumentTrees: List[List[ValDef]],
argumentTypeTrees: List[List[Tree]],
argumentIdTrees: List[List[Ident]]) =
methodType.paramLists.map { parameterList =>
parameterList.map { argumentSymbol =>
val argumentTypeTree: Tree = externalUntyper.untype(argumentSymbol.info)
val argumentName = argumentSymbol.name.toTermName
val argumentTree = if (argumentSymbol.asTerm.isImplicit) {
q"implicit val $argumentName: $argumentTypeTree"
} else {
q"val $argumentName: $argumentTypeTree"
}
(argumentTree, argumentTypeTree, Ident(argumentName))
}.unzip3
}.unzip3
val functionTypeTree = if (argumentTypeTrees.isEmpty) {
tq"${definitions.ByNameParamClass}[$resultTypeTree]"
} else {
argumentTypeTrees.foldRight(resultTypeTree) { (arguments, result) =>
tq"..$arguments => $result"
}
}
val typeParameterTrees = methodType.typeParams.map { typeParamSymbol =>
internalUntyper.typeDefinition(linearThis)(typeParamSymbol.asType)
}
val earlyName = c.freshName(methodName)
val overrideDef = {
q"override def $methodName[..$typeParameterTrees](...$argumentTrees) = $earlyName(...$argumentIdTrees)"
}
(q"private val $earlyName = $argumentName",
Some(overrideDef),
functionTypeTree,
q"val $argumentName: $functionTypeTree",
q"val $methodName: $functionTypeTree")
}
}
val (earlyProxies, lateProxyOptions, parameterTypeTrees, parameterTrees, refinedTree) = unzip5(zippedProxies)
val lateProxies = lateProxyOptions.collect { case Some(proxy) => proxy }
// val (defProxies, valProxies) = proxies.partition(_.isDef)
val typeMembers = for {
owner <- linearThis.baseClasses
member <- owner.info.decls
if member.isType
} yield member.asType
val groupedTypeSymbols = typeMembers.groupBy(_.name.encodedName.toTypeName)
def overrideType(name: TypeName, members: List[TypeSymbol]): Tree = {
val glbType = glb(members.map { memberSymbol =>
memberSymbol.infoIn(linearThis)
})
val typeParameterTrees = glbType.typeParams.map { typeParamSymbol =>
internalUntyper.typeDefinition(linearThis)(typeParamSymbol.asType)
}
val TypeBounds(_, lowerBound) = glbType.resultType
val result = q"override type $name[..$typeParameterTrees] = ${internalUntyper.untype(glb(demixin(lowerBound)))}"
// c.info(c.enclosingPosition, show(result), true)
result
}
val overridenTypes = for {
(name, members) <- groupedTypeSymbols
if members.forall(isAbstractType)
} yield overrideType(name, members)
val refinementTrees = for {
scope <- flattenSelfTypes.refinedScopes
symbol <- scope
} yield {
internalUntyper.definition(linearThis)(symbol)
}
val makeNew = TermName(c.freshName("makeNew"))
val constructorMethod = TermName(c.freshName("constructor"))
val newInstance = TermName(c.freshName("newInstance"))
val refinedOutput = TypeName(c.freshName("RefinedOutput"))
val result = q"""
def $makeNew[$refinedOutput]($newInstance: (..$parameterTypeTrees) => $refinedOutput) =
_root_.com.thoughtworks.feature.Factory.make[$output, ((..$parameterTypeTrees) => $refinedOutput) {
def apply(..$refinedTree): $refinedOutput
}]($newInstance)
def $constructorMethod(..$parameterTrees) = {
final class $mixinClassName extends {
..$refinementTrees
..$overridenTypes
..$earlyProxies
} with ..$componentTypes {
..$lateProxies
..$injects
}
new $mixinClassName
}
$makeNew($constructorMethod _)
"""
// c.info(c.enclosingPosition, show(result), true)
result
}
}
}