Skip to content

Commit a1a1a4b

Browse files
committed
JavaParsers: fix call to annotation methods
Previously, for tests/run/i2760/Fork.java, the parser output was: abstract class Fork(val value: Int = ???, val warmups: Int = ???) extends ... Since Fork is JavaDefined, calls to `value` were translated into field calls, but `value` is a method, not a field, so i2760 crashed at runtime with NoSuchFieldError. We now generate instead: abstract class Fork private[this](x$1: _root_.scala.Unit) extends ... { def <init>(value: Int = ???, warmups: Int = ???) def value(): Int = 1 def warmups(): Int = 1 } This way we get both named parameters for the constructor and real method calls.
1 parent 1420853 commit a1a1a4b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ object JavaParsers {
123123
// A dummy first constructor is needed for Java classes so that the real constructors see the
124124
// import of the companion object. The constructor has parameter of type Unit so no Java code
125125
// can call it.
126+
// This also avoids clashes between the constructor parameter names and member names.
126127
if (needsDummyConstr) {
127128
stats1 = constr1 :: stats1
128129
constr1 = makeConstructor(List(scalaDot(tpnme.Unit)), tparams, Flags.JavaDefined | Flags.PrivateLocal)
@@ -133,7 +134,7 @@ object JavaParsers {
133134
def makeSyntheticParam(count: Int, tpt: Tree): ValDef =
134135
makeParam(nme.syntheticParamName(count), tpt)
135136
def makeParam(name: TermName, tpt: Tree, defaultValue: Tree = EmptyTree): ValDef =
136-
ValDef(name, tpt, defaultValue).withMods(Modifiers(Flags.JavaDefined | Flags.ParamAccessor))
137+
ValDef(name, tpt, defaultValue).withMods(Modifiers(Flags.JavaDefined | Flags.Param))
137138

138139
def makeConstructor(formals: List[Tree], tparams: List[TypeDef], flags: FlagSet = Flags.JavaDefined) = {
139140
val vparams = formals.zipWithIndex.map { case (p, i) => makeSyntheticParam(i + 1, p) }
@@ -787,8 +788,7 @@ object JavaParsers {
787788
}
788789
val constr = DefDef(nme.CONSTRUCTOR,
789790
List(), List(constructorParams), TypeTree(), EmptyTree).withMods(Modifiers(Flags.JavaDefined))
790-
val body1 = body.filterNot(_.isInstanceOf[DefDef])
791-
val templ = makeTemplate(annotationParents, constr :: body1, List(), false)
791+
val templ = makeTemplate(annotationParents, constr :: body, List(), true)
792792
val annot = atPos(start, nameOffset) {
793793
TypeDef(name, templ).withMods(mods | Flags.Abstract)
794794
}

0 commit comments

Comments
 (0)