Skip to content

Commit

Permalink
Fix tests (and refine ReservedKeywordValidatorRecord)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 2, 2021
1 parent f37bfd9 commit 3f01add
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,15 @@ void localRecords() {

}

@Test
void instanceFieldIsNotAllowedInRecord() {
String s = "record X { int record; }";

assertThrows(AssertionFailedError.class, () -> {
CompilationUnit cu = TestParser.parseCompilationUnit(s);
});
}

private void assertCompilationFails(String s) {
assertThrows(AssertionFailedError.class, () -> {
CompilationUnit cu = TestParser.parseCompilationUnit(s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void recordUsedAsClassName() {
void recordUsedAsFieldName() {
String s = "class X { int record; }";
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(s));
TestUtils.assertProblems(result, "(line 1,col 15) 'record' cannot be used as an identifier as it is a keyword.");
TestUtils.assertNoProblems(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void recordUsedAsClassName() {
void recordUsedAsFieldName() {
String s = "class X { int record; }";
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(s));
TestUtils.assertProblems(result, "(line 1,col 15) 'record' cannot be used as an identifier as it is a keyword.");
TestUtils.assertNoProblems(result);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void yieldAllowed() {
class Record {

@Nested
class RecordAsIdentifierForbidden {
class RecordAsIdentifierSometimesForbidden {
@Test
void recordUsedAsClassName() {
String s = "public class record {}";
Expand All @@ -66,11 +66,18 @@ void recordUsedAsClassName() {
}

@Test
void recordUsedAsFieldName() {
void recordUsedAsFieldNameInClass() {
String s = "class X { int record; }";
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(s));
TestUtils.assertNoProblems(result);
}

@Test
void recordUsedAsFieldNameInInterface() {
String s = "interface X { int record; }";
ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(s));
TestUtils.assertNoProblems(result);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.RecordDeclaration;
import com.github.javaparser.ast.expr.Name;
import com.github.javaparser.ast.expr.SimpleName;

Expand All @@ -43,25 +44,35 @@ public ReservedKeywordValidatorRecord() {

@Override
public void visit(Name n, ProblemReporter arg) {
if (n.getIdentifier().equals(keyword) && usedAsClassOrInterfaceName(n)) {
if (n.getIdentifier().equals(keyword) && validUsage(n)) {
arg.report(n, error);
}
super.visit(n, arg);
}

@Override
public void visit(SimpleName n, ProblemReporter arg) {
if (n.getIdentifier().equals(keyword) && usedAsClassOrInterfaceName(n)) {
if (n.getIdentifier().equals(keyword) && validUsage(n)) {
arg.report(n, error);
}
super.visit(n, arg);
}

private boolean usedAsClassOrInterfaceName(Node n) {
if (!n.getParentNode().isPresent()) {
private boolean validUsage(Node node) {
if (!node.getParentNode().isPresent()) {
return false;
}
Node parent = n.getParentNode().get();
return (parent instanceof ClassOrInterfaceDeclaration);
Node parent = node.getParentNode().get();

if (parent instanceof ClassOrInterfaceDeclaration) {
return true;
}

if (!parent.getParentNode().isPresent()) {
return false;
}
Node grandParent = parent.getParentNode().get();

return (grandParent instanceof ClassOrInterfaceDeclaration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Java14PreviewValidator() {
{
// first preview within Java 14 - https://openjdk.java.net/jeps/359
remove(noRecordDeclaration);
add(recordAsTypeIdentifierNotAllowed);
add(recordAsClassAndInterfaceIdentifierNotAllowed);
add(recordDeclarationValidator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import com.github.javaparser.ast.body.RecordDeclaration;
import com.github.javaparser.ast.validator.ReservedKeywordValidator;
import com.github.javaparser.ast.validator.ReservedKeywordValidatorRecord;
import com.github.javaparser.ast.validator.SingleNodeTypeValidator;
import com.github.javaparser.ast.validator.Validator;
import com.github.javaparser.ast.validator.language_level_validations.chunks.RecordDeclarationValidator;
Expand All @@ -45,7 +46,7 @@ public class Java14Validator extends Java13Validator {
* </blockquote>
* https://docs.oracle.com/javase/specs/jls/se15/preview/specs/records-jls.html#jls-3.8
*/
final Validator recordAsTypeIdentifierNotAllowed = new ReservedKeywordValidator("record");
final Validator recordAsClassAndInterfaceIdentifierNotAllowed = new ReservedKeywordValidatorRecord();

final Validator recordDeclarationValidator = new SingleNodeTypeValidator<>(RecordDeclaration.class, new RecordDeclarationValidator());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Java15PreviewValidator() {
{
// Records - 2nd preview within Java 15 - https://openjdk.java.net/jeps/384
remove(noRecordDeclaration);
add(recordAsTypeIdentifierNotAllowed);
add(recordAsClassAndInterfaceIdentifierNotAllowed);
add(recordDeclarationValidator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@

package com.github.javaparser.ast.validator.language_level_validations;

import com.github.javaparser.ast.body.RecordDeclaration;
import com.github.javaparser.ast.validator.ReservedKeywordValidatorRecord;
import com.github.javaparser.ast.validator.SingleNodeTypeValidator;
import com.github.javaparser.ast.validator.Validator;
import com.github.javaparser.ast.validator.language_level_validations.chunks.RecordDeclarationValidator;

/**
* This validator validates according to Java 16 syntax rules.
Expand All @@ -34,8 +31,6 @@
*/
public class Java16Validator extends Java15Validator {

final Validator recordAsClassAndInterfaceIdentifierNotAllowed = new ReservedKeywordValidatorRecord();

public Java16Validator() {
super();

Expand Down

0 comments on commit 3f01add

Please sign in to comment.