@@ -128,6 +128,7 @@ class ClassfileParser(
128128 for (i <- 0 until in.nextChar) parseMember(method = false )
129129 for (i <- 0 until in.nextChar) parseMember(method = true )
130130 classInfo = parseAttributes(classRoot.symbol, classInfo)
131+ if (isAnnotation) addAnnotationConstructor(classInfo)
131132 setClassInfo(classRoot, classInfo)
132133 setClassInfo(moduleRoot, staticInfo)
133134 }
@@ -544,6 +545,45 @@ class ClassfileParser(
544545 newType
545546 }
546547
548+ /** Add a synthetic constructor and potentially also default getters which
549+ * reflects the fields of the annotation with given `classInfo`.
550+ * Annotations in Scala are assumed to get all their arguments as constructor
551+ * parameters. For Java annotations we need to fake it by making up the constructor.
552+ * Note that default getters have type Nothing. That's OK because we need
553+ * them only to signal that the corresponding parameter is optional.
554+ */
555+ def addAnnotationConstructor (classInfo : Type , tparams : List [Symbol ] = Nil )(implicit ctx : Context ): Unit = {
556+ def addDefaultGetter (attr : Symbol , n : Int ) =
557+ ctx.newSymbol(
558+ owner = moduleRoot.symbol,
559+ name = nme.CONSTRUCTOR .defaultGetterName(n),
560+ flags = attr.flags & Flags .AccessFlags ,
561+ info = defn.NothingType ).entered
562+
563+ classInfo match {
564+ case classInfo @ TempPolyType (tparams, restpe) if tparams.isEmpty =>
565+ addAnnotationConstructor(restpe, tparams)
566+ case classInfo : TempClassInfoType =>
567+ val attrs = classInfo.decls.toList.filter(_.isTerm)
568+ val targs = tparams.map(_.typeRef)
569+ val methType = MethodType (
570+ attrs.map(_.name.asTermName),
571+ attrs.map(_.info.resultType),
572+ classRoot.typeRef.appliedTo(targs))
573+ val constr = ctx.newSymbol(
574+ owner = classRoot.symbol,
575+ name = nme.CONSTRUCTOR ,
576+ flags = Flags .Synthetic ,
577+ info = if (tparams.isEmpty) methType else TempPolyType (tparams, methType)
578+ ).entered
579+ for ((attr, i) <- attrs.zipWithIndex)
580+ if (attr.hasAnnotation(defn.AnnotationDefaultAnnot )) {
581+ constr.setFlag(Flags .HasDefaultParams )
582+ addDefaultGetter(attr, i)
583+ }
584+ }
585+ }
586+
547587 /** Enter own inner classes in the right scope. It needs the scopes to be set up,
548588 * and implicitly current class' superclasses.
549589 */
0 commit comments