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 3 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
40 changes: 29 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,44 @@ 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())
var skipAnnot = false

// only parse annotations without arguments
if (in.token == LPAREN) {
if (in.lookaheadToken != RPAREN) {
skipAnnot = true
skipAhead()
}
else in.nextToken()
accept(RPAREN)
}

if (skipAnnot) None
else 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 +375,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 +414,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
23 changes: 23 additions & 0 deletions tests/pos/annotations-in-java/AnnoJava.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AnnoJavaParent {
public String n(int i) {
return "n: " + i;
}
}

public class AnnoJava extends AnnoJavaParent {

@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)
}