@@ -11,6 +11,12 @@ import NameKinds.DefaultGetterName
11
11
import Annotations .Annotation
12
12
13
13
object MainProxies {
14
+
15
+ /** Generate proxy classes for @main functions and @myMain functions where myMain <:< MainAnnotation */
16
+ def proxies (stats : List [tpd.Tree ])(using Context ): List [untpd.Tree ] = {
17
+ mainAnnotationProxies(stats) ++ mainProxies(stats)
18
+ }
19
+
14
20
/** Generate proxy classes for @main functions.
15
21
* A function like
16
22
*
@@ -29,7 +35,7 @@ object MainProxies {
29
35
* catch case err: ParseError => showError(err)
30
36
* }
31
37
*/
32
- def mainProxiesOld (stats : List [tpd.Tree ])(using Context ): List [untpd.Tree ] = {
38
+ private def mainProxies (stats : List [tpd.Tree ])(using Context ): List [untpd.Tree ] = {
33
39
import tpd ._
34
40
def mainMethods (stats : List [Tree ]): List [Symbol ] = stats.flatMap {
35
41
case stat : DefDef if stat.symbol.hasAnnotation(defn.MainAnnot ) =>
@@ -39,11 +45,11 @@ object MainProxies {
39
45
case _ =>
40
46
Nil
41
47
}
42
- mainMethods(stats).flatMap(mainProxyOld )
48
+ mainMethods(stats).flatMap(mainProxy )
43
49
}
44
50
45
51
import untpd ._
46
- def mainProxyOld (mainFun : Symbol )(using Context ): List [TypeDef ] = {
52
+ private def mainProxy (mainFun : Symbol )(using Context ): List [TypeDef ] = {
47
53
val mainAnnotSpan = mainFun.getAnnotation(defn.MainAnnot ).get.tree.span
48
54
def pos = mainFun.sourcePos
49
55
val argsRef = Ident (nme.args)
@@ -160,7 +166,7 @@ object MainProxies {
160
166
* }
161
167
* }
162
168
*/
163
- def mainProxies (stats : List [tpd.Tree ])(using Context ): List [untpd.Tree ] = {
169
+ private def mainAnnotationProxies (stats : List [tpd.Tree ])(using Context ): List [untpd.Tree ] = {
164
170
import tpd ._
165
171
166
172
/**
@@ -183,12 +189,12 @@ object MainProxies {
183
189
def mainMethods (scope : Tree , stats : List [Tree ]): List [(Symbol , ParameterAnnotationss , DefaultValueSymbols , Option [Comment ])] = stats.flatMap {
184
190
case stat : DefDef =>
185
191
val sym = stat.symbol
186
- sym.annotations.filter(_.matches(defn.MainAnnot )) match {
192
+ sym.annotations.filter(_.matches(defn.MainAnnotationClass )) match {
187
193
case Nil =>
188
194
Nil
189
195
case _ :: Nil =>
190
196
val paramAnnotations = stat.paramss.flatMap(_.map(
191
- valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotParameterAnnotation ))
197
+ valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotationParameterAnnotation ))
192
198
))
193
199
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
194
200
case mainAnnot :: others =>
@@ -202,11 +208,11 @@ object MainProxies {
202
208
}
203
209
204
210
// Assuming that the top-level object was already generated, all main methods will have a scope
205
- mainMethods(EmptyTree , stats).flatMap(mainProxy )
211
+ mainMethods(EmptyTree , stats).flatMap(mainAnnotationProxy )
206
212
}
207
213
208
- def mainProxy (mainFun : Symbol , paramAnnotations : ParameterAnnotationss , defaultValueSymbols : DefaultValueSymbols , docComment : Option [Comment ])(using Context ): List [TypeDef ] = {
209
- val mainAnnot = mainFun.getAnnotation(defn.MainAnnot ).get
214
+ private def mainAnnotationProxy (mainFun : Symbol , paramAnnotations : ParameterAnnotationss , defaultValueSymbols : DefaultValueSymbols , docComment : Option [Comment ])(using Context ): List [TypeDef ] = {
215
+ val mainAnnot = mainFun.getAnnotation(defn.MainAnnotationClass ).get
210
216
def pos = mainFun.sourcePos
211
217
val cmdName : TermName = Names .termName(" cmd" )
212
218
@@ -244,15 +250,15 @@ object MainProxies {
244
250
val (argRef, formalType, getterSym) = {
245
251
val argRef0 = Apply (Ident (argName), Nil )
246
252
if formal.isRepeatedParam then
247
- (repeated(argRef0), formal.argTypes.head, defn.MainAnnotCommand_varargGetter )
248
- else (argRef0, formal, defn.MainAnnotCommand_argGetter )
253
+ (repeated(argRef0), formal.argTypes.head, defn.MainAnnotationCommand_varargGetter )
254
+ else (argRef0, formal, defn.MainAnnotationCommand_argGetter )
249
255
}
250
256
251
257
// The ParameterInfos
252
258
val parameterInfos = {
253
259
val param = paramName.toString
254
260
val paramInfosTree = New (
255
- TypeTree (defn.MainAnnotParameterInfos .typeRef),
261
+ TypeTree (defn.MainAnnotationParameterInfos .typeRef),
256
262
// Arguments to be passed to ParameterInfos' constructor
257
263
List (List (lit(param), lit(formalType.show)))
258
264
)
@@ -352,11 +358,11 @@ object MainProxies {
352
358
cmdName,
353
359
TypeTree (),
354
360
Apply (
355
- Select (instanciateAnnotation(mainAnnot), defn.MainAnnot_command .name),
361
+ Select (instanciateAnnotation(mainAnnot), defn.MainAnnotation_command .name),
356
362
Ident (nme.args) :: lit(mainFun.showName) :: lit(documentation.mainDoc) :: parameterInfoss
357
363
)
358
364
)
359
- val run = Apply (Select (Ident (cmdName), defn.MainAnnotCommand_run .name), mainCall)
365
+ val run = Apply (Select (Ident (cmdName), defn.MainAnnotationCommand_run .name), mainCall)
360
366
val body = Block (cmd :: args, run)
361
367
val mainArg = ValDef (nme.args, TypeTree (defn.ArrayType .appliedTo(defn.StringType )), EmptyTree )
362
368
.withFlags(Param )
@@ -369,7 +375,7 @@ object MainProxies {
369
375
case tree => super .transform(tree)
370
376
}
371
377
val annots = mainFun.annotations
372
- .filterNot(_.matches(defn.MainAnnot ))
378
+ .filterNot(_.matches(defn.MainAnnotationClass ))
373
379
.map(annot => insertTypeSplices.transform(annot.tree))
374
380
val mainMeth = DefDef (nme.main, (mainArg :: Nil ) :: Nil , TypeTree (defn.UnitType ), body)
375
381
.withFlags(JavaStatic )
0 commit comments