@@ -11,6 +11,12 @@ import NameKinds.DefaultGetterName
1111import Annotations .Annotation
1212
1313object 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+
1420 /** Generate proxy classes for @main functions.
1521 * A function like
1622 *
@@ -29,7 +35,7 @@ object MainProxies {
2935 * catch case err: ParseError => showError(err)
3036 * }
3137 */
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 ] = {
3339 import tpd ._
3440 def mainMethods (stats : List [Tree ]): List [Symbol ] = stats.flatMap {
3541 case stat : DefDef if stat.symbol.hasAnnotation(defn.MainAnnot ) =>
@@ -39,11 +45,11 @@ object MainProxies {
3945 case _ =>
4046 Nil
4147 }
42- mainMethods(stats).flatMap(mainProxyOld )
48+ mainMethods(stats).flatMap(mainProxy )
4349 }
4450
4551 import untpd ._
46- def mainProxyOld (mainFun : Symbol )(using Context ): List [TypeDef ] = {
52+ private def mainProxy (mainFun : Symbol )(using Context ): List [TypeDef ] = {
4753 val mainAnnotSpan = mainFun.getAnnotation(defn.MainAnnot ).get.tree.span
4854 def pos = mainFun.sourcePos
4955 val argsRef = Ident (nme.args)
@@ -160,7 +166,7 @@ object MainProxies {
160166 * }
161167 * }
162168 */
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 ] = {
164170 import tpd ._
165171
166172 /**
@@ -183,12 +189,12 @@ object MainProxies {
183189 def mainMethods (scope : Tree , stats : List [Tree ]): List [(Symbol , ParameterAnnotationss , DefaultValueSymbols , Option [Comment ])] = stats.flatMap {
184190 case stat : DefDef =>
185191 val sym = stat.symbol
186- sym.annotations.filter(_.matches(defn.MainAnnot )) match {
192+ sym.annotations.filter(_.matches(defn.MainAnnotationClass )) match {
187193 case Nil =>
188194 Nil
189195 case _ :: Nil =>
190196 val paramAnnotations = stat.paramss.flatMap(_.map(
191- valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotParameterAnnotation ))
197+ valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotationParameterAnnotation ))
192198 ))
193199 (sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
194200 case mainAnnot :: others =>
@@ -202,11 +208,11 @@ object MainProxies {
202208 }
203209
204210 // 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 )
206212 }
207213
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
210216 def pos = mainFun.sourcePos
211217 val cmdName : TermName = Names .termName(" cmd" )
212218
@@ -244,15 +250,15 @@ object MainProxies {
244250 val (argRef, formalType, getterSym) = {
245251 val argRef0 = Apply (Ident (argName), Nil )
246252 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 )
249255 }
250256
251257 // The ParameterInfos
252258 val parameterInfos = {
253259 val param = paramName.toString
254260 val paramInfosTree = New (
255- TypeTree (defn.MainAnnotParameterInfos .typeRef),
261+ TypeTree (defn.MainAnnotationParameterInfos .typeRef),
256262 // Arguments to be passed to ParameterInfos' constructor
257263 List (List (lit(param), lit(formalType.show)))
258264 )
@@ -352,11 +358,11 @@ object MainProxies {
352358 cmdName,
353359 TypeTree (),
354360 Apply (
355- Select (instanciateAnnotation(mainAnnot), defn.MainAnnot_command .name),
361+ Select (instanciateAnnotation(mainAnnot), defn.MainAnnotation_command .name),
356362 Ident (nme.args) :: lit(mainFun.showName) :: lit(documentation.mainDoc) :: parameterInfoss
357363 )
358364 )
359- val run = Apply (Select (Ident (cmdName), defn.MainAnnotCommand_run .name), mainCall)
365+ val run = Apply (Select (Ident (cmdName), defn.MainAnnotationCommand_run .name), mainCall)
360366 val body = Block (cmd :: args, run)
361367 val mainArg = ValDef (nme.args, TypeTree (defn.ArrayType .appliedTo(defn.StringType )), EmptyTree )
362368 .withFlags(Param )
@@ -369,7 +375,7 @@ object MainProxies {
369375 case tree => super .transform(tree)
370376 }
371377 val annots = mainFun.annotations
372- .filterNot(_.matches(defn.MainAnnot ))
378+ .filterNot(_.matches(defn.MainAnnotationClass ))
373379 .map(annot => insertTypeSplices.transform(annot.tree))
374380 val mainMeth = DefDef (nme.main, (mainArg :: Nil ) :: Nil , TypeTree (defn.UnitType ), body)
375381 .withFlags(JavaStatic )
0 commit comments