Skip to content
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

Handle nested records #3814

Merged
merged 1 commit into from Dec 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -271,6 +271,26 @@ void record_permitStaticFields() {
assertOneRecordDeclaration(cu);
}

@Test
void record_permitPublicStaticFieldInRecord1() {
String s = "public final record RecordPublicField() {" +
" public static final Object EMPTY = new Object();" +
"}\n";
CompilationUnit cu = TestParser.parseCompilationUnit(s);
assertOneRecordDeclaration(cu);
}

@Test
void record_permitPublicStaticFieldInNestedRecord() {
String s = "public final record RecordTopLevel(Object member) {\n" +
" private static record RecordNested() {\n" +
" public static final RecordNested EMPTY = new RecordNested();\n" +
" }\n" +
"}\n";
CompilationUnit cu = TestParser.parseCompilationUnit(s);
assertTwoRecordDeclarations(cu);
}

@Test
void record_permitStaticFields2() {
String s = "" +
Expand Down Expand Up @@ -717,4 +737,9 @@ private void assertOneRecordDeclaration(CompilationUnit cu) {
List<RecordDeclaration> recordDeclarations = cu.findAll(RecordDeclaration.class);
assertEquals(1, recordDeclarations.size());
}

private void assertTwoRecordDeclarations(CompilationUnit cu) {
List<RecordDeclaration> recordDeclarations = cu.findAll(RecordDeclaration.class);
assertEquals(2, recordDeclarations.size());
}
}
19 changes: 13 additions & 6 deletions javaparser-core/src/main/javacc/java.jj
Expand Up @@ -1292,13 +1292,14 @@ ModifierHolder Modifiers():
* Also note that JavaParser handles the hierarchy/specialisations of classes, interfaces, and enums differently to the JLS.
* <ul>
* <li>This {@code ClassOrInterfaceDeclaration} refers to the {@code NormalClassDeclaration} and {@code NormalInterfaceDeclaration},</li>
* <li>while {@code AnnotationTypeDeclaration} and {@code EnumDeclaration} are handled separately within this grammar.</li>
* <li>while {@code AnnotationTypeDeclaration}, {@code EnumDeclaration}, and {@code RecordDeclaration} are handled separately within this grammar.</li>
* </ul>
* https://docs.oracle.com/javase/specs/jls/se15/html/jls-8.html#jls-8.1
* https://docs.oracle.com/javase/specs/jls/se17/html/jls-8.html#jls-8.1
* <pre>{@code
* ClassDeclaration:
* NormalClassDeclaration
* EnumDeclaration
* RecordDeclaration
* NormalClassDeclaration:
* {ClassModifier} class TypeIdentifier [TypeParameters] [Superclass] [Superinterfaces] ClassBody
* }</pre>
Expand Down Expand Up @@ -1339,11 +1340,11 @@ ClassOrInterfaceDeclaration ClassOrInterfaceDeclaration(ModifierHolder modifier)
}

/**
* https://openjdk.java.net/jeps/395#Java-grammar
* https://docs.oracle.com/javase/specs/jls/se17/html/jls-8.html#jls-8.10
* <pre>{@code
* RecordDeclaration:
* {ClassModifier} `record` TypeIdentifier [TypeParameters]
* RecordHeader [SuperInterfaces] RecordBody
* RecordHeader [ClassImplements] RecordBody
*
* RecordHeader:
* `(` [RecordComponentList] `)`
Expand All @@ -1352,11 +1353,14 @@ ClassOrInterfaceDeclaration ClassOrInterfaceDeclaration(ModifierHolder modifier)
* RecordComponent { `,` RecordComponent}
*
* RecordComponent:
* {Annotation} UnannType Identifier
* {RecordComponentModifier} UnannType Identifier
* VariableArityRecordComponent
*
* VariableArityRecordComponent:
* {Annotation} UnannType {Annotation} `...` Identifier
* {RecordComponentModifier} UnannType {Annotation} `...` Identifier
*
* RecordComponentModifier:
* Annotation
*
* RecordBody:
* `{` {RecordBodyDeclaration} `}`
Expand Down Expand Up @@ -1781,6 +1785,9 @@ BodyDeclaration<?> RecordBodyDeclaration():
|
LOOKAHEAD("enum")
ret = EnumDeclaration(modifiers)
|
LOOKAHEAD("record")
ret = RecordDeclaration(modifiers)
|
LOOKAHEAD("@" "interface")
ret = AnnotationTypeDeclaration(modifiers)
Expand Down