Skip to content

Commit 6d1de70

Browse files
committed
wip
1 parent e9b364a commit 6d1de70

File tree

2 files changed

+14
-30
lines changed

2 files changed

+14
-30
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -826,9 +826,9 @@ object JavaParsers {
826826
addCompanionObject(statics, cls)
827827
}
828828

829-
def unnamedClassDecl(priorTypes: List[Tree], start: Offset): List[Tree] = {
829+
def unnamedClassDecl(priorTypes: List[Tree], firstMemberMods: Modifiers, start: Offset): List[Tree] = {
830830
val name = source.name.replaceAll("\\.java$", "").nn.toTypeName
831-
val (statics, body) = typeBodyDecls(CLASS, name, Nil)
831+
val (statics, body) = typeBodyDecls(CLASS, name, parentTParams = Nil, firstMemberMods = Some(firstMemberMods))
832832

833833
val (priorStatics, priorBody) = priorTypes.partition {
834834
case t: TypeDef => t.mods.is(Flags.JavaStatic)
@@ -920,13 +920,13 @@ object JavaParsers {
920920
defs
921921
}
922922

923-
def typeBodyDecls(parentToken: Int, parentName: Name, parentTParams: List[TypeDef]): (List[Tree], List[Tree]) = {
923+
def typeBodyDecls(parentToken: Int, parentName: Name, parentTParams: List[TypeDef], firstMemberMods: Option[Modifiers] = None): (List[Tree], List[Tree]) = {
924924
val inInterface = definesInterface(parentToken)
925925
val statics = new ListBuffer[Tree]
926926
val members = new ListBuffer[Tree]
927927
while (in.token != RBRACE && in.token != EOF) {
928928
val start = in.offset
929-
var mods = modifiers(inInterface)
929+
var mods = (if (statics.isEmpty && members.isEmpty) firstMemberMods else None).getOrElse(modifiers(inInterface))
930930
if (in.token == LBRACE) {
931931
skipAhead() // skip init block, we just assume we have seen only static
932932
accept(RBRACE)
@@ -1105,8 +1105,7 @@ object JavaParsers {
11051105
if (thisPackageName == tpnme.EMPTY_PACKAGE) {
11061106
// upon encountering non-types directly at a compilation unit level in an unnamed package,
11071107
// the entire compilation unit is treated as a JEP-445 unnamed class
1108-
//TODO support @annotated members of unnamed class
1109-
val cls = unnamedClassDecl(priorTypes = typesBuf.toList, start = afterImports)
1108+
val cls = unnamedClassDecl(priorTypes = typesBuf.toList, firstMemberMods = mods, start = afterImports)
11101109
typesBuf.clear()
11111110
typesBuf ++= cls
11121111
} else {
@@ -1117,7 +1116,6 @@ object JavaParsers {
11171116
}
11181117
}
11191118
}
1120-
11211119
val unit = atSpan(start) { PackageDef(pkg, (buf ++ typesBuf).toList) }
11221120
accept(EOF)
11231121
unit match

compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class JavaJep445ParserTest extends DottyTest {
2020
|
2121
|import some.pkg.*;
2222
|
23-
|private volatile int x = 0;
2423
|@Magic
24+
|private volatile int x = 0;
25+
|
2526
|protected String s = "s";
2627
|
2728
|void main() {}
@@ -36,39 +37,24 @@ class JavaJep445ParserTest extends DottyTest {
3637
fail("TODO")
3738
}
3839

39-
@Test def `treats leading top-level types as nested types of unnamed class`: Unit = {
40+
@Test def `treats leading top-level types as nested types of unnamed class`
41+
: Unit = {
4042
val code =
4143
s"""
4244
|// hello
4345
|
4446
|import some.pkg.*;
4547
|
46-
|interface Inner {}
48+
|@interface InnerAnnotation {}
4749
|
48-
|static class InnerStatic {}
50+
|interface InnerInterface {}
4951
|
50-
|void main() {}
51-
|""".stripMargin
52-
53-
val parser =
54-
JavaParsers.JavaParser(SourceFile.virtual("MyUnnamed.java", code))
55-
val tree = parser.parse()
56-
57-
println(tree.show)
58-
59-
fail("TODO")
60-
}
61-
62-
@Test def `treats leading top-level annotated vars as members of unnamed class`: Unit = {
63-
val code =
64-
s"""
52+
|static class InnerStaticClass {}
6553
|
66-
|import some.pkg.*;
54+
|void main() {}
6755
|
68-
|@MyAnnotation
69-
|int x = 0;
56+
|interface SecondInnerInterface {}
7057
|
71-
|void main() {}
7258
|""".stripMargin
7359

7460
val parser =

0 commit comments

Comments
 (0)