/
Factory.scala
455 lines (418 loc) · 16.2 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
package com.thoughtworks.feature
import scala.language.experimental.macros
import scala.reflect.macros.whitebox
import com.thoughtworks.Extractor._
import scala.annotation.meta.getter
import scala.annotation.{StaticAnnotation, compileTimeOnly}
import scala.collection.generic.Growable
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
/** A factory to create new instances, especially dynamic mix-ins.
*
* @note Factories may be nested
*
* {{{
* import com.thoughtworks.feature.Factory.inject
* trait Outer {
* trait AbstractParameterApi
* type AbstractParameter <: AbstractParameterApi
*
* trait InnerApi {
* def foo: AbstractParameter
* }
* type Inner <: InnerApi
*
* @inject val innerFactory: Factory.UnaryByName[AbstractParameter, Inner]
* }
*
* Factory[Outer]
* }}}
*
* @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.Nullary]],
* which can be summoned by [[Predef.implicitly]],
*
* {{{
* val nullaryFactory = implicitly[Factory.Nullary[Foo[Int]]]
* }}}
*
* and [[newInstance]] method is available on the [[Factory.Nullary]] 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 bar: Int
* var baz: Long
* }
* }}}
* 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(bar = 1, baz = 2L)
* createdFromNamedArguments.bar should be(1)
* createdFromNamedArguments.baz should be(2L)
* }}}
* When using unnamed parameters, the parameters should be passed in alphabetical order
* {{{
* val createdFromUnnamedArguments: Foo = factory.newInstance(1, 2L)
* createdFromUnnamedArguments.bar should be(1)
* createdFromUnnamedArguments.baz should be(2L)
* }}}
*
* @note This [[Factory]] disallows creating types that has an abstract member whose type depends on nested types
*
* {{{
* trait Outer {
* trait Inner
* val inner: Option[Inner]
* }
*
* "Factory.newInstance[Outer](inner = None)" shouldNot typeCheck
* }}}
*
* @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 Nullary[Output] = Lt[Output, () => Output]
type Unary[-Parameter, Output] = Lt[Output, Parameter => Output]
type UnaryByName[-Parameter, Output] = Lt[Output, (=> Parameter) => 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 unzip4[A, B, C, D](xs: Traversable[(A, B, C, D)]): (List[A], List[B], List[C], List[D]) =
xs.foldRight[(List[A], List[B], List[C], List[D])]((Nil, Nil, Nil, Nil)) { (x, res) =>
val (a, b, c, d) = x
(a :: res._1, b :: res._2, c :: res._3, d :: res._4)
}
private def selfTypes(t: Type): List[Type] = {
val builder = new ListBuffer[Type]
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) if refinedScope.isEmpty =>
superTypes.foreach(go)
case typeRef: TypeRef =>
val symbol = dealiased.typeSymbol
if (symbol.isClass) {
val selfType = symbol.asClass.selfType.asSeenFrom(dealiased, symbol)
go(selfType)
}
builder += dealiased
case _ =>
builder += dealiased
}
}
}
go(t)
builder.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 componentTypes = demixin(glb(selfTypes(output)))
val linearOutput = internal.refinedType(componentTypes, c.internal.enclosingOwner)
val linearSymbol = linearOutput.typeSymbol
val linearThis = internal.thisType(linearSymbol)
val mixinClassName = c.freshName(c.internal.enclosingOwner.name.encodedName.toTypeName)
class ThisUntyper extends Untyper[c.universe.type](c.universe) {
private def replaceThisValue: PartialFunction[Type, Tree] = {
case tt @ ThisType(symbol) if symbol == linearSymbol =>
This(mixinClassName)
}
override def singletonValue: PartialFunction[Type, Tree] = {
replaceThisValue.orElse(super.singletonValue)
}
}
def untyper = new ThisUntyper
def dealiasUntyper = new ThisUntyper {
override protected def preprocess(tpe: Type): Type = tpe.dealias
}
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 = untyper.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}: ${untyper.untype(argumentSymbol.info)}"
} else {
q"val ${argumentSymbol.name.toTermName}: ${untyper.untype(argumentSymbol.info)}"
}
})
val typeParameterTrees = methodType.typeParams.map { typeParamSymbol =>
untyper.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, 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 = dealiasUntyper.untype(methodType.finalResultType)
if (memberSymbol.isVar || memberSymbol.setter != NoSymbol) {
(q"override var $methodName = $argumentName",
resultTypeTree,
q"val $argumentName: $resultTypeTree",
q"val $methodName: $resultTypeTree")
} else if (memberSymbol.isVal || memberSymbol.isGetter || memberSymbol.isStable) {
(q"override val $methodName = $argumentName",
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 = dealiasUntyper.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 =>
untyper.typeDefinition(linearThis)(typeParamSymbol.asType)
}
(q"override def $methodName[..$typeParameterTrees](...$argumentTrees) = $argumentName",
functionTypeTree,
q"val $argumentName: $functionTypeTree",
q"val $methodName: $functionTypeTree")
}
}
val (proxies, parameterTypeTrees, parameterTrees, refinedTree) = unzip4(zippedProxies)
val (defProxies, valProxies) = proxies.partition(_.isDef)
val typeMembers = for {
componentType <- componentTypes
member <- componentType.members
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 =>
untyper.typeDefinition(linearThis)(typeParamSymbol.asType)
}
val TypeBounds(_, lowerBound) = glbType.resultType
val result = q"override type $name[..$typeParameterTrees] = ${untyper.untype(lowerBound)}"
// c.info(c.enclosingPosition, show(result), true)
result
}
val overridenTypes = for {
(name, members) <- groupedTypeSymbols
if members.forall(isAbstractType)
} yield overrideType(name, members)
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 {
..$overridenTypes
..$valProxies
} with ..$componentTypes {
..$defProxies
..$injects
}
new $mixinClassName
}
$makeNew($constructorMethod _)
"""
// c.info(c.enclosingPosition, show(result), true)
result
}
}
}