-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java parser: add support for unnamed classes (JEP-445)
Fixes #18584
- Loading branch information
Showing
8 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
compiler/test/dotty/tools/dotc/parsing/JavaJep445ParserTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package dotty.tools.dotc.parsing | ||
|
||
import dotty.tools.DottyTest | ||
import dotty.tools.dotc.ast.Trees.{Ident, PackageDef, TypeDef} | ||
import dotty.tools.dotc.ast.untpd | ||
import dotty.tools.dotc.ast.untpd.ModuleDef | ||
import dotty.tools.dotc.core.Contexts.{Context, ContextBase} | ||
import dotty.tools.dotc.core.StdNames.tpnme | ||
import dotty.tools.dotc.printing.{PlainPrinter, Printer} | ||
import dotty.tools.dotc.util.SourceFile | ||
import dotty.tools.io.PlainFile | ||
import org.junit.Assert.{assertTrue, fail} | ||
import org.junit.Test | ||
import JavaParsers.JavaParser | ||
|
||
class JavaJep445ParserTest extends DottyTest { | ||
|
||
@Test def `the parser produces same trees for a class and an equivalent unnamed class` | ||
: Unit = { | ||
|
||
val unnamed = JavaParser( | ||
SourceFile.virtual( | ||
"MyUnnamed.java", | ||
s""" | ||
|import some.pkg.*; | ||
| | ||
|@interface InnerAnnotation {} | ||
| | ||
|interface InnerInterface {} | ||
| | ||
|@Magic | ||
|public volatile double d; | ||
| | ||
|void main() {} | ||
| | ||
|interface SecondInnerInterface {} | ||
| | ||
|""".stripMargin | ||
) | ||
).parse() | ||
|
||
val named = JavaParser( | ||
SourceFile.virtual( | ||
"SomeFile.java", | ||
s""" | ||
|import some.pkg.*; | ||
| | ||
|private final class MyUnnamed { | ||
| | ||
| @interface InnerAnnotation {} | ||
| | ||
| interface InnerInterface {} | ||
| | ||
| @Magic | ||
| public volatile double d; | ||
| | ||
| void main() {} | ||
| | ||
| interface SecondInnerInterface {} | ||
| | ||
|} | ||
|""".stripMargin | ||
) | ||
).parse() | ||
|
||
assertTrue( | ||
"expected same trees for named and unnamed classes", | ||
unnamed.sameTree(named) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
class B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
void main() {} |
5 changes: 5 additions & 0 deletions
5
tests/pos-java21+/jep445/UnnamedStartsWithAnnotatedField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
@MyAnnotation | ||
int myInt = 10; | ||
|
||
void main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
private volatile int myInt = 10; | ||
|
||
String hello() { | ||
return "hello"; | ||
} | ||
|
||
interface Inner {} | ||
|
||
void main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
class InnerOfUnnamed {} | ||
|
||
void main() {} |