Skip to content

Add Support of Parsing Annotations without Arguments to Java Parser #6893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions compiler/src/dotty/tools/dotc/parsing/JavaParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,43 @@ object JavaParsers {
}

def annotations(): List[Tree] = {
//var annots = new ListBuffer[Tree]
var annots = new ListBuffer[Tree]
while (in.token == AT) {
in.nextToken()
annotation()
annotation() match {
case Some(anno) => annots += anno
case _ =>
}
}
List() // don't pass on annotations for now
annots.toList
}

/** Annotation ::= TypeName [`(` AnnotationArgument {`,` AnnotationArgument} `)`]
*/
def annotation(): Unit = {
qualId()
if (in.token == LPAREN) { skipAhead(); accept(RPAREN) }
else if (in.token == LBRACE) { skipAhead(); accept(RBRACE) }
def annotation(): Option[Tree] = {
val id = convertToTypeId(qualId())
// only parse annotations without arguments
if (in.token == LPAREN && in.lookaheadToken != RPAREN) {
skipAhead()
accept(RPAREN)
None
}
else {
if (in.token == LPAREN) {
in.nextToken()
accept(RPAREN)
}
Some(ensureApplied(Select(New(id), nme.CONSTRUCTOR)))
}
}

def modifiers(inInterface: Boolean): Modifiers = {
var flags: FlagSet = Flags.JavaDefined
// assumed true unless we see public/private/protected
var isPackageAccess = true
var annots: List[Tree] = Nil
var annots = new ListBuffer[Tree]
def addAnnot(sym: ClassSymbol) =
annots :+= atSpan(in.offset) {
annots += atSpan(in.offset) {
in.nextToken()
New(TypeTree(sym.typeRef))
}
Expand All @@ -360,7 +374,10 @@ object JavaParsers {
in.token match {
case AT if (in.lookaheadToken != INTERFACE) =>
in.nextToken()
annotation()
annotation() match {
case Some(anno) => annots += anno
case _ =>
}
case PUBLIC =>
isPackageAccess = false
in.nextToken()
Expand Down Expand Up @@ -396,7 +413,7 @@ object JavaParsers {
if (isPackageAccess && !inInterface) thisPackageName
else tpnme.EMPTY

return Modifiers(flags, privateWithin) withAnnotations annots
return Modifiers(flags, privateWithin) withAnnotations annots.toList
}
assert(false, "should not be here")
throw new RuntimeException
Expand Down
33 changes: 33 additions & 0 deletions tests/pos/annotations-in-java/AnnoJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class AnnoJavaParent {
public String n(int i) {
return "n: " + i;
}
}

public class AnnoJava extends AnnoJavaParent {

@MyAnno
int f() {
return 0;
}

@MyAnno(1)
int g() {
return 1;
}

@MyAnno()
public static String m(int i) {
return "m: " + i;
}

@Override
public String n(int i) {
return "n: " + i;
}
}

@interface MyAnno {
int value() default 1;
}

4 changes: 4 additions & 0 deletions tests/pos/annotations-in-java/CallJava.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class CallJava {
def mm(i: Int): String = AnnoJava.m(i)
}