Skip to content

Commit 799f4c5

Browse files
noti0na1smarter
authored andcommitted
Add Support for Parsing Annotations without Arguments to Java Parser (#6893)
1 parent 5699738 commit 799f4c5

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,43 @@ object JavaParsers {
329329
}
330330

331331
def annotations(): List[Tree] = {
332-
//var annots = new ListBuffer[Tree]
332+
var annots = new ListBuffer[Tree]
333333
while (in.token == AT) {
334334
in.nextToken()
335-
annotation()
335+
annotation() match {
336+
case Some(anno) => annots += anno
337+
case _ =>
338+
}
336339
}
337-
List() // don't pass on annotations for now
340+
annots.toList
338341
}
339342

340343
/** Annotation ::= TypeName [`(` AnnotationArgument {`,` AnnotationArgument} `)`]
341344
*/
342-
def annotation(): Unit = {
343-
qualId()
344-
if (in.token == LPAREN) { skipAhead(); accept(RPAREN) }
345-
else if (in.token == LBRACE) { skipAhead(); accept(RBRACE) }
345+
def annotation(): Option[Tree] = {
346+
val id = convertToTypeId(qualId())
347+
// only parse annotations without arguments
348+
if (in.token == LPAREN && in.lookaheadToken != RPAREN) {
349+
skipAhead()
350+
accept(RPAREN)
351+
None
352+
}
353+
else {
354+
if (in.token == LPAREN) {
355+
in.nextToken()
356+
accept(RPAREN)
357+
}
358+
Some(ensureApplied(Select(New(id), nme.CONSTRUCTOR)))
359+
}
346360
}
347361

348362
def modifiers(inInterface: Boolean): Modifiers = {
349363
var flags: FlagSet = Flags.JavaDefined
350364
// assumed true unless we see public/private/protected
351365
var isPackageAccess = true
352-
var annots: List[Tree] = Nil
366+
var annots = new ListBuffer[Tree]
353367
def addAnnot(sym: ClassSymbol) =
354-
annots :+= atSpan(in.offset) {
368+
annots += atSpan(in.offset) {
355369
in.nextToken()
356370
New(TypeTree(sym.typeRef))
357371
}
@@ -360,7 +374,10 @@ object JavaParsers {
360374
in.token match {
361375
case AT if (in.lookaheadToken != INTERFACE) =>
362376
in.nextToken()
363-
annotation()
377+
annotation() match {
378+
case Some(anno) => annots += anno
379+
case _ =>
380+
}
364381
case PUBLIC =>
365382
isPackageAccess = false
366383
in.nextToken()
@@ -396,7 +413,7 @@ object JavaParsers {
396413
if (isPackageAccess && !inInterface) thisPackageName
397414
else tpnme.EMPTY
398415

399-
return Modifiers(flags, privateWithin) withAnnotations annots
416+
return Modifiers(flags, privateWithin) withAnnotations annots.toList
400417
}
401418
assert(false, "should not be here")
402419
throw new RuntimeException
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class AnnoJavaParent {
2+
public String n(int i) {
3+
return "n: " + i;
4+
}
5+
}
6+
7+
public class AnnoJava extends AnnoJavaParent {
8+
9+
@MyAnno
10+
int f() {
11+
return 0;
12+
}
13+
14+
@MyAnno(1)
15+
int g() {
16+
return 1;
17+
}
18+
19+
@MyAnno()
20+
public static String m(int i) {
21+
return "m: " + i;
22+
}
23+
24+
@Override
25+
public String n(int i) {
26+
return "n: " + i;
27+
}
28+
}
29+
30+
@interface MyAnno {
31+
int value() default 1;
32+
}
33+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class CallJava {
2+
def mm(i: Int): String = AnnoJava.m(i)
3+
}
4+

0 commit comments

Comments
 (0)